应用在创建新活动后崩溃

时间:2015-08-30 03:45:47

标签: android

所以我开了一个新的活动课:

public class TutorialActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tutorial);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.tutorial, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

在线程中我尝试打开它:

public void run() {
            if(newGame) {
                Intent intent = new Intent(this.activity, TutorialActivity.class);
                this.activity.startActivity(intent);
            }

            while(inGame) {
                try {
                    this.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

我看到了新的活动,因为我看到了我在其中设置的文字,但随后它立即崩溃了。所以它确实加载了活动,但随后发生了一些事情,它就崩溃了。

记录:

08-29 23:49:45.242: D/AndroidRuntime(21100): Shutting down VM
08-29 23:49:45.242: E/AndroidRuntime(21100): FATAL EXCEPTION: main
08-29 23:49:45.242: E/AndroidRuntime(21100): Process: com.example.trollacademy, PID: 21100
08-29 23:49:45.242: E/AndroidRuntime(21100): java.lang.RuntimeException: Unable to stop activity {com.example.trollacademy/com.example.trollacademy.MainActivity}: android.util.SuperNotCalledException: Activity {com.example.trollacademy/com.example.trollacademy.MainActivity} did not call through to super.onStop()
08-29 23:49:45.242: E/AndroidRuntime(21100):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3855)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3908)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at android.app.ActivityThread.access$1200(ActivityThread.java:177)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at android.os.Looper.loop(Looper.java:145)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at android.app.ActivityThread.main(ActivityThread.java:5942)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at java.lang.reflect.Method.invoke(Native Method)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at java.lang.reflect.Method.invoke(Method.java:372)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
08-29 23:49:45.242: E/AndroidRuntime(21100): Caused by: android.util.SuperNotCalledException: Activity {com.example.trollacademy/com.example.trollacademy.MainActivity} did not call through to super.onStop()
08-29 23:49:45.242: E/AndroidRuntime(21100):    at android.app.Activity.performStop(Activity.java:6495)
08-29 23:49:45.242: E/AndroidRuntime(21100):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3852)
08-29 23:49:45.242: E/AndroidRuntime(21100):    ... 10 more

3 个答案:

答案 0 :(得分:2)

您可以通过该活动调用新的" TutorialActivity"有一个错误的方法onStop()的实现。

带有标记@Override的方法应始终带有超级调用,因此您的onStop方法应该看起来像:

@Override
public void onStop(){
    super.onStop(); // THIS LINE IS MISSING ON YOUR CODE

    //Other things you want to do.
}

您没有发布其他活动的这部分代码,但可能就是这样。您也可以编辑您的问题并添加此部分代码以帮助其他人。

答案 1 :(得分:0)

要创建新活动,您必须创建新类并在androidManifest.xml中定义它。

答案 2 :(得分:0)

  

引起:android.util.SuperNotCalledException:Activity {com.example.trollacademy / com.example.trollacademy.MainActivity}没有调用super.onStop()

如果你覆盖onStop,你应该调用super.onStop()。 我不知道你使用哪个IDE,但是如果IDE给你一个红线,你应该找到红色的原因并修复它。
希望好玩