2016-04-08 7 views
0

AndroidビューのBluetoothLeGattのサンプルプロジェクトを、追加のビューコールと考えて追加しましたが、デバイスにロードするとアプリケーションがクラッシュします。これは実際の問題である:アクティビティレイアウトの呼び出しのためにAndroidアプリがクラッシュする

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レイアウトファイルを追加したと私は思った私は、この場合はDeviceScanActivity.javaであるMainActivityを通じて追加できます。私はAndroidManifest.xmlがMainActivity.javaをどのように呼び出すのか、そしてそのJavaが他のXMLをどのように呼び出すのかを理解していると思っていましたが、これは最初からactivity_main.xmlではないので少し複雑です。

完全なプロジェクトはここにある:ここで http://developer.android.com/samples/BluetoothLeGatt/project.html

は、完全なlogcatのログです:コードDeviceScanActivityを1として

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)  

答えて

0

ListActivityを延長しています。また、ListActivityにはデフォルトのレイアウトがあり、画面の中央に1つのフルスクリーンのリストビューで構成されています。ただし、必要に応じて、onCreate()のsetContentView()で独自のビューレイアウトを設定して、画面レイアウトをカスタマイズできます。これを行うには、自分のビューにID "@android:id/list"(コード内にあればandroid.R.id)のListViewオブジェクトが含まれている必要があります。

+0

私の不注意なことに、ListActivityが拡張されています。アクティビティにはデフォルトのレイアウト、フルスクリーン、フロント、センターがあることを理解しています。私は新しいビューactivity_main.xmlを作成しましたが、別のアクティビティから呼び出す必要がありますか? – marciokoko

+0

実際にListActivityは、listview(ex:setAdapter()メソッド)を扱ういくつかのメソッドを公開しています。 ListActivityを使用しない場合は、リストビューを管理するために独自のActivityクラスにコードを記述する必要があります。 ListActivity Use ActivityクラスまたはFragmentActivity – Krish

+0

が必要ない場合は、そのままListActivityを使用します。しかし、すでに接続していると、新しいビュー/アクティビティを呼び出すようにしたいと考えています。 – marciokoko

1

android:id = "@ android:id/list "がactivity_main.xmlファイルに存在しないようにしてください。

<ListView 
     android:id="@android:id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
関連する問題