0

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
Kaushik
  • 6,150
  • 5
  • 39
  • 54

3 Answers3

0

i think your listview will be null because your listview should be inside of PlaceholderFragment class. please do one thing in add list view inside layout of placeholderfragment and and then try it will work,thanks;-)

bhavesh kaila
  • 761
  • 1
  • 5
  • 25
  • if i plcae the code inside the placeholderfragment then findviewbyid cant be referenced from inside a static class since it is a non static method – user3819446 Jul 09 '14 at 09:43
  • i know that placeholder is static class but you have to cut your code from line no **12** to **23** from devicelist class and copy it inside placeholder and add list view inside layout of your placeholderframent then it will be work. – bhavesh kaila Jul 09 '14 at 09:59
0

Where is your :

R.id.container

If your code passes by the first condition, it will crash. Remove it if you don't use it.

Tsunaze
  • 3,204
  • 7
  • 44
  • 81
0

You are getting NullPointerException

Caused by: java.lang.NullPointerException at com.example.checkingbluetooth.DeviceList.onCreate(DeviceList.java:48)

My guess in DeviceList.java:48 is

ListView listview = (ListView) findViewById(R.id.listdevices);

Reason listdevices ListView is in your fragment's layout thats why it is null put that ListView in activity_device_list. I think that will solve your problem

Kaushik
  • 6,150
  • 5
  • 39
  • 54