Incorrect Activity when running App Android Studio

271 Views Asked by At

I have starting building an app in Android studio. I have established the MainPage as the launcher activity in the manifest.xml.

<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. It is also not running some code correctly on the NumberPicker. Even though I've set the picker to have a min, max, and default, the picker only shows 0 and will not scroll. The two issues seem to be related somehow, in terms of what activity is being run.

this is the courseSelectCode:

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.

EDIT: on request, here is my MainPage.java code:

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);
    }
    ...
}
4

There are 4 best solutions below

0
On BEST ANSWER

edit the Mainfest.xml, in order to enforce portrait layout there already:

<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):

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 ); to the proper resource.

because it starts the MainPage Activity, but then inflates the wrong XML file.


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).

1
On

First of all There is a mistake in your manifest file

you wrote screenOrientation attribute outside the opening tag

 <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

2
On

Try this and error was in 9th line because your code line is outside of the tag:

    <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
0
On

Your launcher activity is MainPage but you are calling the layout of courseselect activity inside MainPage activity's onCreate method on this line

setContentView(R.layout.activity_course_select);

Change it to your MainPage layout

setContentView(R.layout.yourMainPageLayout);