Android MainActivity扩展ListActivity崩溃应用程序?

Android MainActivity扩展ListActivity崩溃应用程序?,android,crash,listactivity,listadapter,Android,Crash,Listactivity,Listadapter,我正在学习android,并试图实现android的代码,但这给了我很多麻烦,因为“教程”不是很清楚。过了一会儿,我发现了一个例子: package edu.ius.rwisman.AndroidRemoteDiscovery; import android.app.ListActivity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.c

我正在学习android,并试图实现android的代码,但这给了我很多麻烦,因为“教程”不是很清楚。过了一会儿,我发现了一个例子:

package edu.ius.rwisman.AndroidRemoteDiscovery;

import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import java.util.Set;

public class AndroidRemoteDiscoveryActivity extends ListActivity {
    private static final int REQUEST_ENABLE_BT = 2;

    private ArrayAdapter<String> mArrayAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        this.setListAdapter(mArrayAdapter); 

        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) mArrayAdapter.add("Device does not support Bluetooth");

        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            mArrayAdapter.add("Paired devices");            
            for (BluetoothDevice device : pairedDevices)                // Loop through paired devices
                // Add the name and address to an array adapter to show in a ListView
                mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
        }

        mArrayAdapter.add("Discovered devices");

        final BroadcastReceiver mReceiver = new BroadcastReceiver() {   // Create a BroadcastReceiver for ACTION_FOUND
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                if (BluetoothDevice.ACTION_FOUND.equals(action)) {      // When discovery finds a device
                    // Get the BluetoothDevice object from the Intent
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    // Add the name and address to an array adapter to show in a ListView
                    mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                }
            }
        };
        // Register the BroadcastReceiver
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy  

        mBluetoothAdapter.startDiscovery();
    }
}
从堆栈跟踪中:

您的内容必须具有id属性为“android.R.id.list”的ListView

这意味着在布局xml文件中,必须有一个id设置为“android.R.id.list”的ListView

阅读有关ListActivity的更多信息了解如何使用它是必需的


希望这有助于…

转到您的主XML布局,并将ListView的id设置为
@android:id/list

在发生崩溃时从logcat发布堆栈跟踪。很可能您的XML布局中没有具有正确id的ListView,但我们需要查看跟踪。我猜您是在谈论logcat?如果没有,请告诉我该怎么做。加布·塞尚,谢谢。这是我的问题。
package com.example.cellbotproxy;

import java.util.Set;
import android.app.Activity;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {
    private TextView stateMessage;
    private BroadcastReceiver mReceiver;
    private BluetoothAdapter mBluetoothAdapter;
    private ArrayAdapter<String> mArrayAdapter;
    private boolean receiverRegisted = false;
    private final static int REQUEST_ENABLE_BT = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stateMessage = (TextView)findViewById(R.id.state_message);
        stateMessage.setText(R.string.idle_message);

        mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        this.setListAdapter(mArrayAdapter);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if (mBluetoothAdapter == null) {
            stateMessage.setText(R.string.btnotsupported_message);
        }
        else{
            if(!mBluetoothAdapter.isEnabled()) {
                stateMessage.setText(R.string.btoff_message);
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
        }
    }

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_ENABLE_BT){
            if(resultCode == RESULT_CANCELED){
                stateMessage.setText(R.string.requestcanceled_message);
            }
            else if (resultCode == RESULT_OK){
                stateMessage.setText(R.string.bton_message);

                Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
                if (pairedDevices.size() > 0) {
                    for (BluetoothDevice device : pairedDevices) {
                        // Add the name and address to an array adapter to show in a ListView
                        mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                    }
                }

                mReceiver = new BroadcastReceiver(){ // Create a BroadcastReceiver for ACTION_FOUND
                    public void onReceive(Context context, Intent intent){
                        String action = intent.getAction();

                        if (BluetoothDevice.ACTION_FOUND.equals(action)){ // When discovery finds a device
                            // Get the BluetoothDevice object from the Intent
                            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                            // Add the name and address to an array adapter to show in a ListView
                            mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
                        }
                    }
                };

                // Register the BroadcastReceiver
                IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy  
                receiverRegisted = true;
                mBluetoothAdapter.startDiscovery();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if(receiverRegisted){
            unregisterReceiver(mReceiver);
        }
    }
}
07-14 01:38:47.957: E/Trace(2709): error opening trace file: No such file or directory (2)
07-14 01:38:48.899: D/dalvikvm(2709): GC_FOR_ALLOC freed 163K, 11% free 2531K/2824K, paused 60ms, total 67ms
07-14 01:38:48.906: I/dalvikvm-heap(2709): Grow heap (frag case) to 3.202MB for 635812-byte allocation
07-14 01:38:49.076: D/dalvikvm(2709): GC_FOR_ALLOC freed 2K, 9% free 3149K/3448K, paused 162ms, total 162ms
07-14 01:38:49.166: D/dalvikvm(2709): GC_CONCURRENT freed <1K, 9% free 3149K/3448K, paused 32ms+9ms, total 95ms
07-14 01:38:49.166: D/dalvikvm(2709): WAIT_FOR_CONCURRENT_GC blocked 35ms
07-14 01:38:49.187: I/dalvikvm-heap(2709): Grow heap (frag case) to 3.676MB for 500416-byte allocation
07-14 01:38:49.336: D/dalvikvm(2709): GC_FOR_ALLOC freed <1K, 8% free 3638K/3940K, paused 141ms, total 141ms
07-14 01:38:49.456: D/AndroidRuntime(2709): Shutting down VM
07-14 01:38:49.456: W/dalvikvm(2709): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-14 01:38:49.476: E/AndroidRuntime(2709): FATAL EXCEPTION: main
07-14 01:38:49.476: E/AndroidRuntime(2709): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cellbotproxy/com.example.cellbotproxy.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.os.Looper.loop(Looper.java:137)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-14 01:38:49.476: E/AndroidRuntime(2709): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 01:38:49.476: E/AndroidRuntime(2709): at java.lang.reflect.Method.invoke(Method.java:511)
07-14 01:38:49.476: E/AndroidRuntime(2709): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-14 01:38:49.476: E/AndroidRuntime(2709): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-14 01:38:49.476: E/AndroidRuntime(2709): at dalvik.system.NativeStart.main(Native Method)
07-14 01:38:49.476: E/AndroidRuntime(2709): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
07-14 01:38:49.476: E/AndroidRuntime(2709): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.Activity.setContentView(Activity.java:1881)
07-14 01:38:49.476: E/AndroidRuntime(2709): at com.example.cellbotproxy.MainActivity.onCreate(MainActivity.java:27)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.Activity.performCreate(Activity.java:5104)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-14 01:38:49.476: E/AndroidRuntime(2709): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-14 01:38:49.476: E/AndroidRuntime(2709): ... 11 more
07-14 01:38:53.066: I/Process(2709): Sending signal. PID: 2709 SIG: 9