我无法在Manifest.xml中声明MainActivity。

huangapple go评论111阅读模式
英文:

I couldn't declare the MainActivity on Manifest.xml

问题

以下是你的代码部分的翻译:

IntroActivity.java 文件内容:

  1. public class IntroActivity extends AppCompatActivity {
  2. // ... (省略了一些成员变量的声明)
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. // 使活动全屏显示
  7. requestWindowFeature(Window.FEATURE_NO_TITLE);
  8. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  9. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  10. if (restorePrefData()) {
  11. Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
  12. startActivity(mainActivity);
  13. finish();
  14. }
  15. setContentView(R.layout.activity_intro);
  16. btnNext = findViewById(R.id.btn_next);
  17. // ... (省略了一些控件的初始化)
  18. tvSkip = findViewById(R.id.tv_skip);
  19. final List<ScreenItem> mList = new ArrayList<>();
  20. mList.add(new ScreenItem("新鲜食品", "Lorem ipsum ...", R.drawable.img1));
  21. mList.add(new ScreenItem("快速送达", "Lorem ipsum ...", R.drawable.img2));
  22. mList.add(new ScreenItem("便捷支付", "Lorem ipsum ...", R.drawable.img3));
  23. screenPager = findViewById(R.id.screen_viewpager);
  24. introViewPagerAdapter = new IntroViewPagerAdapter(this, mList);
  25. screenPager.setAdapter(introViewPagerAdapter);
  26. tabIndicator.setupWithViewPager(screenPager);
  27. btnNext.setOnClickListener(new View.OnClickListener() {
  28. @Override
  29. public void onClick(View v) {
  30. // ... (省略了按钮点击事件的部分)
  31. }
  32. });
  33. // ... (省略了一些事件监听器的设置)
  34. btnGetStarted.setOnClickListener(new View.OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
  38. startActivity(mainActivity);
  39. savePrefsData();
  40. finish();
  41. }
  42. });
  43. tvSkip.setOnClickListener(new View.OnClickListener() {
  44. @Override
  45. public void onClick(View v) {
  46. screenPager.setCurrentItem(mList.size());
  47. }
  48. });
  49. }
  50. // ... (省略了一些方法的定义)
  51. }

MainActivity.java 文件内容:

  1. @RequiresApi(api = Build.VERSION_CODES.O)
  2. public class MainActivity extends AppCompatActivity {
  3. // ... (省略了一些成员变量的声明)
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. defVars();
  9. }
  10. public void defVars() {
  11. textSwitcherGender = findViewById(R.id.textSwitcherGender);
  12. // ... (省略了一些控件的初始化)
  13. nextclick();
  14. previousclick();
  15. }
  16. public void nextclick() {
  17. nextButton.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View view) {
  20. // ... (省略了按钮点击事件的部分)
  21. }
  22. });
  23. previousButton.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View view) {
  26. // ... (省略了按钮点击事件的部分)
  27. }
  28. });
  29. textSwitcherage.setFactory(new ViewSwitcher.ViewFactory() {
  30. @Override
  31. public View makeView() {
  32. // ... (省略了文本切换器的工厂方法部分)
  33. }
  34. });
  35. textSwitcherage.setText(age[stringIndex1]);
  36. }
  37. // ... (省略了一些方法的定义)
  38. }

希望这些翻译对你有帮助。如果有任何问题,请随时提问。

英文:

