应用程序在此活动的启动时崩溃,但如果在单击按钮时通过onClick()方法调用设置适配器的代码,则代码运行正常... 如果代码在onCreateview()中使用而不是onCreate(),则静态引用不能用于静态方法findViewById()
public class DeviceList extends ActionBarActivity {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_list);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
ListView listview = (ListView) findViewById(R.id.listdevices);
ArrayList<String> list = new ArrayList<String>();
if(pairedDevices.size()>0) {
for(BluetoothDevice device: pairedDevices) {
list.add(device.getName() + "\n" + device.getAddress());
}
}
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
}
xml文件是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.checkingbluetooth.DeviceList$PlaceholderFragment"
android:background="#C0C0C0"
android:orientation="vertical">
<ListView
android:id="@+id/listdevices"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
日志
07-09 15:08:07.414: W/dalvikvm(22973): threadid=1: thread exiting with uncaught exception (group=0x41e6cae0)
07-09 15:08:07.414: E/AndroidRuntime(22973): FATAL EXCEPTION: main
07-09 15:08:07.414: E/AndroidRuntime(22973): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.checkingbluetooth/com.example.checkingbluetooth.DeviceList}: java.lang.NullPointerException
07-09 15:08:07.414: E/AndroidRuntime(22973): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
07-09 15:08:07.414: E/AndroidRuntime(22973): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
07-09 15:08:07.414: E/AndroidRuntime(22973): at android.app.ActivityThread.access$600(ActivityThread.java:146)
07-09 15:08:07.414: E/AndroidRuntime(22973): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1239)
07-09 15:08:07.414: E/AndroidRuntime(22973): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 15:08:07.414: E/AndroidRuntime(22973): at android.os.Looper.loop(Looper.java:137)
07-09 15:08:07.414: E/AndroidRuntime(22973): at android.app.ActivityThread.main(ActivityThread.java:5085)
07-09 15:08:07.414: E/AndroidRuntime(22973): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 15:08:07.414: E/AndroidRuntime(22973): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 15:08:07.414: E/AndroidRuntime(22973): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-09 15:08:07.414: E/AndroidRuntime(22973): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-09 15:08:07.414: E/AndroidRuntime(22973): at dalvik.system.NativeStart.main(Native Method)
07-09 15:08:07.414: E/AndroidRuntime(22973): Caused by: java.lang.NullPointerException
07-09 15:08:07.414: E/AndroidRuntime(22973): at com.example.checkingbluetooth.DeviceList.onCreate(DeviceList.java:48)
07-09 15:08:07.414: E/AndroidRuntime(22973): at android.app.Activity.performCreate(Activity.java:5104)
07-09 15:08:07.414: E/AndroidRuntime(22973): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-09 15:08:07.414: E/AndroidRuntime(22973): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151)
07-09 15:08:07.414: E/AndroidRuntime(22973): ... 11 more
答案 0 :(得分:0)
我认为您的listview
将为null,因为您的列表视图应位于PlaceholderFragment
类内。请在placeholderfragment
的布局中添加列表视图中做一件事,然后尝试它会起作用,谢谢; - )
答案 1 :(得分:0)
你的位置:
R.id.container
如果您的代码超过了第一个条件,它就会崩溃。如果你不使用它,请将其删除。
答案 2 :(得分:0)
您将获得NullPointerException
引起:java.lang.NullPointerException at com.example.checkingbluetooth.DeviceList.onCreate(DeviceList.java:48)
我在DeviceList.java:48
的猜测是
ListView listview = (ListView) findViewById(R.id.listdevices);
原因listdevices
ListView
位于您的片段布局中,这就是为什么 null 将ListView
放在activity_device_list
中。我认为这将解决您的问题