简体   繁体   English

运行App Android Studio时活动不正确

[英]Incorrect Activity when running App Android Studio

I have starting building an app in Android studio. 我已经开始在Android Studio中构建应用。 I have established the MainPage as the launcher activity in the manifest.xml . 我已经在manifest.xml中将MainPage建立为启动器活动。

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">

    <activity android:name=".MainPage">
        android:screenOrientation="portrait"
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".courseSelect" />
    <activity android:name=".profile1" />
    <activity android:name=".stats1" />
    <activity android:name=".ReviewRounds" />
    <activity android:name=".ReferFriends" />
    <activity android:name=".RangeMode" />
</application>

I have double checked that the run configuration is set to 'Default' and yet the app is running a different activity, entitled courseSelect . 我已经仔细检查了运行配置是否设置为“默认”,但该应用程序正在运行另一个名为courseSelect活动。 It is also not running some code correctly on the NumberPicker . 它也没有在NumberPicker上正确运行某些代码。 Even though I've set the picker to have a min, max, and default, the picker only shows 0 and will not scroll. 即使我已将选择器设置为具有最小值,最大值和默认值,但选择器仅显示0,并且不会滚动。 The two issues seem to be related somehow, in terms of what activity is being run. 就正在运行的活动而言,这两个问题似乎有某种联系。

this is the courseSelect Code: 这是courseSelect代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.NumberPicker;
import co.ceryle.segmentedbutton.SegmentedButtonGroup;

public class courseSelect extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_select);

        //Hole Picker
        NumberPicker holePicker = (NumberPicker)findViewById(R.id.holePicker);
        holePicker.setMaxValue(18);
        holePicker.setMinValue(1);
        holePicker.setWrapSelectorWheel(false);
        holePicker.setValue(1);

        SegmentedButtonGroup sbg = (SegmentedButtonGroup) findViewById(R.id.segmentedButtonGroup);
        sbg.setOnClickedButtonPosition(new SegmentedButtonGroup.OnClickedButtonPosition() {
            @Override
            public void onClickedButtonPosition(int position) {
                // if(position == 0)
            }
        });
    }
}

I tried to set the run configuration specifically to the MainPage activity and it still opens in the courseSelect Page. 我试图将运行配置专门设置为MainPage活动,但它仍在courseSelect页面中打开。

EDIT: on request, here is my MainPage.java code: 编辑:根据要求,这是我的MainPage.java代码:

public class MainPage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_select);

        Window g = getWindow();
        g.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.TYPE_STATUS_BAR);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    ...
}

edit the Mainfest.xml , in order to enforce portrait layout there already: 编辑Mainfest.xml ,以便在那里已经强制执行纵向布局:

<activity android:name=".MainPage"
    android:screenOrientation="portrait"
    android:configChanges="orientation|keyboardHidden">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

...which makes this code merely useless ( styles.xml can also be used for window styles): ...这使得此代码仅无用( styles.xml也可用于窗口样式):

Window g = getWindow();
g.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.TYPE_STATUS_BAR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

also update setContentView( R.layout.activity_course_select ); 还更新setContentView( R.layout.activity_course_select ); to the proper resource. 到适当的资源。

because it starts the MainPage Activity, but then inflates the wrong XML file. 因为它启动MainPage Activity,但随后又膨胀了错误的XML文件。


one "suggested edit" before was to swap the order of setContentView() and the paragraph below ...which I've rejected, because setting it in Manifest.xml appeared more organized (less code). 之前的一个“建议编辑”是交换setContentView()和下面的段落的顺序...我拒绝了,因为在Manifest.xml设置似乎更有条理(代码更少)。

First of all There is a mistake in your manifest file 首先,清单文件中有一个错误

you wrote screenOrientation attribute outside the opening tag 您在开始标记之外编写了screenOrientation属性

 <activity android:name=".MainPage">
        android:screenOrientation="portrait"
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

it should be 它应该是

<activity 
        android:name=".MainPage"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and you used wrong xml to setContenView 并且您使用了错误的xml来setContenView

Try this and error was in 9th line because your code line is outside of the tag: 尝试此操作,错误在第9行,因为您的代码行在标记之外:

    <activity android:name=".MainPage">
       android:screenOrientation="portrait"  // error 

Do this : 做这个 :

    <activity android:name=".MainPage"
       android:screenOrientation="portrait">  // After doing this no error

and do also this thing : 并做这个事情:

  public class MainPage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_select); // error

do this : 做这个 :

public class MainPage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.MAIN_PAGE_ACTIVITY_NAME); // no error

Your launcher activity is MainPage but you are calling the layout of courseselect activity inside MainPage activity's onCreate method on this line 您的启动器活动是MainPage,但您正在此行上在MainPage活动的onCreate方法中调用Courseselect活动的布局

setContentView(R.layout.activity_course_select);

Change it to your MainPage layout 将其更改为您的MainPage布局

setContentView(R.layout.yourMainPageLayout); 

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

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