简体   繁体   English

Android意图切换导致onClick崩溃

[英]Android Intent Switching Crashes onClick

I'm fairly confused with this one. 我对此很困惑。 In my MainActivity.java I have a button that's supposed to go over to another class in another file RedGreenTest.java 在我的MainActivity.java中,我有一个按钮,该按钮应该转到另一个文件RedGreenTest.java另一个类。

redButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        findViewById(R.id.testRG_button);
        Intent i = new Intent(MainActivity.this, RedGreenTest.class);
        startActivity(i);
    }
});

So the button gets clicked, and bam insta-crash. 因此,该按钮被单击,并且bam insta-crash。 Debugger tells me that it's startActivity(i); 调试器告诉我,它是startActivity(i); no dua. 不对。 Going into the manifest, I change my <activity android:name=".MainActivity"> to <activity android:name=".RedGreenTest"> and the app starts up just fine. 进入清单,我将我的<activity android:name=".MainActivity">更改为<activity android:name=".RedGreenTest"> ,该应用程序就可以正常启动了。

Here is the beginning of my onCreate() class from the MainActivity.java file: 这是MainActivity.java文件中onCreate()类的开头:

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

And my onCreate() in my RedGreenTest.java : 还有我的RedGreenTest.java onCreate():

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

Running this through the debugger, I'm not getting any errors when the RedGreenTest.java file is set in the Manifest. 通过调试器运行它,在清单中设置RedGreenTest.java文件时,我没有收到任何错误。

What fundamental am I just missing from this what-should-be simple intent switch? 从这个应该简单的意图切换中我到底缺少什么基础?

You need to have both activities added to the manifest: 您需要将两个活动都添加到清单中:

<activity android:name=".RedGreenTest">
  ...
<activity android:name=".MainActivity">
      <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>

The reason that the app starts when you change MainActivity to RedGreenTest, is because you are setting the RedGreenTest activity to be the launch activity, which is set by the intent filter. 当您将MainActivity更改为RedGreenTest时,应用程序启动的原因是,您将RedGreenTest活动设置为启动活动,该活动由意图过滤器设置。

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

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