2013-10-24 107 views
0

由於某種原因,這個新的藍牙代碼導致我的應用程序「停止工作」並崩潰。當我遇到這樣的錯誤時,我懷疑它是Android Manifest。但是,我找不到任何問題。請將您的注意力集中在位於啓動功能中的藍牙代碼。沒有它的代碼不會崩潰應用程序。我的格式也很不好。藍牙致命異常主要android

主要Activity.java

package com.example.real; 



import java.util.Set; 
import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.speech.tts.TextToSpeech; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 


public class MainActivity extends Activity implements LocationListener{ 
    Button start, stop; 
    TextView tv; 
    TextView tv2; 
    TextView sum; 
    LocationManager lm; 
    TextToSpeech Talker; 
    Intent phone = new Intent(Intent.ACTION_CALL); 
    String Hello = ("Hello World"); 
    int i,sum1; 
    BluetoothAdapter mBluetoothAdapter; 
    BluetoothDevice mmDevice; 
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //Initilize Test Function Hello World 
     Button button = (Button) this.findViewById(R.id.Button01); 
     button.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
      Toast.makeText(MainActivity.this, "GPS in Progress", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     //Initialize Counter Sequence 
     start = (Button)findViewById(R.id.start);// Launch Seizure Sequence 
     stop = (Button)findViewById(R.id.stop);//Override Function 
     tv = (TextView)findViewById(R.id.tv); 
     tv.setText("30"); // starting from 30. 
     sum = (TextView)findViewById(R.id.sum); 

     final MyCounter timer = new MyCounter(30000,1000); 
     start.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
       if(pairedDevices.size() > 0) 
       { 
       for(BluetoothDevice device : pairedDevices) 
       { 
        if(device.getName().equals("uIceblue2")) 
        { 
         mmDevice = device; 
         for(i=0;i<100;i++) 
          { 
           sum1 = sum1 + i; 
           sum.setText("Sum:"+sum1); 
           if(sum1 > 120) 
            { 
            timer.start(); 
            } 
          } 
        } 
       } 
       } 
      } 
     }); 
     stop.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       timer.cancel(); 
       tv.setText("Emergency Call to first responders prevented"); 
      } 
     }); 
     //Initialize GPS layout 
     tv2 = (TextView) findViewById(R.id.GPS); 
     lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20, 1, this); 
    } 
     public class MyCounter extends CountDownTimer{ 

      public MyCounter(long millisInFuture, long countDownInterval) { 
       super(millisInFuture, countDownInterval); 
      } 

      @Override 
      public void onFinish() { 
       System.out.println("Timer Completed."); 
       tv.setText("Sending information to first responders."); 


       phone.setData(Uri.parse("tel:7034004407")); 
       startActivity(phone); 

      } 

      @Override 
      public void onTick(long millisUntilFinished) { 
       tv.setText((millisUntilFinished/1000)+""); 
       System.out.println("Timer : " + (millisUntilFinished/1000)); 
      } 
     } 

     @Override 
     public void onLocationChanged(Location arg0) 
     { 
      String lat = String.valueOf(arg0.getLatitude()); 
      String lon = String.valueOf(arg0.getLongitude()); 
      Log.e("GPS", "location changed: lat="+lat+", lon="+lon); 
      tv2.setText("lat="+lat+", long="+lon); 
     } 
     public void onProviderDisabled(String arg0) { 
      Log.e("GPS", "provider disabled " + arg0); 
     } 
     public void onProviderEnabled(String arg0) { 
      Log.e("GPS", "provider enabled " + arg0); 
     } 
     public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
      Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]"); 
     } 
} 

清單

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.real.MainActivity" 
      android:label="@string/app_name" 
      > 
      <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.ACCESS_COARSE_LOCATION" /> 
<uses-permission 
android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission 
android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 
<uses-permission 
android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
<uses-permission 
android:name="android.permission.BLUETOOTH" /> 
<uses-permission 
android:name="android.permission.BLUETOOTH_ADMIN" /> 
<uses-permission 
android:name="android.permission.CALL_PHONE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
</manifest> 

錯誤日誌

10-24 16:58:12.716: E/AndroidRuntime(794): FATAL EXCEPTION: main 
10-24 16:58:12.716: E/AndroidRuntime(794): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.real/com.example.real.MainActivity}: java.lang.NullPointerException 
10-24 16:58:12.716: E/AndroidRuntime(794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137) 
10-24 16:58:12.716: E/AndroidRuntime(794): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
10-24 16:58:12.716: E/AndroidRuntime(794): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
10-24 16:58:12.716: E/AndroidRuntime(794): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
10-24 16:58:12.716: E/AndroidRuntime(794): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-24 16:58:12.716: E/AndroidRuntime(794): at android.os.Looper.loop(Looper.java:137) 
10-24 16:58:12.716: E/AndroidRuntime(794): at android.app.ActivityThread.main(ActivityThread.java:5103) 
10-24 16:58:12.716: E/AndroidRuntime(794): at java.lang.reflect.Method.invokeNative(Native Method) 
10-24 16:58:12.716: E/AndroidRuntime(794): at java.lang.reflect.Method.invoke(Method.java:525) 
10-24 16:58:12.716: E/AndroidRuntime(794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
10-24 16:58:12.716: E/AndroidRuntime(794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-24 16:58:12.716: E/AndroidRuntime(794): at dalvik.system.NativeStart.main(Native Method) 
10-24 16:58:12.716: E/AndroidRuntime(794): Caused by: java.lang.NullPointerException 
10-24 16:58:12.716: E/AndroidRuntime(794): at com.example.real.MainActivity.<init>(MainActivity.java:124) 
10-24 16:58:12.716: E/AndroidRuntime(794): at java.lang.Class.newInstanceImpl(Native Method) 
10-24 16:58:12.716: E/AndroidRuntime(794): at java.lang.Class.newInstance(Class.java:1130) 
10-24 16:58:12.716: E/AndroidRuntime(794): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 
10-24 16:58:12.716: E/AndroidRuntime(794): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128) 
10-24 16:58:12.716: E/AndroidRuntime(794): ... 11 more 

謝謝!

回答

0

你的問題是你試圖使用mBluetoothAdapter來構造一個成員變量。

BluetoothAdapter mBluetoothAdapter; //mBluetoothAdapter is null. 
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
+0

謝謝您的迴應!嘿,我是一個noob,但是這意味着我需要把它的代碼行放在實際的Onclick(View V方法)中。 – user2246076

+0

那麼,或者你可以只將BluetoothAdapter初始化爲BluetoothAdapter.getDefaultAdapter();而不是空。然而,我不會做那些關於對象構造vs onCreate或獨立線程(取決於你做什麼)。 – d3n13d1