Android Studio - setAdapter() - 失败

时间:2016-04-19 15:58:20

标签: java android

我尝试使用setAdapter()制作蓝牙应用并遇到麻烦。

private BluetoothAdapter BA;
ListView listB;
private Set<BluetoothDevice> pairedDevices;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    BA = BluetoothAdapter.getDefaultAdapter();
    listB = (ListView)findViewById(R.id.listBlu);
    if (!BA.isEnabled()) {
        Intent blu_turn_on = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(blu_turn_on, 0);
        Toast.makeText(getApplicationContext(),"Bluetooth turn on",Toast.LENGTH_SHORT).show();
        Set<BluetoothDevice> pariedDevices;
        pariedDevices = BA.getBondedDevices();
    }
    else
    {
        Toast.makeText(getApplicationContext(),"Bluetooth already on", Toast.LENGTH_SHORT).show();
    }
    Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(getVisible, 0);
    pairedDevices = BA.getBondedDevices();
    ArrayList list = new ArrayList();

    for(BluetoothDevice bt : pairedDevices)
        list.add(bt.getName());
    Toast.makeText(getApplicationContext(),"Showing Paired Devices",Toast.LENGTH_SHORT).show();

    final ListAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
    listB.setAdapter(adapter);
}

04-20 21:52:15.761 17597-17597/com.example.werewolf.deltacontroler W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 04-20 21:52:16.074 17597-17765/com.example.werewolf.deltacontroler D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 04-20 21:52:16.085 17597-17597/com.example.werewolf.deltacontroler D/Atlas: Validating map... 04-20 21:52:16.201 17597-17765/com.example.werewolf.deltacontroler I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.02.006.020_msm8960_LA.AF.1.1_RB1__release_AU () OpenGL ES Shader Compiler Version: E031.25.03.06 Build Date: 05/22/15 Fri Local Branch: mybranch10237523 Remote Branch: quic/LA.AF.1.1_rb1.18 Local Patches: NONE Reconstruct Branch: AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.02.006.020 + 9b2699f + 2215637 + 60aa592 + f2362e6 + 5c64f59 + 82411a1 + 89a0db6 + 6151be1 + NOTHING 04-20 21:52:16.210 17597-17765/com.example.werewolf.deltacontroler I/OpenGLRenderer: Initialized EGL, version 1.4 04-20 21:52:16.260 17597-17765/com.example.werewolf.deltacontroler D/OpenGLRenderer: Enabling debug mode 0 04-20 21:52:16.535 17597-17597/com.example.werewolf.deltacontroler I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@31ae5690 time:79315035 04-20 21:52:42.340 17597-17597/com.example.werewolf.deltacontroler I/Timeline: Timeline: Activity_launch_request id:com.example.werewolf.deltacontroler time:79340840 04-20 21:52:42.495 17597-17597/com.example.werewolf.deltacontroler D/BluetoothAdapter: 850763313: getState() : mService = null. Returning STATE_OFF 04-20 21:52:42.501 17597-17597/com.example.werewolf.deltacontroler D/BluetoothAdapter: 850763313: getState() : mService = null. Returning STATE_OFF 04-20 21:52:42.507 17597-17597/com.example.werewolf.deltacontroler D/AndroidRuntime: Shutting down VM 04-20 21:52:42.514 17597-17597/com.example.werewolf.deltacontroler E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.werewolf.deltacontroler, PID: 17597 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.werewolf.deltacontroler/com.example.werewolf.deltacontroler.Configuration}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) at android.app.ActivityThread.access$800(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5389) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.werewolf.deltacontroler.Configuration.onCreate(Configuration.java:51) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)  at android.app.ActivityThread.access$800(ActivityThread.java:156)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:211)  at android.app.ActivityThread.main(ActivityThread.java:5389)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)  04-20 21:52:49.245 18952-18952/com.example.werewolf.deltacontroler I/Process: Sending signal. PID: 18952 SIG: 9 04-20 21:52:49.512 19066-19066/com.example.werewolf.deltacontroler W/ResourceType: Found multiple library tables, ignoring... 04-20 21:52:49.522 19066-19066/com.example.werewolf.deltacontroler W/ResourceType: Found multiple library tables, ignoring... 04-20 21:52:49.859 19066-19066/com.example.werewolf.deltacontroler W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 04-20 21:52:50.172 19066-19066/com.example.werewolf.deltacontroler D/BluetoothAdapter: 132902748: getState() : mService = null. Returning STATE_OFF 04-20 21:52:50.194 19066-19066/com.example.werewolf.deltacontroler D/BluetoothAdapter: 132902748: getState() : mService = null. Returning STATE_OFF 04-20 21:52:50.206 19066-19066/com.example.werewolf.deltacontroler D/AndroidRuntime: Shutting down VM 04-20 21:52:50.208 19066-19066/com.example.werewolf.deltacontroler E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.werewolf.deltacontroler, PID: 19066 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.werewolf.deltacontroler/com.example.werewolf.deltacontroler.Configuration}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) at android.app.ActivityThread.access$800(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:211) at android.app.ActivityThread.main(ActivityThread.java:5389) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.werewolf.deltacontroler.Configuration.onCreate(Configuration.java:51) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)  at android.app.ActivityThread.access$800(ActivityThread.java:156)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:211)  at android.app.ActivityThread.main(ActivityThread.java:5389)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)  04-20 21:52:51.591 19066-19066/com.example.werewolf.deltacontroler I/Process: Sending signal. PID: 19066 SIG: 9

应用程序运行正常,但只要输入此部分代码就会崩溃。
问题出在setAdapter()的最后一行,因为如果我从代码应用程序中删除它,则通常会在结果空窗口中输入。
我试图寻找解决方案,但似乎每个人都这样做,我无法发现我的错误。

0 个答案:

没有答案