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

エミュレーターはBluetoothをサポートしていないため、エミュレーターでアプリを実行しようとしていると思います

 mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();

mBluetoothAdapter is coming out to be null. Run this app on your bluetooth enabled droid and i think it will work fine.

于 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 に答える