2012-03-03 22 views
0

LogCatメッセージでNullPointerExceptionエラーが発生しましたが、問題の検出に問題があります。私のコードとlogcatメッセージが怒鳴る接続されているAndroid App Bluetooth Logcat NullPointerException

// Get a set of currently paired devices 
Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices(); 

package android.mgo.helloandroid; 

import java.util.Set; 

import android.app.Activity; 
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.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

public class BTDdetecetwithV7Activity extends Activity { 
    /** Called when the activity is first created. */ 
    // Debugging 
    private static final String Tag="Messages"; 
    private static final boolean D = true; 

    // Return Intent extra 
    // public static String EXTRA_DEVICE_ADDRESS = "device_address"; 

    // Member fields 
    private BluetoothAdapter mBtAdapter; 
    private ArrayAdapter<String> mPairedDevicesArrayAdapter; 
    private ArrayAdapter<String> mNewDevicesArrayAdapter; 




    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (D) Log.d(Tag, "in onCreate()"); 
     if (D) Log.e(Tag, "onCreate()"); 
     if (D) Log.i(Tag, "onCreate()"); 
     // Setup the window 


     requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
     setContentView(R.layout.main); 

     // Set result CANCELED in case the user backs out 
     setResult(Activity.RESULT_CANCELED); 
     if (D) Log.i(Tag, "in setResult()"); 
     // Initialize the button to perform device discovery 
     Button scanButton = (Button) findViewById(R.id.button_scan); 
     scanButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       doDiscovery(); 
       v.setVisibility(View.GONE); 
      } 
     }); 

     // Initialize array adapters. One for already paired devices and 
     // one for newly discovered devices 
     mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.main); 
     mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.main); 


     // Get the local Bluetooth adapter 
     mBtAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (D) Log.e(Tag, "On line 87"); 


     // Find and set up the ListView for paired devices 
     ListView pairedListView = (ListView) findViewById(R.id.paired_devices); 
     pairedListView.setAdapter(mPairedDevicesArrayAdapter); 

     // Find and set up the ListView for newly discovered devices 
     ListView newDevicesListView = (ListView) findViewById(R.id.new_devices); 
     newDevicesListView.setAdapter(mNewDevicesArrayAdapter); 


     // Register for broadcasts when a device is discovered 
     IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
     this.registerReceiver(mReceiver, filter); 


     // Register for broadcasts when discovery has finished 
     filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
     this.registerReceiver(mReceiver, filter); 

     // Get a set of currently paired devices 
     Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices(); 


     // If there are paired devices, add each one to the ArrayAdapter 
     if (pairedDevices.size() > 0) { 
      findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); 
      if (D) Log.d(Tag, "in the doDiscovery()"); 

      for (BluetoothDevice device : pairedDevices) { 
       mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
      } 

     } else { 
      String noDevices = getResources().getText(R.string.none_paired).toString(); 
      mPairedDevicesArrayAdapter.add(noDevices); 
     } 
    } 

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


     // Make sure we're not doing discovery anymore 
     if (mBtAdapter != null) { 
      mBtAdapter.cancelDiscovery(); 
     } 


     // Unregister broadcast listeners 
     this.unregisterReceiver(mReceiver); 
    } 

    /** 
    * Start device discover with the BluetoothAdapter 
    */ 
    private void doDiscovery() { 
     //if (D) Log.d(TAG, "doDiscovery()"); 
     //if (D) Log.e(TAG, "doDiscovery()"); 

     // Indicate scanning in the title 
     setProgressBarIndeterminateVisibility(true); 
     setTitle(R.string.scanning); 


     // Turn on sub-title for new devices 
     findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE); 

     // If we're already discovering, stop it 
     if (mBtAdapter.isDiscovering()) { 
      mBtAdapter.cancelDiscovery(); 
     } 



     // Request discover from BluetoothAdapter 
     mBtAdapter.startDiscovery(); 

    } 
     // The BroadcastReceiver that listens for discovered devices and 
     // changes the title when discovery is finished 
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       String action = intent.getAction(); 



      // When discovery finds a device 
      if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
       // Get the BluetoothDevice object from the Intent 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       // If it's already paired, skip it, because it's been listed already 
       if (device.getBondState() != BluetoothDevice.BOND_BONDED) { 
        mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); 
       } 

      } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
       setProgressBarIndeterminateVisibility(false); 
       setTitle(R.string.select_device); 
       if (mNewDevicesArrayAdapter.getCount() == 0) { 
        String noDevices = getResources().getText(R.string.none_found).toString(); 
        mNewDevicesArrayAdapter.add(noDevices); 
       } 
      } 
     } 
    }; 

} 


