marciokoko
marciokoko

Reputation: 4986

Android app crashes because of Activity layout call

I modified the Android Studio BluetoothLeGatt example project by adding what I thought was an additional view call, but the app crashes when I load it onto the device. This is the actual problem:

Your content must have a ListView whose id attribute is 'android.R.id.list'

The reason its crashing is because I added this line on april 7:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setTitle(R.string.title_devices);
    mHandler = new Handler();
    setContentView(R.layout.activity_main); //added april 7

    // Use this check to determine whether BLE is supported on the device.  Then you can
    // selectively disable BLE-related features.
    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
        finish();
    }

    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Checks if Bluetooth is supported on the device.
    if (mBluetoothAdapter == null) {
        Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
}

I added the activity_main.xml layout file to the original project and I thought I could add it through the MainActivity which in this case is DeviceScanActivity.java. I thought I understood how AndroidManifest.xml calls the MainActivity.java and how that java calls other xml's, but this is a bit more complicated for me because its not a activity_main.xml from the start.

The complete project is here: http://developer.android.com/samples/BluetoothLeGatt/project.html

Here is the complete logcat log:

04-07 18:10:08.925 11603-11603/com.example.android.bluetoothlegatt D/dalvikvm: GetMethodID: not returning static method Landroid/os/Process;.getFreeMemory ()J
04-07 18:10:09.240 11603-11603/com.example.android.bluetoothlegatt D/AndroidRuntime: Shutting down VM
04-07 18:10:09.240 11603-11603/com.example.android.bluetoothlegatt W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x416ceba8)
04-07 18:10:09.252 11603-11603/com.example.android.bluetoothlegatt E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.android.bluetoothlegatt, PID: 11603
                                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.bluetoothlegatt/com.example.android.bluetoothlegatt.DeviceScanActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2189)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
                                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:138)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:136)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5016)
                                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
                                                                                         at dalvik.system.NativeStart.main(Native Method)
                                                                                      Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
                                                                                         at android.app.ListActivity.onContentChanged(ListActivity.java:243)
                                                                                         at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
                                                                                         at android.app.Activity.setContentView(Activity.java:1946)
                                                                                         at com.example.android.bluetoothlegatt.DeviceScanActivity.onCreate(DeviceScanActivity.java:59)
                                                                                         at android.app.Activity.performCreate(Activity.java:5248)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238) 
                                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:138) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at android.os.Looper.loop(Looper.java:136) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5016) 
                                                                                         at java.lang.reflect.Method.invokeNative(Native Method) 

Upvotes: 0

Views: 496

Answers (2)

Harsha Vardhan
Harsha Vardhan

Reputation: 3344

android:id="@android:id/list" should be missing in our activity_main.xml file

 <ListView 
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

Upvotes: 1

Krish
Krish

Reputation: 3885

As per the code DeviceScanActivity is extending ListActivity. And ListActivity has a default layout that consists of a single, full-screen listview in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or android.R.id if it's in code)

Upvotes: 0

Related Questions