2013-07-14 124 views
1

我正在學習android,並試圖從android的Bluetooth guide實現代碼,但它給了我很多麻煩,因爲「tutorial」不是很清楚。稍後,我確實發現了一個例子:MainActivity擴展ListActivity崩潰應用程序?

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(); 
    } 
} 

當我試圖在真實設備和仿真器上運行它時,這個程序崩潰了。我還編寫了自己的代碼,結合了我所理解的所有內容,如下所示:

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); 
     } 
    } 
} 

這也是崩潰。

經過幾分鐘的嚴格測試後,我得出一個結論:如果我嘗試使用ListActivity擴展MainActivity,應用程序將在啓動時崩潰。同時,如果我使用Activity擴展MainActivity,它將毫無問題地啓動。但在這種情況下,我不能使用this.setListAdapter(mArrayAdapter);,我看到的唯一解決方案是創建另一個活動,它將在啓動時從MainActivity調用並執行此藍牙代碼。所以,我的問題是:1)僅僅因爲MainActivity擴展了ListActivity和2)除了我提到的解決方案之外,還有其他可行的解決方案嗎?

謝謝。

的logcat:

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 
+1

張貼堆棧跟蹤,如果你有一個崩潰。 – Karakuri

+1

最有可能你沒有在你的xml佈局中有正確的id的listview,但我們需要看到跟蹤。 –

+0

我猜你在談論LogCat?如果沒有,請給我一個小費怎麼辦。謝謝你,Gabe Sechan, – Anton

回答

2

從您的堆棧跟蹤:

您的內容必須有一個ListView其id屬性爲 'android.R.id.list'

那意味着在佈局xml文件中,您必須有一個ListView,其ID設置爲「android.R.id.list」。

瞭解更多關於ListActivity here.瞭解如何使用它是強制性的。

希望這有助於...

0

轉到您的主XML佈局和ListView控件的ID從logcat的設置@android:id/list