2

i wrote a java file, and i get an error at line :"blutooth_enabled_flag= mBluetoothAdapter.isEnabled();"

and i dont find the problem...when i run this app, i get "Unfortunately Application has stopped"

please help me find the problem

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; } } 

and this is the manifest file:

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

and here is the 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' 
7
  • can you post the logCat entry? Commented Sep 3, 2012 at 9:52
  • your error my be something diffrent.pls post your logcat Commented Sep 3, 2012 at 9:57
  • hi, i add a logcat to the original thread Commented Sep 3, 2012 at 10:11
  • sorround these with a try catch and give a try mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter(); wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE); blutooth_enabled_flag= mBluetoothAdapter.isEnabled(); Commented Sep 3, 2012 at 10:16
  • and what is the catch() input that i need to wrote? no one see where is the problem? Commented Sep 3, 2012 at 10:19

2 Answers 2

1

i think you are trying to run your app in emulator, since emulator doesn't support 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.

Sign up to request clarification or add additional context in comments.

Comments

0

"Caused by: java.lang.NullPointerException at com.example.fast.charger.Fast_Charger.onCreate(Fast_Charger.java:33)"

Line 33 is "blutooth_enabled_flag= mBluetoothAdapter.isEnabled();". The only way to get NPE here is to have null in mBluetoothAdapter. Check this

6 Comments

i dont understand why that the mBluetoothAdapter will be NULL?am i doing something wrong?
See the documentation. It says that result can be null if there is no bluetooth support on the target device. So you have to check this.
i even installed this app on my inspire 4g....and it also says:"Unfortunately Application has stopped" (and i have a bluetooth support)..please someone can help me?
replace "blutooth_enabled_flag= mBluetoothAdapter.isEnabled();" with "blutooth_enabled_flag= null != mBluetoothAdapter && mBluetoothAdapter.isEnabled();"
i understand that the problem is that mBluetoothAdapter is NULL, however i dont know hot to fix it...any ideas?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.