Android应用因活动布局调用而崩溃

时间:2016-04-08 00:19:44

标签: android android-activity

我通过添加我认为是额外的视图调用来修改Android Studio BluetoothLeGatt示例项目,但是当我将其加载到设备上时应用程序崩溃了。这是实际问题:

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

它崩溃的原因是因为我在4月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;
    }
}

我将activity_main.xml布局文件添加到原始项目中,我想我可以通过MainActivity添加它,在这种情况下是DeviceScanActivity.java。我以为我理解AndroidManifest.xml如何调用MainActivity.java以及java如何调用其他xml,但这对我来说有点复杂,因为它从一开始就不是activity_main.xml。

完整的项目在这里: http://developer.android.com/samples/BluetoothLeGatt/project.html

这是完整的logcat日志:

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) 

2 个答案:

答案 0 :(得分:1)

我们的activity_main.xml文件中应该缺少

android:id =“@ android:id / list”


insert into x select * from raw

答案 1 :(得分:0)

根据代码,DeviceScanActivity正在扩展ListActivity。 ListActivity的默认布局包含屏幕中央的单个全屏列表视图。但是,如果需要,可以通过在onCreate()中使用setContentView()设置自己的视图布局来自定义屏幕布局。为此,您自己的视图必须包含一个ID为“@android:id / list”的ListView对象(如果它在代码中则为android.R.id)

相关问题