我的代码有问题,我似乎找不到它。错误是:
应用程序意外停止了!
这是我的代码:
public class MainActivity extends ListActivity {
private static final int REQUEST_BT_ENABLE = 0x1;
ListView listGeraete;
TextView text;
BluetoothAdapter bluetoothAdapter;
ArrayAdapter<String> arrayAdapter;
static private ArrayAdapter<BluetoothDevice> BtDevices = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
text = (TextView) findViewById(R.id.txt);
listGeraete = (ListView) findViewById(R.id.list);
arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1);
listGeraete.setAdapter(arrayAdapter);
registerReceiver(ActionFoundReceiver, new IntentFilter(
BluetoothDevice.ACTION_FOUND));
}
// Aktivierung von Bluetooth
public void ClickBtOn(View view)
{
bluetoothAdapter.enable();
Intent enablerBt = new Intent(bluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enablerBt, REQUEST_BT_ENABLE);
}
// Deaktivierung von Bluetooth
public void ClickBtOff(View view) {
bluetoothAdapter.disable();
}
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(ActionFoundReceiver);
}
public void ClickBtScan(View view) {
if (!bluetoothAdapter.isEnabled()) {
Intent enableBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBt, REQUEST_BT_ENABLE);
}
arrayAdapter.clear();
bluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
arrayAdapter.add(device.getName() + "\n" + device.getAddress());
arrayAdapter.notifyDataSetChanged();
}
}
};
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
if(bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
logcat是:
07-26 15:33:48.105: D/Process(3919): killProcess, pid=3919
07-26 15:33:48.105: D/Process(3919): dalvik.system.VMStack.getThreadStackTrace(Native Method)
07-26 15:33:48.105: D/Process(3919): java.lang.Thread.getStackTrace(Thread.java:599)
07-26 15:33:48.105: D/Process(3919): android.os.Process.killProcess(Process.java:788)
07-26 15:33:48.105: D/Process(3919): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104)
07-26 15:33:48.115: D/Process(3919): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
07-26 15:33:48.115: D/Process(3919): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
07-26 15:33:48.115: D/Process(3919): dalvik.system.NativeStart.main(Native Method)
07-26 15:33:48.115: I/Process(3919): Sending signal. PID: 3919 SIG: 9
07-26 15:34:09.476: D/AndroidRuntime(4062): Shutting down VM
07-26 15:34:09.476: W/dalvikvm(4062): threadid=1: thread exiting with uncaught exception (group=0x40ad6228)
07-26 15:34:09.486: E/AndroidRuntime(4062): FATAL EXCEPTION: main
07-26 15:34:09.486: E/AndroidRuntime(4062): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.upcin.kommunikationuberbt/com.upcin.kommunikationuberbt.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.app.ActivityThread.access$600(ActivityThread.java:139)
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.os.Looper.loop(Looper.java:154)
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.app.ActivityThread.main(ActivityThread.java:4945)
07-26 15:34:09.486: E/AndroidRuntime(4062): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 15:34:09.486: E/AndroidRuntime(4062): at java.lang.reflect.Method.invoke(Method.java:511)
07-26 15:34:09.486: E/AndroidRuntime(4062): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-26 15:34:09.486: E/AndroidRuntime(4062): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-26 15:34:09.486: E/AndroidRuntime(4062): at dalvik.system.NativeStart.main(Native Method)
07-26 15:34:09.486: E/AndroidRuntime(4062): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.app.ListActivity.onContentChanged(ListActivity.java:250)
07-26 15:34:09.486: E/AndroidRuntime(4062): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:281)
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.app.Activity.setContentView(Activity.java:1885)
07-26 15:34:09.486: E/AndroidRuntime(4062): at com.upcin.kommunikationuberbt.MainActivity.onCreate(MainActivity.java:36)
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.app.Activity.performCreate(Activity.java:4531)
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
07-26 15:34:09.486: E/AndroidRuntime(4062): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
07-26 15:34:09.486: E/AndroidRuntime(4062): ... 11 more
答案 0 :(得分:3)
你的日志说明了一切
Your content must have a ListView whose id attribute is 'android.R.id.list'
这意味着您需要使用ListView's
设置android.R.id.list
ID
因此,您的布局ListView
文件中必须有XML
id="@android:id/list"
<ListView
android:id="@android:id/list"
...
/>
因为您从ListActivity
延伸。
你也可以看看