Java 蓝牙android错误
我编写了一个java文件,第行出现错误:“blutooth_enabled_flag=mBluetoothAdapter.isEnabled();” 我没有发现问题…当我运行这个应用程序时,我得到“不幸的是,应用程序已停止” 请帮我找到问题Java 蓝牙android错误,java,android,bluetooth,Java,Android,Bluetooth,我编写了一个java文件,第行出现错误:“blutooth_enabled_flag=mBluetoothAdapter.isEnabled();” 我没有发现问题…当我运行这个应用程序时,我得到“不幸的是,应用程序已停止” 请帮我找到问题 package com.example.fast.battery.charger; import android.net.wifi.WifiManager; import android.os.Bundle; import android.app.Acti
package com.example.fast.battery.charger;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.view.View.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
public class Fast_Charger extends Activity implements OnClickListener {
Button Onbutton,Offbutton,prefbutton;
BluetoothAdapter mBluetoothAdapter;
WifiManager wifi;
SeekBar brightbar;
int bluetooth_state;
Boolean wifi_enable_flag;
Boolean blutooth_enabled_flag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fast__charger);
Onbutton= (Button)findViewById(R.id.Onbutton);
Offbutton= (Button)findViewById(R.id.Offbutton);
prefbutton = (Button)findViewById(R.id.prefbutton);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
blutooth_enabled_flag= mBluetoothAdapter.isEnabled();
// wifi_enable_flag=wifi.isWifiEnabled();
// Onbutton.setOnClickListener(this);
// Offbutton.setOnClickListener(this);
// prefbutton.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_fast__charger, menu);
return true;
}
public void onClick(View src) {
// switch (src.getId()) {
// case R.id.Onbutton:
// mBluetoothAdapter.disable();
// wifi.setWifiEnabled(Boolean.TRUE);
// break;
// case R.id.Offbutton:
// if (bluetooth_state==BluetoothAdapter.STATE_ON) {
// mBluetoothAdapter.enable();
// }
// if(wifi_enable_flag==Boolean.TRUE) {
// wifi.setWifiEnabled(Boolean.FALSE);
// }
// break;
// case R.id.Brightbar:
// break;
// case R.id.prefbutton:
// break;
}
}
这是清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fast.charger"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Fast_Charger"
android:label="@string/title_activity_fast__charger" >
<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>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
<uses-permission android:name="com.example.bluetooth.PERMISSION"></uses-permission>
<uses-feature android:name="android.hardware.wifi" />
</manifest>
“原因:com.example.fast.charger.fast\u charger.onCreate(fast\u charger.java:33)上的java.lang.NullPointerException”
第33行是“blutooth_enabled_flag=mBluetoothAdapter.isEnabled();”。在这里获取NPE的唯一方法是在mBluetoothAdapter中设置null。检查此项我认为您正在尝试在emulator中运行应用程序,因为emulator不支持蓝牙
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter显示为空。
在支持蓝牙功能的droid上运行此应用程序,我认为它可以正常工作。您可以发布logCat条目吗?您的错误可能有所不同。请发布您的logcathi,我将logCat添加到原始线程中,或使用try catch绕过这些线程,然后尝试mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();wifi=(WifiManager)getSystemService(Context.wifi\u服务);blutooth_enabled_flag=mBluetoothAdapter.isEnabled();我需要编写的catch()输入是什么?没人知道问题出在哪里?我不明白为什么mBluetoothAdapter会为空?我做错什么了吗?请参阅。它说,如果目标设备上没有蓝牙支持,结果可能为空。所以你必须检查一下。我甚至在我的inspire 4g上安装了这个应用程序……它还说:“不幸的是,应用程序已经停止了”(我有蓝牙支持)…请有人能帮我吗?将“blutooth_enabled_flag=mBluetoothAdapter.isEnabled();”替换为“blutooth_enabled_flag=null!=mBluetoothAdapter&&mBluetoothAdapter.isEnabled()”我知道问题是mBluetoothAdapter为空,但是我不知道如何修复它…有什么想法吗?
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();