简体   繁体   English

Android Studio:无法切换到新活动

[英]Android Studio: Can't switch to new activity

I'm trying to learn Android development and have gone through the basic tutorials. 我正在尝试学习Android开发,并已完成了基础教程。 I'm making a golf scorecard application. 我正在申请高尔夫记分卡。 Originally I went from Homescreen -> SelectCourseActivity -> Scorecard. 最初,我来自主屏幕-> SelectCourseActivity->记分卡。 Everything was working fine, but I wanted to get rid of the SelectCourseActivity, so I tried to have the main button on the Homescreen to straight to the Scorecard, but now it always throws this exception: 一切工作正常,但我想摆脱SelectCourseActivity,因此我尝试使主屏幕上的主按钮直接指向记分卡,但现在它始终会引发以下异常:

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

Here is the code for the button OnClick: 这是按钮OnClick的代码:

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

And yes I have gone into the AndroidManifest and set the parent of ScorecardActivity to Homescreen. 是的,我进入了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>

Even after I returned everything to normal using Git, the ScorecardActivity will still not start. 即使我使用Git使一切恢复正常后,ScorecardActivity仍将无法启动。 I appreciate any and all help! 非常感谢您的帮助! Please let me know if there's any more information needed. 请让我知道是否需要更多信息。

If what @DmytroSytro doesn't work, you can try editing your manifest file to look like the below and see if that helps you. 如果@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>

Try this in OnCreate: 在OnCreate中尝试以下操作:

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

        }
    });

I made a dumb mistake, that I just figured out. 我弄明白了,这是一个愚蠢的错误。 I'm storing the information necessary for the scorecard on a local file, because originally I wanted to learn how that worked for android studio. 我将记分卡所需的信息存储在本地文件中,因为最初我想学习如何在android studio中使用。 Turns out that I wasn't asking permission to access the devices local files before I tried to load the scorecard information in the ScorecardActivity OnCreate. 原来,在尝试将记分卡信息加载到ScorecardActivity OnCreate中之前,我并没有要求访问设备本地文件的权限。 As a follow up, anybody know how to ensure that permission is obtained before the information is accessed? 作为后续行动,有人知道如何确保在访问信息之前获得许可吗?

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

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