我正在创建一个将使用WiFi Aware来发布服务的应用程序,但无法正确调用实例化的WiFiAwareManager对象上的attach方法。当我按下触发方法的按钮时,所有内容都会编译,应用程序崩溃。我在下面添加了我的代码和调试日志。谢谢。
我的代码:
package com.patrickutz.wifiawarepublish;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.aware.AttachCallback;
import android.net.wifi.aware.WifiAwareManager;
import android.net.wifi.aware.WifiAwareSession;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.net.wifi.aware.PublishConfig;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Constant values of WifiAwareManager
String state_change = WifiAwareManager.ACTION_WIFI_AWARE_STATE_CHANGED;
int data_init = WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_INITIATOR;
int data_resp = WifiAwareManager.WIFI_AWARE_DATA_PATH_ROLE_RESPONDER;
public void publish(View view) {
// Check whether or not device supports WiFi Aware
boolean hasWiFiAware = getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_AWARE);
// Toast myToast = Toast.makeText(this, message, duration);
// Messages for whether or not device has WiFi Aware
Toast hasAware = Toast.makeText(this, "WiFi Aware Supported", Toast.LENGTH_SHORT);
Toast noAware = Toast.makeText(this, "WiFi Aware Unsupported", Toast.LENGTH_SHORT);
if (hasWiFiAware) {
hasAware.show();
} else {
noAware.show();
}
System.out.println(hasWiFiAware);
// Create WiFiAwareManager object
WifiAwareManager wifiAwareManager = (WifiAwareManager)getSystemService(Context.WIFI_AWARE_SERVICE);
AttachCallback attachCallback = new AttachCallback();
Handler handler = new Handler();
wifiAwareManager.attach(attachCallback, handler);
// Get the text views
TextView showStateChangeTextView = (TextView) findViewById(R.id.stateChangeTextView);
TextView showDataPathRoleInitTextView = (TextView) findViewById(R.id.dataPathRoleInitTextView);
TextView showDataPathRoleRespTextView = (TextView) findViewById(R.id.dataPathRoleRespTextView);
// Display the new values of current state in the text view.
showStateChangeTextView.setText("State: " + state_change);
showDataPathRoleInitTextView.setText("Data Initiator: " + Integer.toString(data_init));
showDataPathRoleRespTextView.setText("Data Responder: " + Integer.toString(data_resp));
}
}
日志:
06-26 15:18:20.070 7365-7365/com.patrickutz.wifiawarepublish E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.patrickutz.wifiawarepublish, PID: 7365
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.net.wifi.aware.WifiAwareManager.attach(android.net.wifi.aware.AttachCallback, android.os.Handler)' on a null object reference
at com.patrickutz.wifiawarepublish.MainActivity.publish(MainActivity.java:67)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:2)
WiFiAwareManager。因此,这些设备在以下设备中不可用。为了避免在这种情况下出现Null指针异常,请考虑通过以下代码行检查其可用性:
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_AWARE)) {
Log.e("ERROR_TAG", "WiFiAwareManager service is not available.");
return;
}
//if available then initialize.
WifiAwareManager was = (WifiAwareManager)getSystemService(WifiAwareManager.class);
引用:https://developer.android.com/reference/android/net/wifi/aware/WifiAwareManager
答案 1 :(得分:0)
一直以来的问题是我在不支持WiFi Aware的Pixel(运行Android 8.1.0)上运行此代码。当我在Pixel 2 XL(运行Android 8.1.0)上运行代码时,代码工作正常,并且不会导致应用程序崩溃。值得一提的是,isAvailable()方法会使应用程序在不支持的设备上崩溃,而不是返回false。哇! (将会与Google联系)