简体   繁体   English

我无法在 Manifest.xml 上声明 MainActivity

[英]I couldn't declare the MainActivity on Manifest.xml

that's my IntroActivty.java file:那是我的 IntroActivty.java 文件:

public class IntroActivity extends AppCompatActivity {

    private ViewPager screenPager;
    IntroViewPagerAdapter introViewPagerAdapter ;
    TabLayout tabIndicator;
    Button btnNext;
    int position = 0 ;
    Button btnGetStarted;
    Animation btnAnim ;
    TextView tvSkip;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // make the activity on full screen

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);


        // when this activity is about to be launch we need to check if its openened before or not

        if (restorePrefData()) {

            Intent mainActivity = new Intent(getApplicationContext(),MainActivity.class );
            startActivity(mainActivity);
            finish();


        }

        setContentView(R.layout.activity_intro);

        // hide the action bar

        //getSupportActionBar().hide();

        // ini views
        btnNext = findViewById(R.id.btn_next);
        btnGetStarted = findViewById(R.id.btn_get_started);
        tabIndicator = findViewById(R.id.tab_indicator);
        btnAnim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.button_animation);
        tvSkip = findViewById(R.id.tv_skip);

        // fill list screen

        final List<ScreenItem> mList = new ArrayList<>();
        mList.add(new ScreenItem("Fresh Food","Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, consectetur  consectetur adipiscing elit",R.drawable.img1));
        mList.add(new ScreenItem("Fast Delivery","Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, consectetur  consectetur adipiscing elit",R.drawable.img2));
        mList.add(new ScreenItem("Easy Payment","Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, consectetur  consectetur adipiscing elit",R.drawable.img3));

        // setup viewpager
        screenPager =findViewById(R.id.screen_viewpager);
        introViewPagerAdapter = new IntroViewPagerAdapter(this,mList);
        screenPager.setAdapter(introViewPagerAdapter);

        // setup tablayout with viewpager

        tabIndicator.setupWithViewPager(screenPager);

        // next button click Listner

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                position = screenPager.getCurrentItem();
                if (position < mList.size()) {

                    position++;
                    screenPager.setCurrentItem(position);


                }

                if (position == mList.size()-1) { // when we rech to the last screen

                    // TODO : show the GETSTARTED Button and hide the indicator and the next button

                    loaddLastScreen();


                }



            }
        });

        // tablayout add change listener


        tabIndicator.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                if (tab.getPosition() == mList.size()-1) {

                    loaddLastScreen();

                }


            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });



        // Get Started button click listener

        btnGetStarted.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                //open main activity

                Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(mainActivity);
                // also we need to save a boolean value to storage so next time when the user run the app
                // we could know that he is already checked the intro screen activity
                // i'm going to use shared preferences to that process
                savePrefsData();
                finish();



            }
        });

        // skip button click listener

        tvSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                screenPager.setCurrentItem(mList.size());
            }
        });



    }

    private boolean restorePrefData() {


        SharedPreferences pref = getApplicationContext().getSharedPreferences("myPrefs",MODE_PRIVATE);
        Boolean isIntroActivityOpnendBefore = pref.getBoolean("isIntroOpnend",false);
        return  isIntroActivityOpnendBefore;



    }

    private void savePrefsData() {

        SharedPreferences pref = getApplicationContext().getSharedPreferences("myPrefs",MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("isIntroOpnend",true);
        editor.commit();


    }

    // show the GETSTARTED Button and hide the indicator and the next button
    private void loaddLastScreen() {

        btnNext.setVisibility(View.INVISIBLE);
        btnGetStarted.setVisibility(View.VISIBLE);
        tvSkip.setVisibility(View.INVISIBLE);
        tabIndicator.setVisibility(View.INVISIBLE);
        // TODO : ADD an animation the getstarted button
        // setup animation
        btnGetStarted.setAnimation(btnAnim);



    }
}

and this is the Manifest.xml file.这是 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/AppTheme">
        <activity android:name=".IntroActivity">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


    </application>