that's my IntroActivty.java file:

  1. public class IntroActivity extends AppCompatActivity {
  2. private ViewPager screenPager;
  3. IntroViewPagerAdapter introViewPagerAdapter ;
  4. TabLayout tabIndicator;
  5. Button btnNext;
  6. int position = 0 ;
  7. Button btnGetStarted;
  8. Animation btnAnim ;
  9. TextView tvSkip;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. // make the activity on full screen
  14. requestWindowFeature(Window.FEATURE_NO_TITLE);
  15. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  16. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  17. // when this activity is about to be launch we need to check if its openened before or not
  18. if (restorePrefData()) {
  19. Intent mainActivity = new Intent(getApplicationContext(),MainActivity.class );
  20. startActivity(mainActivity);
  21. finish();
  22. }
  23. setContentView(R.layout.activity_intro);
  24. // hide the action bar
  25. //getSupportActionBar().hide();
  26. // ini views
  27. btnNext = findViewById(R.id.btn_next);
  28. btnGetStarted = findViewById(R.id.btn_get_started);
  29. tabIndicator = findViewById(R.id.tab_indicator);
  30. btnAnim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.button_animation);
  31. tvSkip = findViewById(R.id.tv_skip);
  32. // fill list screen
  33. final List&lt;ScreenItem&gt; mList = new ArrayList&lt;&gt;();
  34. mList.add(new ScreenItem(&quot;Fresh Food&quot;,&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, consectetur consectetur adipiscing elit&quot;,R.drawable.img1));
  35. mList.add(new ScreenItem(&quot;Fast Delivery&quot;,&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, consectetur consectetur adipiscing elit&quot;,R.drawable.img2));
  36. mList.add(new ScreenItem(&quot;Easy Payment&quot;,&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua, consectetur consectetur adipiscing elit&quot;,R.drawable.img3));
  37. // setup viewpager
  38. screenPager =findViewById(R.id.screen_viewpager);
  39. introViewPagerAdapter = new IntroViewPagerAdapter(this,mList);
  40. screenPager.setAdapter(introViewPagerAdapter);
  41. // setup tablayout with viewpager
  42. tabIndicator.setupWithViewPager(screenPager);
  43. // next button click Listner
  44. btnNext.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. position = screenPager.getCurrentItem();
  48. if (position &lt; mList.size()) {
  49. position++;
  50. screenPager.setCurrentItem(position);
  51. }
  52. if (position == mList.size()-1) { // when we rech to the last screen
  53. // TODO : show the GETSTARTED Button and hide the indicator and the next button
  54. loaddLastScreen();
  55. }
  56. }
  57. });
  58. // tablayout add change listener
  59. tabIndicator.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
  60. @Override
  61. public void onTabSelected(TabLayout.Tab tab) {
  62. if (tab.getPosition() == mList.size()-1) {
  63. loaddLastScreen();
  64. }
  65. }
  66. @Override
  67. public void onTabUnselected(TabLayout.Tab tab) {
  68. }
  69. @Override
  70. public void onTabReselected(TabLayout.Tab tab) {
  71. }
  72. });
  73. // Get Started button click listener
  74. btnGetStarted.setOnClickListener(new View.OnClickListener() {
  75. @Override
  76. public void onClick(View v) {
  77. //open main activity
  78. Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
  79. startActivity(mainActivity);
  80. // also we need to save a boolean value to storage so next time when the user run the app
  81. // we could know that he is already checked the intro screen activity
  82. // i&#39;m going to use shared preferences to that process
  83. savePrefsData();
  84. finish();
  85. }
  86. });
  87. // skip button click listener
  88. tvSkip.setOnClickListener(new View.OnClickListener() {
  89. @Override
  90. public void onClick(View v) {
  91. screenPager.setCurrentItem(mList.size());
  92. }
  93. });
  94. }
  95. private boolean restorePrefData() {
  96. SharedPreferences pref = getApplicationContext().getSharedPreferences(&quot;myPrefs&quot;,MODE_PRIVATE);
  97. Boolean isIntroActivityOpnendBefore = pref.getBoolean(&quot;isIntroOpnend&quot;,false);
  98. return isIntroActivityOpnendBefore;
  99. }
  100. private void savePrefsData() {
  101. SharedPreferences pref = getApplicationContext().getSharedPreferences(&quot;myPrefs&quot;,MODE_PRIVATE);
  102. SharedPreferences.Editor editor = pref.edit();
  103. editor.putBoolean(&quot;isIntroOpnend&quot;,true);
  104. editor.commit();
  105. }
  106. // show the GETSTARTED Button and hide the indicator and the next button
  107. private void loaddLastScreen() {
  108. btnNext.setVisibility(View.INVISIBLE);
  109. btnGetStarted.setVisibility(View.VISIBLE);
  110. tvSkip.setVisibility(View.INVISIBLE);
  111. tabIndicator.setVisibility(View.INVISIBLE);
  112. // TODO : ADD an animation the getstarted button
  113. // setup animation
  114. btnGetStarted.setAnimation(btnAnim);
  115. }
  116. }

and this is the Manifest.xml file.

  1. &lt;application
  2. android:allowBackup=&quot;true&quot;
  3. android:icon=&quot;@mipmap/ic_launcher&quot;
  4. android:label=&quot;@string/app_name&quot;
  5. android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
  6. android:supportsRtl=&quot;true&quot;
  7. android:theme=&quot;@style/AppTheme&quot;&gt;
  8. &lt;activity android:name=&quot;.IntroActivity&quot;&gt;
  9. &lt;intent-filter&gt;
  10. &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  11. &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
  12. &lt;/intent-filter&gt;
  13. &lt;/activity&gt;
  14. &lt;/application&gt;

In my app, i have an OnBoarding screen which is for introducing the app content. 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. And now, i can't declare the MainActivity in Manifest file.

