gpt4 book ai didi

java - IllegalStateException:系统服务在 onCreate() 之前对 Activity 不可用

转载 作者:行者123 更新时间:2023-11-30 03:52:28 25 4
gpt4 key购买 nike

当我启动我的应用程序时,我不断收到致命的 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

感谢您的帮助。

最佳答案

尝试更改您的代码

Handler h ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h = new Handler();
Checker();
}

关于java - IllegalStateException:系统服务在 onCreate() 之前对 Activity 不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com