蓝牙致命异常主要是android

时间:2013-10-24 21:21:46

标签: java android bluetooth

出于某种原因,这个新的蓝牙代码导致我的应用程序“停止工作”并崩溃。当我得到这样的错误时,我怀疑它是Android Manifest。但是,我发现它没有任何问题。请将注意力集中在启动功能中的蓝牙代码上。没有它的代码工作而不会崩溃应用程序。我的格式也非常苛刻。

主要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

谢谢!!!

1 个答案:

答案 0 :(得分:0)

您的问题是您尝试使用mBluetoothAdapter构建成员变量。

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