【问题标题】:Android Studio: Can't switch to new activityAndroid Studio:无法切换到新活动
【发布时间】:2019-01-13 19:21:57
【问题描述】:

我正在尝试学习 Android 开发并已完成基本教程。我正在制作高尔夫记分卡应用程序。最初我从主屏幕 -> SelectCourseActivity -> 记分卡。一切正常,但我想摆脱 SelectCourseActivity,所以我试图让主屏幕上的主按钮直接进入记分卡,但现在它总是抛出这个异常:

catch (InvocationTargetException e) {
            throw new IllegalStateException(
                    "Could not execute method for android:onClick", e);
        }

这里是按钮 OnClick 的代码:

public void Scorecard(View view)  {
    Intent MyIntent = new Intent(this, ScorecardActivity.class);
    startActivity(MyIntent);
}

是的,我已经进入 AndroidManifest 并将 ScorecardActivity 的父级设置为 Homescreen。

<application
    android:allowBackup="true"
    android:icon="@mipmap/wgcc_icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/wgcc_icon_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".HomeScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SelectCourseActivity"
        android:parentActivityName=".HomeScreen" />
    <activity
        android:name=".ScorecardActivity"
        android:parentActivityName=".HomeScreen" />
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/btn_dropdown"
        android:spinnerMode="dropdown"/>

</application>

即使在我使用 Git 使一切恢复正常后,ScorecardActivity 仍然无法启动。我感谢任何和所有的帮助!如果需要更多信息,请告诉我。

【问题讨论】:

  • 我不确定这是否能解决问题,但请尝试在此行中添加 Homescreen.this 而不是仅添加 this Intent MyIntent = new Intent(this, ScorecardActivity.class);
  • 为什么你的清单中有一个微调器?

标签: java android android-studio button android-activity


【解决方案1】:

如果 @DmytroSytro 不起作用,您可以尝试将清单文件编辑为如下所示,看看是否对您有帮助。

<application
    android:allowBackup="true"
    android:icon="@mipmap/wgcc_icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/wgcc_icon_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".HomeScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name=".ScorecardActivity"
        android:parentActivityName=".HomeScreen">
        <meta-data
               android:name="android.support.PARENT_ACTIVITY"
               android:value="com.example.android.your_activity_class.MainActivity" />
    </activity>
</application>

【讨论】:

    【解决方案2】:

    在 OnCreate 中试试这个:

     Button.setOnClickListener(new View.OnClickListener() {
     @Override
            public void onClick(View v) {
                Intent intent = new Intent(Homescreen.this, Scorecard.class);
                startActivity(intent);
    
            }
        });
    

    【讨论】:

      【解决方案3】:

      我犯了一个愚蠢的错误,我刚刚发现。我将记分卡所需的信息存储在本地文件中,因为最初我想了解它是如何在 android studio 中工作的。事实证明,在我尝试在 ScorecardActivity OnCreate 中加载记分卡信息之前,我并没有请求访问设备本地文件的权限。作为后续,有人知道如何确保在访问信息之前获得许可吗?

      【讨论】:

      • 这个link 可以帮助你获得文件权限
      猜你喜欢
      • 2016-01-05
      • 2019-05-15
      • 2012-01-31
      • 2015-04-26
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多