2

我写了一个java文件,我在行出现错误:“blutooth_enabled_flag= mBluetoothAdapter.isEnabled();”

我没有找到问题......当我运行这个应用程序时,我得到“不幸的是应用程序已停止”

请帮我找出问题

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>

这是logcat:

09-03 10:07:07.701: I/dalvikvm(537): threadid=3: reacting to signal 3
09-03 10:07:07.721: I/dalvikvm(537): Wrote stack traces to '/data/anr/traces.txt'
09-03 10:07:08.221: I/dalvikvm(537): threadid=3: reacting to signal 3
09-03 10:07:08.261: I/dalvikvm(537): Wrote stack traces to '/data/anr/traces.txt'
09-03 10:07:08.442: D/AndroidRuntime(537): Shutting down VM
09-03 10:07:08.442: W/dalvikvm(537): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-03 10:07:08.451: E/AndroidRuntime(537): FATAL EXCEPTION: main
09-03 10:07:08.451: E/AndroidRuntime(537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fast.charger/com.example.fast.charger.Fast_Charger}: java.lang.NullPointerException
09-03 10:07:08.451: E/AndroidRuntime(537):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-03 10:07:08.451: E/AndroidRuntime(537):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-03 10:07:08.451: E/AndroidRuntime(537):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-03 10:07:08.451: E/AndroidRuntime(537):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-03 10:07:08.451: E/AndroidRuntime(537):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 10:07:08.451: E/AndroidRuntime(537):  at android.os.Looper.loop(Looper.java:137)
09-03 10:07:08.451: E/AndroidRuntime(537):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-03 10:07:08.451: E/AndroidRuntime(537):  at java.lang.reflect.Method.invokeNative(Native Method)
09-03 10:07:08.451: E/AndroidRuntime(537):  at java.lang.reflect.Method.invoke(Method.java:511)
09-03 10:07:08.451: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-03 10:07:08.451: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-03 10:07:08.451: E/AndroidRuntime(537):  at dalvik.system.NativeStart.main(Native Method)
09-03 10:07:08.451: E/AndroidRuntime(537): Caused by: java.lang.NullPointerException
09-03 10:07:08.451: E/AndroidRuntime(537):  at com.example.fast.charger.Fast_Charger.onCreate(Fast_Charger.java:33)
09-03 10:07:08.451: E/AndroidRuntime(537):  at android.app.Activity.performCreate(Activity.java:4465)
09-03 10:07:08.451: E/AndroidRuntime(537):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-03 10:07:08.451: E/AndroidRuntime(537):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-03 10:07:08.451: E/AndroidRuntime(537):  ... 11 more
09-03 10:07:08.731: I/dalvikvm(537): threadid=3: reacting to signal 3
09-03 10:07:08.821: I/dalvikvm(537): Wrote stack traces to '/data/anr/traces.txt'
09-03 10:07:09.102: I/dalvikvm(537): threadid=3: reacting to signal 3
09-03 10:07:09.111: I/dalvikvm(537): Wrote stack traces to '/data/anr/traces.txt'
4

2 回答 2

1

我认为您正在尝试在模拟器中运行您的应用程序,因为模拟器不支持蓝牙

 mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();

mBluetoothAdapter 出来是null。在您启用蓝牙的机器人上运行此应用程序,我认为它会正常工作。

于 2012-09-04T05:57:19.083 回答
0

“引起:com.example.fast.charger.Fast_Charger.onCreate(Fast_Charger.java:33) 处的 java.lang.NullPointerException”

第 33 行是“blutooth_enabled_flag= mBluetoothAdapter.isEnabled();”。在此处获取 NPE 的唯一方法是在 mBluetoothAdapter 中设置为 null。检查这个

于 2012-09-03T10:17:36.537 回答