You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Intent跳转异常:点击按钮未跳转到MainActivity3却打开MainActivity2

Intent跳转异常:点击按钮期望打开MainActivity3却打开MainActivity2

尝试通过Intent从MainActivity跳转至MainActivity3,但点击按钮后实际打开的是MainActivity2。已关联正确的控件ID,代码可正常编译,调试器能识别MainActivity3,但模拟器仍显示MainActivity2。ListView点击跳转至MainActivity2的功能正常。


控件实例化代码

textViewCities = findViewById(R.id.listViewFirestore);
recyclerRedirect = findViewById(R.id.firestoreRecycler);

对应XML布局控件

<Button
    android:id="@+id/firestoreRecycler"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/delAccount"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:hint="testingRecyclerViewFirestore"
    />

<ListView
    android:id="@+id/listViewFirestore"
    android:layout_width="300dp"
    android:layout_height="400dp"
    android:paddingTop="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/signOut"
    app:layout_constraintVertical_bias="0.428" />

按钮点击事件代码(期望跳转MainActivity3)

recyclerRedirect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent d = new Intent(MainActivity.this, MainActivity3.class);
            d.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(d);
        }
    });

ListView点击事件代码(跳转MainActivity2正常)

textViewCities.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            clickedCity = (City)  adapterView.getItemAtPosition(i);

            Bundle bundle = new Bundle();
            bundle.putString("country", clickedCity.getCountry());
            bundle.putString("name", clickedCity.getName());
            bundle.putLong("population", clickedCity.getPopulation());

            Intent c = new Intent(getApplicationContext(), MainActivity2.class);
            c.putExtras(bundle);
            startActivity(c);
        }
    });

点击按钮后的Logcat截图

点击按钮后的Logcat日志


排查与解决方案

  1. 检查MainActivity3的清单注册
    确认AndroidManifest.xml中MainActivity3的注册项正确,无错误的启动模式或intent-filter干扰:

    <activity
        android:name=".MainActivity3"
        android:exported="false" />
    

    显式Intent跳转无需额外intent-filter,确保注册内容无误。

  2. 检查MainActivity3的 onCreate 逻辑
    查看MainActivity3的onCreate方法,是否存在自动跳转至MainActivity2的代码(比如误加的跳转逻辑),这类代码会导致打开MainActivity3后立刻跳转到MainActivity2,造成“直接打开后者”的错觉。

  3. 清理缓存并重建项目

    • 执行Build -> Clean Project,再Rebuild Project清除编译缓存
    • 卸载模拟器上的应用后重新安装,避免旧缓存残留导致异常。
  4. 调整Intent标志位
    按钮点击事件中使用的FLAG_ACTIVITY_NEW_TASK可能复用任务栈中已存在的MainActivity3实例,若该实例内部有跳转逻辑会引发问题。可尝试移除该标志位,或根据业务需求改用FLAG_ACTIVITY_CLEAR_TOP等合适标志。

  5. 验证控件点击范围
    检查布局中按钮是否被ListView等控件覆盖(比如ListView的布局范围包含了按钮区域),导致实际点击的是ListView而非按钮。可暂时隐藏ListView,单独测试按钮跳转功能确认问题。

内容的提问来源于stack exchange,提问作者cls

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 16:20:31