2

当电池处于定义的水平时,我试图更改 TextView 的文本,但是当发生这种情况时,我得到了一个错误,错误是

java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in famium.fokus.fhg.de.monitoringthebatterylevelandchargingstate.MainActivity$1@4139cca8

在这里我的代码,我在导致错误的行之前发表了评论。

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        /*
            BatteryManager
                The BatteryManager class contains strings and constants used for values in the
                ACTION_BATTERY_CHANGED Intent, and provides a method for querying battery
                and charging properties.
        */
        /*
            public static final String EXTRA_SCALE
                Extra for ACTION_BATTERY_CHANGED: integer containing the maximum battery level.
                Constant Value: "scale"
        */
        // Get the battery scale
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
        // Display the battery scale in TextView
        mTextViewInfo.setText("Battery Scale : " + scale);

        /*
            public static final String EXTRA_LEVEL
                Extra for ACTION_BATTERY_CHANGED: integer field containing the current battery
                level, from 0 to EXTRA_SCALE.

                Constant Value: "level"
        */
        // get the battery level
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
        // Display the battery level in TextView
        mTextViewInfo.setText(mTextViewInfo.getText() + "\nBattery Level : " + level);

        // Calculate the battery charged percentage
        float percentage = level/ (float) scale;
        // Update the progress bar to display current battery charged percentage
        mProgressStatus = (int)((percentage)*100);

        // Show the battery charged percentage text inside progress bar
        mTextViewPercentage.setText("" + mProgressStatus + "%");

        // Show the battery charged percentage in TextView
        mTextViewInfo.setText(mTextViewInfo.getText() +
                "\nPercentage : "+ mProgressStatus + "%");

        // Display the battery charged percentage in progress bar
        mProgressBar.setProgress(mProgressStatus);

        String action = intent.getAction();
        if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
            IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            Intent batteryStatus = context.registerReceiver(null, ifilter);

            int level1 = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale1 = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
            int percent = (level1*100)/scale1;
 // here the lines that causes the error
            if (percent >= 97) {
                _textView2.setText(percent);
            }
        }
    }
};

我试图关注这篇文章https://stackoverflow.com/a/10910024/9308420但我不知道出了什么问题。

4

1 回答 1

1

看完这篇文章我明白了

https://stackoverflow.com/a/23791368/9308420

所以,而不是

_textView2.setText(percent);

变成

_textView2.setText("" + percent);
于 2018-02-04T19:23:04.787 回答