Java 蓝牙致命异常主android
由于某种原因,这个新的蓝牙代码导致我的应用程序“停止工作”并崩溃。当我遇到这样的错误时,我怀疑这是Android清单。但是,我找不到任何问题。请注意启动功能中的蓝牙代码。没有它的代码不会使应用程序崩溃。我的格式也很不规范 Main Activity.javaJava 蓝牙致命异常主android,java,android,bluetooth,Java,Android,Bluetooth,由于某种原因,这个新的蓝牙代码导致我的应用程序“停止工作”并崩溃。当我遇到这样的错误时,我怀疑这是Android清单。但是,我找不到任何问题。请注意启动功能中的蓝牙代码。没有它的代码不会使应用程序崩溃。我的格式也很不规范 Main Activity.java package com.example.real; import java.util.Set; import android.app.Activity; import android.bluetooth.BluetoothAdapte
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 + "]");
}
}
package com.example.real;
导入java.util.Set;
导入android.app.Activity;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.content.Context;
导入android.content.Intent;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.CountDownTimer;
导入android.speech.tts.TextToSpeech;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展活动实现LocationListener{
按钮启动、停止;
文本视图电视;
文本视图tv2;
文本视图和;
位置经理lm;
texttospeechtalker;
意向电话=新意向(意向.行动电话);
字符串Hello=(“Hello World”);
int i,sum1;
蓝牙适配器mBluetoothAdapter;
蓝牙设备;
设置pairedDevices=mBluetoothAdapter.getBondedDevices();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化测试函数helloworld
Button Button=(Button)this.findViewById(R.id.Button01);
setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
Toast.makeText(MainActivity.this,“GPS正在进行”,Toast.LENGTH_SHORT.show();
}
});
//初始化计数器序列
start=(按钮)findViewById(R.id.start);//启动扣押序列
stop=(按钮)findViewById(R.id.stop);//覆盖函数
tv=(TextView)findviewbyd(R.id.tv);
tv.setText(“30”);//从30开始。
sum=(TextView)findViewById(R.id.sum);
最终MyCounter计时器=新的MyCounter(300001000);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v)
{
如果(pairedDevices.size()>0)
{
用于(蓝牙设备:pairedDevices)
{
if(device.getName().equals(“uIceblue2”))
{
mmDevice=设备;
对于(i=0;i 120)
{
timer.start();
}
}
}
}
}
}
});
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
timer.cancel();
tv.setText(“防止紧急呼叫第一响应者”);
}
});
//初始化GPS布局
tv2=(TextView)findViewById(R.id.GPS);
lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
lm.RequestLocationUpdate(LocationManager.GPS_提供程序,20,1,本);
}
公共类MyCounter扩展了倒计时{
公共MyCounter(长百万未来,长倒计时间隔){
超级(毫秒未来,倒计时间隔);
}
@凌驾
公共无效onFinish(){
System.out.println(“计时器完成”);
tv.setText(“向第一响应者发送信息”);
setData(Uri.parse(“电话:7034004407”);
星触觉(电话);
}
@凌驾
公共void onTick(长毫秒未完成){
tv.setText((毫秒/1000)+”);
System.out.println(“计时器:”+(毫秒直到完成/1000));
}
}
@凌驾
已更改公共void onLocationChanged(位置arg0)
{
String lat=String.valueOf(arg0.getLatitude());
String lon=String.valueOf(arg0.getLongitude());
Log.e(“GPS”,“位置更改:lat=“+lat+”,lon=“+lon”);
tv2.setText(“lat=“+lat+”,long=“+lon”);
}
公共无效onProviderDisabled(字符串arg0){
Log.e(“GPS”、“提供商禁用”+arg0);
}
已启用公共void onProviderEnabled(字符串arg0){
Log.e(“GPS”、“供应商启用”+arg0);
}
状态已更改的公共void(字符串arg0、整数arg1、捆绑包arg2){
Log.e(“GPS”,“状态更改为“+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
10-24 16:58:12.716:E/AndroidRuntime(794):致命异常:main
10-24 16:58:12.716:E/AndroidRuntime(794):java.lang.RuntimeException:无法实例化活动组件信息{com.example.real/com.example.real.MainActivity}:java.lang.NullPointerException
10-24 16:58:12.716:E/AndroidRuntime(794):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
10-24 16:58:12.716:E/AndroidRuntime(794):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-24 16:58:12.716:E/AndroidRuntime(794):在android.app.ActivityThread.access$600(ActivityThread.java:141)
10-24 16:58:12.716:E/AndroidRuntime(794):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
BluetoothAdapter mBluetoothAdapter; //mBluetoothAdapter is null.
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();