In my app, i have an OnBoarding screen which is for introducing the app content.在我的应用程序中,我有一个 OnBoarding 屏幕,用于介绍应用程序内容。 As you know it shown only once.如你所知,它只显示了一次。 Actually i wasn't know that the issue is actually about Manifest file, in my previous question a benevolent guy said me that.实际上我不知道这个问题实际上是关于 Manifest 文件,在我之前的问题中,一个仁慈的人对我说。 And now, i can't declare the MainActivity in Manifest file.现在,我无法在 Manifest 文件中声明 MainActivity。

this is the MainActivity.java:这是 MainActivity.java:

@RequiresApi(api = Build.VERSION_CODES.O)
public class MainActivity extends AppCompatActivity {

        TextSwitcher textSwitcherage, textSwitcherGender;
        Button nextButton, previousButton, nextButton2, previousButton2;
        String[] age = {"U-4","Kid","Teen","Adult","Old"};
        String[] gender = {"Male","Female","Other"};
        TextView textView;
        Typeface typeface;
        Integer stringIndex1=0, stringIndex2=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        defVars();
    }

    public void defVars()
    {
        textSwitcherGender = findViewById(R.id.textSwitcherGender);
        textSwitcherage = findViewById(R.id.textSwitcherage);
        nextButton = findViewById(R.id.nextButton1);
        nextButton2 = findViewById(R.id.nextButton2);
        previousButton = findViewById(R.id.previousButton1);
        previousButton2 = findViewById(R.id.previousButton2);
        typeface = getResources().getFont(R.font.lettersforlearners);

        nextclick();
        previousclick();

        //nextAndPreviousButtons(nextButton,previousButton,textSwitcherage,age,0);
        //nextAndPreviousButtons(nextButton2, previousButton2, textSwitcherGender, gender,0);
    }

    public void nextclick()
    {
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(stringIndex1 == age.length-1)
                {
                    stringIndex1 = 0;
                    textSwitcherage.setText(age[stringIndex1]);
                }
                else
                {
                    textSwitcherage.setText(age[++stringIndex1]);
                }
            }
        });

        previousButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (stringIndex1==0)
                {
                    stringIndex1 = age.length-1;
                    textSwitcherage.setText(age[stringIndex1]);
                }
                else
                {
                    textSwitcherage.setText(age[--stringIndex1]);
                }
            }
        });

        textSwitcherage.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                textView = new TextView(MainActivity.this);
                textView.setTypeface(typeface);
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(40);
                textView.setGravity(Gravity.CENTER_HORIZONTAL);
                return textView;
            }
        });

        textSwitcherage.setText(age[stringIndex1]);

    }

    //--------------------------------------------------------------------------------------------

    public void previousclick()
    {
        nextButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(stringIndex2 == gender.length-1)
                {
                    stringIndex2 = 0;
                    textSwitcherGender.setText(gender[stringIndex2]);
                }
                else
                {
                    textSwitcherGender.setText(gender[++stringIndex2]);
                }
            }
        });

        previousButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (stringIndex2==0)
                {
                    stringIndex2 = gender.length-1;
                    textSwitcherGender.setText(gender[stringIndex2]);
                }
                else
                {
                    textSwitcherGender.setText(gender[--stringIndex2]);
                }
            }
        });

        textSwitcherGender.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                textView = new TextView(MainActivity.this);
                textView.setTypeface(typeface);
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(40);
                textView.setGravity(Gravity.CENTER_HORIZONTAL);
                return textView;
            }
        });

        textSwitcherGender.setText(gender[stringIndex2]);

    }

}

Try adding this line尝试添加这一行

<activity android:name=".MainActivity"></activity>

Here's the full code这是完整的代码

<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/AppTheme">
        <activity android:name=".IntroActivity">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

Hope this helps.希望这可以帮助。 Feel free to ask for clarifications...随时要求澄清...

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

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