简体   繁体   English

切换活动时,简单Droid App强制关闭

[英]Simple Droid App force closes when switching activities

I new to app development and I have been working on a Flash card app. 我是应用开发的新手,我一直在开发Flash卡应用。 There is a main screen and the user chooses a button. 有一个主屏幕,用户选择一个按钮。 On the emulator when I touch a button to go to a new activity, my app force closes. 在模拟器上,当我触摸一个按钮以转到新活动时,我的应用程序强制关闭。 I have added the activity in the manifest. 我已在清单中添加了活动。 The second activity has its own layout (main2.xml). 第二个活动具有其自己的布局(main2.xml)。 My buttons look like this: 我的按钮如下所示:

Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent addIntent = new Intent(view.getContext(), Add.class);
        startActivity(addIntent);

    }
});

Any ideas of what's going wrong? 有什么问题的想法吗? If more info is needed I can supply it, I've been stuck here for two days. 如果需要更多信息,我可以提供,我已经在这里停留了两天。 Thanks 谢谢

Your activity in the manifest should look like this: 您在清单中的活动应如下所示:

<activity android:name="ActivityClassName" 
          android:label="label"
    <intent-filter>
        <category
            android:name="android.intent.category.DEFAULT" />
        <action
            android:name="my.package.ACTIVITY_NAME" />
    </intent-filter>
</activity>

You then can start it like this: 然后,您可以像这样启动它:

Intent intent = new Intent("my.package.ACTIVITY_NAME");            
startActivity(intent);

To get more help post the manifest file and the exception you get, when the application crashes. 要获得更多帮助,请在应用程序崩溃时将清单文件和异常信息发布。 You can see that in Eclipse's debug perspective, in the LogCat view. 您可以在Eclipse的调试透视图中的LogCat视图中看到这一点。

you can not call startActivity on OnClickListener. 您不能在OnClickListener上调用startActivity。 you need to call it on your application context object or activity object. 您需要在应用程序上下文对象或活动对象上调用它。

Button add = (Button) findViewById(R.id.add);
Context ctx=this;
add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent addIntent = new Intent(view.getContext(), Add.class);
        ctx.startActivity(addIntent);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM