电池百分比代码中的空指针异常

时间:2014-09-22 11:39:41

标签: android broadcastreceiver batterylevel

这是我的代码我在电池百分比代码中得到空指针异常: 我要做的就是将电池百分比放在文本视图中,而不是全部

public class MainActivity extends Activity implements SurfaceHolder.Callback {

public static CFlashLight flash = new CFlashLight();
public static SurfaceView surfaceView;
public static SurfaceHolder surfaceHolder;
private final String sCRLF = System.getProperty("line.separator");
boolean bDarkBackground;

//teststrob
StrobeRunner runner;
Thread bw;
Handler mHandler = new Handler();
Runnable mShowToastRunnable = new Runnable() {
    public void run() {
        showMessage();
    }
};
//test battery
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);
      }
//test

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bDarkBackground = true;

    getBatteryPercentage();

这里是log cat out permissoins可以。

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

4 个答案:

答案 0 :(得分:1)

你甚至在onCreate等之前过早地执行这个陈述:

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

至少应该在onCreate中充气布局之后。

答案 1 :(得分:1)

在设置onContentView后在OnCreate方法中初始化变量 所以改变像这样的创建方法

 private void getBatteryPercentage() {
    final TextView batteryPercent = (TextView) this.findViewById(R.id.TextView02);
      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);
      }

答案 2 :(得分:1)

使用此代码:

package com.example.ex;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

public class MainActivity extends Activity implements SurfaceHolder.Callback {

    public static CFlashLight flash = new CFlashLight();
    public static SurfaceView surfaceView;
    public static SurfaceHolder surfaceHolder;
    private final String sCRLF = System.getProperty("line.separator");
    boolean bDarkBackground;

    // teststrob
    StrobeRunner runner;
    Thread bw;
    Handler mHandler = new Handler();
    Runnable mShowToastRunnable = new Runnable() {
        public void run() {
            showMessage();
        }
    };
    // test battery
    private TextView batteryPercent;

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

    // test

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        bDarkBackground = true;

        getBatteryPercentage();

答案 3 :(得分:0)

我会在布局通胀后将TextView代码移动。

setContentView(R.layout.activity_main);

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

这应该有效(假设没有逻辑错误)。