this is the MainActivity.java:

  1. @RequiresApi(api = Build.VERSION_CODES.O)
  2. public class MainActivity extends AppCompatActivity {
  3. TextSwitcher textSwitcherage, textSwitcherGender;
  4. Button nextButton, previousButton, nextButton2, previousButton2;
  5. String[] age = {&quot;U-4&quot;,&quot;Kid&quot;,&quot;Teen&quot;,&quot;Adult&quot;,&quot;Old&quot;};
  6. String[] gender = {&quot;Male&quot;,&quot;Female&quot;,&quot;Other&quot;};
  7. TextView textView;
  8. Typeface typeface;
  9. Integer stringIndex1=0, stringIndex2=0;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. defVars();
  15. }
  16. public void defVars()
  17. {
  18. textSwitcherGender = findViewById(R.id.textSwitcherGender);
  19. textSwitcherage = findViewById(R.id.textSwitcherage);
  20. nextButton = findViewById(R.id.nextButton1);
  21. nextButton2 = findViewById(R.id.nextButton2);
  22. previousButton = findViewById(R.id.previousButton1);
  23. previousButton2 = findViewById(R.id.previousButton2);
  24. typeface = getResources().getFont(R.font.lettersforlearners);
  25. nextclick();
  26. previousclick();
  27. //nextAndPreviousButtons(nextButton,previousButton,textSwitcherage,age,0);
  28. //nextAndPreviousButtons(nextButton2, previousButton2, textSwitcherGender, gender,0);
  29. }
  30. public void nextclick()
  31. {
  32. nextButton.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View view) {
  35. if(stringIndex1 == age.length-1)
  36. {
  37. stringIndex1 = 0;
  38. textSwitcherage.setText(age[stringIndex1]);
  39. }
  40. else
  41. {
  42. textSwitcherage.setText(age[++stringIndex1]);
  43. }
  44. }
  45. });
  46. previousButton.setOnClickListener(new View.OnClickListener() {
  47. @Override
  48. public void onClick(View view) {
  49. if (stringIndex1==0)
  50. {
  51. stringIndex1 = age.length-1;
  52. textSwitcherage.setText(age[stringIndex1]);
  53. }
  54. else
  55. {
  56. textSwitcherage.setText(age[--stringIndex1]);
  57. }
  58. }
  59. });
  60. textSwitcherage.setFactory(new ViewSwitcher.ViewFactory() {
  61. @Override
  62. public View makeView() {
  63. textView = new TextView(MainActivity.this);
  64. textView.setTypeface(typeface);
  65. textView.setTextColor(Color.BLACK);
  66. textView.setTextSize(40);
  67. textView.setGravity(Gravity.CENTER_HORIZONTAL);
  68. return textView;
  69. }
  70. });
  71. textSwitcherage.setText(age[stringIndex1]);
  72. }
  73. //--------------------------------------------------------------------------------------------
  74. public void previousclick()
  75. {
  76. nextButton2.setOnClickListener(new View.OnClickListener() {
  77. @Override
  78. public void onClick(View view) {
  79. if(stringIndex2 == gender.length-1)
  80. {
  81. stringIndex2 = 0;
  82. textSwitcherGender.setText(gender[stringIndex2]);
  83. }
  84. else
  85. {
  86. textSwitcherGender.setText(gender[++stringIndex2]);
  87. }
  88. }
  89. });
  90. previousButton2.setOnClickListener(new View.OnClickListener() {
  91. @Override
  92. public void onClick(View view) {
  93. if (stringIndex2==0)
  94. {
  95. stringIndex2 = gender.length-1;
  96. textSwitcherGender.setText(gender[stringIndex2]);
  97. }
  98. else
  99. {
  100. textSwitcherGender.setText(gender[--stringIndex2]);
  101. }
  102. }
  103. });
  104. textSwitcherGender.setFactory(new ViewSwitcher.ViewFactory() {
  105. @Override
  106. public View makeView() {
  107. textView = new TextView(MainActivity.this);
  108. textView.setTypeface(typeface);
  109. textView.setTextColor(Color.BLACK);
  110. textView.setTextSize(40);
  111. textView.setGravity(Gravity.CENTER_HORIZONTAL);
  112. return textView;
  113. }
  114. });
  115. textSwitcherGender.setText(gender[stringIndex2]);
  116. }
  117. }

答案1

得分: 0

尝试添加这行代码:

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

以下是完整的代码:

  1. <application
  2. android:allowBackup="true"
  3. android:icon="@mipmap/ic_launcher"
  4. android:label="@string/app_name"
  5. android:roundIcon="@mipmap/ic_launcher_round"
  6. android:supportsRtl="true"
  7. android:theme="@style/AppTheme">
  8. <activity android:name=".IntroActivity">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <activity android:name=".MainActivity"></activity>
  15. </application>

希望对你有所帮助。如有疑问,请随意询问...

英文:

Try adding this line

&lt;activity android:name=&quot;.MainActivity&quot;&gt;&lt;/activity&gt;

Here's the full code

  1. &lt;application
  2. android:allowBackup=&quot;true&quot;
  3. android:icon=&quot;@mipmap/ic_launcher&quot;
  4. android:label=&quot;@string/app_name&quot;
  5. android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
  6. android:supportsRtl=&quot;true&quot;
  7. android:theme=&quot;@style/AppTheme&quot;&gt;
  8. &lt;activity android:name=&quot;.IntroActivity&quot;&gt;
  9. &lt;intent-filter&gt;
  10. &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  11. &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
  12. &lt;/intent-filter&gt;
  13. &lt;/activity&gt;
  14. &lt;activity android:name=&quot;.MainActivity&quot;&gt;&lt;/activity&gt;
  15. &lt;/application&gt;

Hope this helps. Feel free to ask for clarifications...

huangapple
  • 本文由 发表于 2020年8月27日 22:53:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63618696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定