问题描述
请我想构建一个 NSD(网络服务发现)应用程序,使用 Jni 和 Qt,但我收到以下错误:
JNI 在应用程序中检测到错误:JNI GetStaticmethodID 调用时出现挂起异常 java.lang.NullPointerException:尝试调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)”在空对象引用上
代码如下:
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.net.*;
import android.net.wifi.WifiManager;
import android.net.wifi.*;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.DhcpInfo;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdManager.discoveryListener;
import android.util.Log;
import android.net.nsd.NsdServiceInfo;
import android.text.format.Formatter;
import java.net.NetworkInterface;
import java.net.socketException;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.net.InetAddress;
import java.io.IOException;
import java.net.UnkNownHostException;
//import android.app.Activity;
import java.lang.reflect.Method;
public class MusicList extends org.qtproject.qt5.android.bindings.QtActivity {
final String TAG="display Message";
final String SERVICE_TYPE="_http._tcp.";
final String serviceName= "esp32";
private static NsdManager.discoveryListener discoveryListener;
private static NsdManager nsdManager;
private static Context context;
public void initializediscoveryListener() {
// Instantiate a new discoveryListener
discoveryListener = new NsdManager.discoveryListener() {
// Called as soon as service discovery begins.
@Override
public void ondiscoveryStarted(String regType) {
Log.d(TAG,"Service discovery started");
}
@Override
public void onServiceFound(NsdServiceInfo service) {
// A service was found! Do something with it.
Log.d(TAG,"Service discovery success" + service);
if (!service.getServiceType().equals(SERVICE_TYPE)) {
// Service type is the string containing the protocol and
// transport layer for this service.
Log.d(TAG,"UnkNown Service Type: " + service.getServiceType());
} else if (service.getServiceName().equals(serviceName)) {
// The name of the service tells the user what they'd be
// connecting to. It Could be "Bob's Chat App".
Log.d(TAG,"Same machine: " + serviceName);
} else if (service.getServiceName().contains("NsdChat")){
// nsdManager.resolveService(service,resolveListener);
}
}
@Override
public void onServiceLost(NsdServiceInfo service) {
// When the network service is no longer available.
// Internal bookkeeping code goes here.
Log.e(TAG,"service lost: " + service);
}
@Override
public void ondiscoveryStopped(String serviceType) {
Log.i(TAG,"discovery stopped: " + serviceType);
}
@Override
public void onStartdiscoveryFailed(String serviceType,int errorCode) {
Log.e(TAG,"discovery Failed: Error code:" + errorCode);
nsdManager.stopServicediscovery(this);
}
@Override
public void onStopdiscoveryFailed(String serviceType,"discovery Failed: Error code:" + errorCode);
nsdManager.stopServicediscovery(this);
}
};
}
public static void discover(){
System.out.println("discoverServices");
nsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
System.out.println("oui");
nsdManager.discoverServices("_http._tcp.",NsdManager.PROTOCOL_DNS_SD,discoveryListener);
System.out.println("oui");
}
//nsdManager = (NsdManager)c.getSystemService(Context.NSD_SERVICE);
public static String discoverDevices(Context context)throws IOException {
System.out.println("up");
//MusicList scan= new MusicList();
//System.out.println("up");
discover();
System.out.println("up");
return "Abdelhamid";
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)