我的应用程序有几个操作栏标签,这是其中一个片段。它应该显示有关电池的一些信息。导航应用程序一段时间后会出现错误。请解释导致问题的原因以及解决问题的方法
以下是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();
}
};
答案 0 :(得分:1)
您的View层次结构中的过程,batteryInfo视图在哪里?如果它们位于onCreateView中的R.layout.layout中,请将findViewByIds移动到onCreateView并使用myFragmentView而不是getActivity():
例如:procent = (TextView) myFragmentView.findViewById(R.id.BatteryProc);