ArrayAdapter 添加方法转换非法状态异常
ArrayAdapter add method casting Illegal state exception
我在将项目添加到阵列适配器时遇到问题。
这是代码
public void find_BondedDevices (View view) {
// Look for bonded devices
pairedDevices = btAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String deviceBTName = device.getName();
String deviceBTMajorClass = getBTMajorDeviceClass(device
.getBluetoothClass()
.getMajorDeviceClass());
pairedDevicesAdapter.add(deviceBTName + "\n" + deviceBTMajorClass);
}
}
如果将代码放在onCreate方法中,就可以了!
如果我评论这行
pairedDevicesAdapter.add(deviceBTName + "\n" + deviceBTMajorClass);
有效!
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View.onClick(View.java:3638)
at android.view.View.performClick(View.java:4243)
at android.view.View$PerformClick.run(View.java:17520)
android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View.onClick(View.java:3633)
at android.view.View.performClick(View.java:4243)
at android.view.View$PerformClick.run(View.java:17520)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.voice.benz.instaremote.MainActivity.find_BondedDevices(MainActivity.java:95)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View.onClick(View.java:3633)
at android.view.View.performClick(View.java:4243)
at android.view.View$PerformClick.run(View.java:17520)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
我已经尝试了几乎所有方法但没有结果,我需要将此代码放在一个单独的函数中,因为在 onCreate 方法中没有用
这是应用程序的完整代码:
http://pastebin.com/nZYdSkaA
有什么想法吗?
谢谢
问题的主要原因是
Caused by: java.lang.NullPointerException
at com.voice.benz.instaremote.MainActivity.find_BondedDevices(MainActivity.java:95)
而且只有当你的 pairedDevicesAdapter
为 null(即 null)时,它才可以,检查你是否正确初始化它
您正在将适配器分配给 局部变量 pairedDevicesAdapter 而不是 实例变量.
它是 onCreate() 方法的局部内容,请参阅:
ArrayAdapter<String> pairedDevicesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
因此,您用来调用方法 'add' 的 pairedDevicesAdapter 为空(从未初始化)。
修复:
在 onCreate() 上改为这样做:
this.pairedDevicesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
我在将项目添加到阵列适配器时遇到问题。
这是代码
public void find_BondedDevices (View view) {
// Look for bonded devices
pairedDevices = btAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String deviceBTName = device.getName();
String deviceBTMajorClass = getBTMajorDeviceClass(device
.getBluetoothClass()
.getMajorDeviceClass());
pairedDevicesAdapter.add(deviceBTName + "\n" + deviceBTMajorClass);
}
}
如果将代码放在onCreate方法中,就可以了!
如果我评论这行
pairedDevicesAdapter.add(deviceBTName + "\n" + deviceBTMajorClass);
有效!
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View.onClick(View.java:3638)
at android.view.View.performClick(View.java:4243)
at android.view.View$PerformClick.run(View.java:17520)
android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View.onClick(View.java:3633)
at android.view.View.performClick(View.java:4243)
at android.view.View$PerformClick.run(View.java:17520)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.voice.benz.instaremote.MainActivity.find_BondedDevices(MainActivity.java:95)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View.onClick(View.java:3633)
at android.view.View.performClick(View.java:4243)
at android.view.View$PerformClick.run(View.java:17520)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
我已经尝试了几乎所有方法但没有结果,我需要将此代码放在一个单独的函数中,因为在 onCreate 方法中没有用
这是应用程序的完整代码: http://pastebin.com/nZYdSkaA
有什么想法吗? 谢谢
问题的主要原因是
Caused by: java.lang.NullPointerException
at com.voice.benz.instaremote.MainActivity.find_BondedDevices(MainActivity.java:95)
而且只有当你的 pairedDevicesAdapter
为 null(即 null)时,它才可以,检查你是否正确初始化它
您正在将适配器分配给 局部变量 pairedDevicesAdapter 而不是 实例变量.
它是 onCreate() 方法的局部内容,请参阅:
ArrayAdapter<String> pairedDevicesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
因此,您用来调用方法 'add' 的 pairedDevicesAdapter 为空(从未初始化)。
修复:
在 onCreate() 上改为这样做:
this.pairedDevicesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);