Android : null pointer exception in battery percent code

on Monday, September 22, 2014


here is my code i get null pointer exception in battery percentage code here: what im triyng to do is to put the battery percentage in a textview thahts all



private TextView batteryPercent= (TextView) this.findViewById(R.id.TextView01);

private void getBatteryPercentage() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (currentLevel >= 0 && scale > 0) {
level = (currentLevel * 100) / scale;
}

batteryPercent.setText("Battery: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}


and here is the log cat out put permissoins are ok .



09-22 14:12:40.417: E/AndroidRuntime(17552): FATAL EXCEPTION: main
09-22 14:12:40.417: E/AndroidRuntime(17552): Process: com.soheil.prolight, PID: 17552
09-22 14:12:40.417: E/AndroidRuntime(17552): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.soheil.prolight/com.soheil.prolight.MainActivity}: java.lang.NullPointerException
09-22 14:12:40.417: E/AndroidRuntime(17552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
09-22 14:12:40.417: E/AndroidRuntime(17552): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-22 14:12:40.417: E/AndroidRuntime(17552): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-22 14:12:40.417: E/AndroidRuntime(17552): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-22 14:12:40.417: E/AndroidRuntime(17552): at android.os.Handler.dispatchMessage(Handler.java:102)
09-22 14:12:40.417: E/AndroidRuntime(17552): at android.os.Looper.loop(Looper.java:136)
09-22 14:12:40.417: E/AndroidRuntime(17552): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-22 14:12:40.417: E/AndroidRuntime(17552): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 14:12:40.417: E/AndroidRuntime(17552): at java.lang.reflect.Method.invoke(Method.java:515)
09-22 14:12:40.417: E/AndroidRuntime(17552): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-22 14:12:40.417: E/AndroidRuntime(17552): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-22 14:12:40.417: E/AndroidRuntime(17552): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
09-22 14:12:40.417: E/AndroidRuntime(17552): at dalvik.system.NativeStart.main(Native Method)
09-22 14:12:40.417: E/AndroidRuntime(17552): Caused by: java.lang.NullPointerException
09-22 14:12:40.417: E/AndroidRuntime(17552): at android.app.Activity.findViewById(Activity.java:1884)
09-22 14:12:40.417: E/AndroidRuntime(17552): at com.soheil.prolight.MainActivity.<init>(MainActivity.java:56)
09-22 14:12:40.417: E/AndroidRuntime(17552): at java.lang.Class.newInstanceImpl(Native Method)
09-22 14:12:40.417: E/AndroidRuntime(17552): at java.lang.Class.newInstance(Class.java:1208)
09-22 14:12:40.417: E/AndroidRuntime(17552): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
09-22 14:12:40.417: E/AndroidRuntime(17552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
09-22 14:12:40.417: E/AndroidRuntime(17552): ... 12 more

0 comments:

Post a Comment