1

アプリを起動すると、致命的な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

助けてくれてありがとう。

4

2 に答える 2

1

コードを変更してみてください

Handler h ;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            h = new Handler();
            Checker();
        }
于 2012-12-18T08:29:15.350 に答える
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します。また、メソッド名は大文字で始まりません。taskListslistSizehhandler

于 2012-12-18T08:55:13.537 に答える