**the main.xml:** 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 




    <TextView android:id="@+id/title_paired_devices" 

     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="@string/title_paired_devices" 
     android:visibility="gone" 
     android:background="#666" 
     android:textColor="#fff" 
     android:paddingLeft="5dp"/> 


    <ListView android:id="@+id/paired_devices" 

     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:stackFromBottom="true" 
     android:layout_weight="1" /> 

<TextView android:id="@+id/title_new_devices" 

     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="@string/title_other_devices" 
     android:visibility="gone" 
     android:background="#666" 
     android:textColor="#fff" 
     android:paddingLeft="5dp" /> 

<ListView android:id="@+id/new_devices" 

     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:stackFromBottom="true" 
     android:layout_weight="2" /> 



    <Button android:id="@+id/button_scan" 

     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="@string/button_scan" 
    /> 


</LinearLayout> 

Manifest file: 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="android.mgo.helloandroid" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="7" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:label="@string/app_name" 
      android:name=".BTDdetecetwithV7Activity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 

       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 
     </activity> 
    </application> 
      <uses-permission android:name="android.permission.BLUETOOTH"/> 
      <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
</manifest> 

- **And the logcat is:** 


03-03 18:41:51.616: D/Messages(209): in onCreate() 
03-03 18:41:51.637: E/Messages(209): onCreate() 
03-03 18:41:51.637: I/Messages(209): onCreate() 
03-03 18:41:51.897: I/Messages(209): in setResult() 
03-03 18:41:51.976: E/Messages(209): On line 87 
03-03 18:41:52.026: D/AndroidRuntime(209): Shutting down VM 
03-03 18:41:52.057: W/dalvikvm(209): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
03-03 18:41:52.136: E/AndroidRuntime(209): Uncaught handler: thread main exiting due to uncaught exception 
03-03 18:41:52.166: E/AndroidRuntime(209): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.mgo.helloandroid/android.mgo.helloandroid.BTDdetecetwithV7Activity}: java.lang.NullPointerException 
03-03 18:41:52.166: E/AndroidRuntime(209): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
03-03 18:41:52.166: E/AndroidRuntime(209): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
03-03 18:41:52.166: E/AndroidRuntime(209): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
03-03 18:41:52.166: E/AndroidRuntime(209): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
03-03 18:41:52.166: E/AndroidRuntime(209): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-03 18:41:52.166: E/AndroidRuntime(209): at android.os.Looper.loop(Looper.java:123) 
03-03 18:41:52.166: E/AndroidRuntime(209): at android.app.ActivityThread.main(ActivityThread.java:4363) 
03-03 18:41:52.166: E/AndroidRuntime(209): at java.lang.reflect.Method.invokeNative(Native Method) 
03-03 18:41:52.166: E/AndroidRuntime(209): at java.lang.reflect.Method.invoke(Method.java:521) 
03-03 18:41:52.166: E/AndroidRuntime(209): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
03-03 18:41:52.166: E/AndroidRuntime(209): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
03-03 18:41:52.166: E/AndroidRuntime(209): at dalvik.system.NativeStart.main(Native Method) 
03-03 18:41:52.166: E/AndroidRuntime(209): Caused by: java.lang.NullPointerException 
03-03 18:41:52.166: E/AndroidRuntime(209): at android.mgo.helloandroid.BTDdetecetwithV7Activity.onCreate(BTDdetecetwithV7Activity.java:93) 
03-03 18:41:52.166: E/AndroidRuntime(209): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-03 18:41:52.166: E/AndroidRuntime(209): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
03-03 18:41:52.166: E/AndroidRuntime(209): ... 11 more 
03-03 18:41:52.226: I/dalvikvm(209): threadid=7: reacting to signal 3 
03-03 18:41:52.246: I/dalvikvm(209): Wrote stack trace to '/data/anr/traces.txt' 
03-03 18:46:52.267: I/Process(209): Sending signal. PID: 209 SIG: 9 

いかなる推測何のよう

はLogCatメッセージによると、問題はライン93から開始しました間違っていると感謝されます。

+0

明らかに、あなたのmBtAdapterは 'null'です。エミュレータでこれをテストしていますか? – nhaarman

+0

はい、私は、Windows 7の64bitで... –

+0

と私はアンドロイド2.1と2.2(エミュレータ上)で試して、それは同じ行を指している... –

答えて

0

実際のデバイスでアプリケーションをテストする必要があります。エミュレータはBluetoothをサポートしていません。

+0

私は知っているが、それらのすべてではなく、Linuxのエミュレータ2.4だから、Linux 2.4とLinuxの両方でOracle VirtualBoxで試してみたのですが、HTC Heroでも試しましたが、「予期しないエラーが発生しました...」というメッセージが表示されてクラッシュします。 –

+0

その他の提案は非常に高く評価されます... –