我的应用程序有几个操作栏选项卡,这是其中一个片段。它应该显示一些有关电池的信息。浏览应用程序后出现错误。请说明导致问题的原因以及应该采取的措施来解决它
以下是来自 LogCat 的错误:
05-15 07:24:30.809: E/AndroidRuntime(25883): FATAL EXCEPTION: main
05-15 07:24:30.809: E/AndroidRuntime(25883): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in com.example.mytaskmanager.Battery$1@4185ea78
05-15 07:24:30.809: E/AndroidRuntime(25883): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
05-15 07:24:30.809: E/AndroidRuntime(25883): at android.os.Handler.handleCallback(Handler.java:605)
05-15 07:24:30.809: E/AndroidRuntime(25883): at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 07:24:30.809: E/AndroidRuntime(25883): at android.os.Looper.loop(Looper.java:154)
05-15 07:24:30.809: E/AndroidRuntime(25883): at android.app.ActivityThread.main(ActivityThread.java:4624)
05-15 07:24:30.809: E/AndroidRuntime(25883): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 07:24:30.809: E/AndroidRuntime(25883): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 07:24:30.809: E/AndroidRuntime(25883): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-15 07:24:30.809: E/AndroidRuntime(25883): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-15 07:24:30.809: E/AndroidRuntime(25883): at dalvik.system.NativeStart.main(Native Method)
05-15 07:24:30.809: E/AndroidRuntime(25883): Caused by: java.lang.NullPointerException
05-15 07:24:30.809: E/AndroidRuntime(25883): at com.example.mytaskmanager.Battery.setare(Battery.java:77)
05-15 07:24:30.809: E/AndroidRuntime(25883): at com.example.mytaskmanager.Battery$1.onReceive(Battery.java:120)
05-15 07:24:30.809: E/AndroidRuntime(25883): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:746)
05-15 07:24:30.809: E/AndroidRuntime(25883): ... 9 more
和代码:
public class Battery extends Fragment {
private TextView batteryInfo, memoryInfo, procent;
int health = 0, level=0, plugged=0, scale=0, status=0, temperature=0, voltage=0;
String technology = "";
boolean present = false;
ProgressBar pb;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.layout, container, false);
return myFragmentView;
}
@Override
public void onResume() {
super.onResume();
procent=(TextView)getActivity().findViewById(R.id.BatteryProc);
batteryInfo=(TextView)getActivity().findViewById(R.id.textViewBatteryInfo);
pb = (ProgressBar)getActivity().findViewById(R.id.progressbar);
memoryInfo = (TextView)getActivity().findViewById(R.id.memory);
setare();
getMemInfo();
}
public void setare(){
procent.setText("Level: " + level + "%");
pb.setProgress(level);
batteryInfo.setText(
"Health: "+health+"\n"+
"Plugged: "+plugged+"\n"+
"Present: "+present+"\n"+
"Scale: "+scale+"\n"+
"Status: "+status+"\n"+
"Technology: "+technology+"\n"+
"Temperature: "+temperature+"\n"+
"Voltage: "+voltage+"\n");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
health= intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
plugged= intent.getIntExtra(BatteryManager.EXTRA_PLUGGED,0);
present= intent.getExtras().getBoolean(BatteryManager.EXTRA_PRESENT);
scale= intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
status= intent.getIntExtra(BatteryManager.EXTRA_STATUS,0);
technology= intent.getExtras().getString(BatteryManager.EXTRA_TECHNOLOGY);
temperature= intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
voltage= intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);
batteryInfo=(TextView)getActivity().findViewById(R.id.textViewBatteryInfo);
pb = (ProgressBar)getActivity().findViewById(R.id.progressbar);
memoryInfo = (TextView)getActivity().findViewById(R.id.memory);
procent=(TextView)getActivity().findViewById(R.id.BatteryProc);
setare();
getMemInfo();
}
};