如何避免Illegalstateexception:非片段活动没有活动

时间:2015-10-28 09:06:00

标签: android android-activity

我正在使用活动开发简单的Android应用程序。当我因illegastateexception: no activity而导致第二次活动应用崩溃时切换回主要活动。

我在第二次活动中使用了handlertimertask

10-28 04:52:19.778: E/AndroidRuntime(1094): java.lang.IllegalStateException: No activity
10-28 04:52:19.778: E/AndroidRuntime(1094):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at android.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1848)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at android.app.Activity.onCreate(Activity.java:901)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at com.example.timertask.MainActivity.onCreate(MainActivity.java:33)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at com.example.timertask.MainActivity.stopTask(MainActivity.java:169)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at com.example.timertask.MainActivity$5$1.run(MainActivity.java:151)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at android.os.Handler.handleCallback(Handler.java:733)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at android.os.Handler.dispatchMessage(Handler.java:95)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at android.os.Looper.loop(Looper.java:137)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at android.app.ActivityThread.main(ActivityThread.java:4998)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at java.lang.reflect.Method.invokeNative(Native Method)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at java.lang.reflect.Method.invoke(Method.java:515)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
10-28 04:52:19.778: E/AndroidRuntime(1094):     at dalvik.system.NativeStart.main(Native Method)  

这是我的第一个活动

public class StartScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_screen);
        TextView play = (TextView) findViewById(R.id.textView2);
        play.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent intent = new Intent(StartScreen.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.start_screen, menu);
        return true;
    }

}

这是我的第二项活动

public class MainActivity extends Activity {

TimerTask mtimertask;
final Handler handler = new Handler();
Timer t = new Timer();
TextView tv1, tv2, time;
Button but1, but2, but3, but4;
Random rannum1;
int num1, num2, timecount = 6, sum = 0;

private int ncounter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 = (TextView) findViewById(R.id.textView1);
    tv2 = (TextView) findViewById(R.id.textView2);
    but1 = (Button) findViewById(R.id.button1);
    but1.setOnClickListener(mbut1Listener);
    but2 = (Button) findViewById(R.id.button2);
    but2.setOnClickListener(mbut2Listener);
    but3 = (Button) findViewById(R.id.button3);
    but3.setOnClickListener(mbut3Listener);
    but4 = (Button) findViewById(R.id.button4);
    but4.setOnClickListener(mbut4Listener);
    doTimerTask();
    rannum1 = new Random();
    num1 = rannum1.nextInt(10 - 1) + 1;
    num2 = rannum1.nextInt(10 - 1) + 1;
    sum = num1 + num2;
    int ans[] = { sum, sum + 1, sum + 2, sum - 1 };
    tv1.setText(String.valueOf(num1));
    tv2.setText(String.valueOf(num2));

    but1.setText(String.valueOf(ans[rannum1.nextInt(4)]));
    but2.setText(String.valueOf(ans[rannum1.nextInt(4)]));
    but3.setText(String.valueOf(ans[rannum1.nextInt(4)]));
    but4.setText(String.valueOf(ans[rannum1.nextInt(4)]));

}

View.OnClickListener mbut1Listener = new OnClickListener() {

    @Override
    public void onClick(View v) {

        int num = Integer.valueOf((String) but1.getText());
        if (num == sum) {
            stopTask();
        } else {
            // finish();
            // Intent intent=new
            // Intent(MainActivity.this,StartScreen.class);
            // startActivity(intent);
        }
    }
};
View.OnClickListener mbut2Listener = new OnClickListener() {

    @Override
    public void onClick(View v) {

        int num = Integer.valueOf((String) but2.getText());
        if (num == sum) {
            stopTask();
        } else {
            // finish();
            // Intent intent=new
            // Intent(MainActivity.this,StartScreen.class);
            // startActivity(intent);

        }
    }
};
View.OnClickListener mbut3Listener = new OnClickListener() {

    @Override
    public void onClick(View v) {

        int num = Integer.valueOf((String) but3.getText());
        if (num == sum) {
            stopTask();
        } else {
            // finish();
            // Intent intent=new
            // Intent(MainActivity.this,StartScreen.class);
            // startActivity(intent);

        }
    }
};
View.OnClickListener mbut4Listener = new OnClickListener() {

    @Override
    public void onClick(View v) {

        int num = Integer.valueOf((String) but4.getText());
        if (num == sum) {
            stopTask();
        } else {
            // finish();
            // Intent intent=new
            // Intent(MainActivity.this,StartScreen.class);
            // startActivity(intent);

        }
    }
};

public void doTimerTask() {
    mtimertask = new TimerTask() {

        @Override
        public void run() {

            handler.post(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    time = (TextView) findViewById(R.id.textView3);
                    time.setText("" + String.valueOf(timecount - 1));
                    timecount--;
                    ncounter++;

                    if (ncounter == 6) {
                        stopTask();
                    }
                }
            });

        }

    };
    t.schedule(mtimertask, 0, 5000);
}

public void stopTask() {

    if (mtimertask != null) {

        mtimertask.cancel();
        ncounter = 0;
        timecount = 6;
        onCreate(null);

    }
}  

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.timertask"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.timertask.MainActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="com.example.timertask.StartScreen"
        android:label="@string/title_activity_start_screen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

 </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

这是因为您的TimerTask仍在运行,而在第二个Activity停止时未停止。因此Runnable执行并尝试操纵第二个活动中的TextView,该活动不再存在。在第二项活动中调用Runnable时,请彻底停止onPause()