当我启动应用程序时,我不断收到致命的java.lang.IllegalStateException错误,但我不确定原因。基本上我所要做的就是每10秒返回一次正在运行的进程和任务。
这是我的代码:
public class MainActivity extends Activity {
Handler h = new Handler();
public static final String TAG = "MyActivity";
int s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Checker();
}
public void Checker() {
Handler h = new Handler();
h.post(check);
}
Runnable check = new Runnable() {
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
public void run() {
Processes(am);
Tasks(am);
h.postAtTime(check, 10000);
}
};
public void Processes(ActivityManager am) {
List<ActivityManager.RunningAppProcessInfo> l = am.getRunningAppProcesses();
s = l.size();
for(int i = 0; i < s; i++) {
Log.d(TAG, "Process: " + l.get(i).processName);
}
}
public void Tasks(ActivityManager am) {
List<ActivityManager.RunningTaskInfo> l = am.getRunningTasks(1000);
s = l.size();
for(int i = 0; i < s; i++) {
// .baseActivity returns the process that started the Task
Log.d(TAG, "Task: " + l.get(i).baseActivity.getPackageName());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
以下是我的日志:
12-18 08:03:53.190: E/AndroidRuntime(1577): FATAL EXCEPTION: main
12-18 08:03:53.190: E/AndroidRuntime(1577): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidside/com.example.androidside.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
12-18 08:03:53.190: E/AndroidRuntime(1577): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
12-18 08:03:53.190: E/AndroidRuntime(1577): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-18 08:03:53.190: E/AndroidRuntime(1577): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-18 08:03:53.190: E/AndroidRuntime(1577): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-18 08:03:53.190: E/AndroidRuntime(1577): at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 08:03:53.190: E/AndroidRuntime(1577): at android.os.Looper.loop(Looper.java:137)
12-18 08:03:53.190: E/AndroidRuntime(1577): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-18 08:03:53.190: E/AndroidRuntime(1577): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 08:03:53.190: E/AndroidRuntime(1577): at java.lang.reflect.Method.invoke(Method.java:511)
12-18 08:03:53.190: E/AndroidRuntime(1577): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-18 08:03:53.190: E/AndroidRuntime(1577): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-18 08:03:53.190: E/AndroidRuntime(1577): at dalvik.system.NativeStart.main(Native Method)
12-18 08:03:53.190: E/AndroidRuntime(1577): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
12-18 08:03:53.190: E/AndroidRuntime(1577): at android.app.Activity.getSystemService(Activity.java:4463)
12-18 08:03:53.190: E/AndroidRuntime(1577): at com.example.androidside.MainActivity$1.<init>(MainActivity.java:54)
12-18 08:03:53.190: E/AndroidRuntime(1577): at com.example.androidside.MainActivity.<init>(MainActivity.java:53)
12-18 08:03:53.190: E/AndroidRuntime(1577): at java.lang.Class.newInstanceImpl(Native Method)
12-18 08:03:53.190: E/AndroidRuntime(1577): at java.lang.Class.newInstance(Class.java:1319)
12-18 08:03:53.190: E/AndroidRuntime(1577): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
12-18 08:03:53.190: E/AndroidRuntime(1577): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
12-18 08:03:53.190: E/AndroidRuntime(1577): ... 11 more
感谢您的帮助。
答案 0 :(得分:1)
尝试更改代码
Handler h ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h = new Handler();
Checker();
}
答案 1 :(得分:0)
首先尝试在需要时初始化变量:
private Handler h;
private Runnable check;
public void Checker() {
check = new Runnable() {
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
public void run() {
Processes(am);
Tasks(am);
h.postAtTime(check, 10000);
}
};
h = new Handler();
h.post(check);
}
public void onResume() {
super.onResume();
Checker();
}
其次,尝试选择实际上说明它是什么的变量名。例如,更改l
中的taskList
,s
中的listSize
和h
中的handler
。此外,方法名称不以大写字母开头。