application crashes on the launch of this activity but the code runs fine if the code to set the adapter is called through the onClick() method upon clicking a button ... If the code is used in onCreateview() than onCreate() then static reference cant be made to no static method 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);
}
the xml file is
<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>
the log is
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