添加項目到我的陣列適配器時出現問題。ArrayAdapter添加方法投射非法狀態異常
下面是代碼
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$1.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$1.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$1.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
任何想法? 感謝
您可以發佈您的堆棧跟蹤與錯誤? – AndroidEx
我已編輯帖子!我想我沒有實現onClick方法等等,是嗎? – Benz
請同時添加一些關於您的適配器的信息。但是,由於輸出已經聲明您在第95行有一個NullPointerException(在com.voice.benz.instaremote.MainActivity.find_BondedDevices(MainActivity.java:95))。只要檢查第95行。那裏會發生什麼?哪些var可以爲null? – mikepenz