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

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

I couldn't declare the MainActivity on Manifest.xml

问题

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

IntroActivity.java 文件内容:

public class IntroActivity extends AppCompatActivity {

    // ... (省略了一些成员变量的声明)

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

        // 使活动全屏显示
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        if (restorePrefData()) {
            Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(mainActivity);
            finish();
        }

        setContentView(R.layout.activity_intro);

        btnNext = findViewById(R.id.btn_next);
        // ... (省略了一些控件的初始化)
        tvSkip = findViewById(R.id.tv_skip);

        final List<ScreenItem> mList = new ArrayList<>();
        mList.add(new ScreenItem("新鲜食品", "Lorem ipsum ...", R.drawable.img1));
        mList.add(new ScreenItem("快速送达", "Lorem ipsum ...", R.drawable.img2));
        mList.add(new ScreenItem("便捷支付", "Lorem ipsum ...", R.drawable.img3));

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

        tabIndicator.setupWithViewPager(screenPager);

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // ... (省略了按钮点击事件的部分)
            }
        });

        // ... (省略了一些事件监听器的设置)

        btnGetStarted.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mainActivity = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(mainActivity);
                savePrefsData();
                finish();
            }
        });

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

    // ... (省略了一些方法的定义)
}

MainActivity.java 文件内容:

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

    // ... (省略了一些成员变量的声明)

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

    public void defVars() {
        textSwitcherGender = findViewById(R.id.textSwitcherGender);
        // ... (省略了一些控件的初始化)

        nextclick();
        previousclick();
    }

    public void nextclick() {
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // ... (省略了按钮点击事件的部分)
            }
        });

        previousButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // ... (省略了按钮点击事件的部分)
            }
        });

        textSwitcherage.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                // ... (省略了文本切换器的工厂方法部分)
            }
        });

        textSwitcherage.setText(age[stringIndex1]);
    }

    // ... (省略了一些方法的定义)

}

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

英文:

that's my IntroActivty.java file:

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&lt;ScreenItem&gt; mList = new ArrayList&lt;&gt;();
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));
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));
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));
// 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 &lt; 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&#39;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(&quot;myPrefs&quot;,MODE_PRIVATE);
Boolean isIntroActivityOpnendBefore = pref.getBoolean(&quot;isIntroOpnend&quot;,false);
return  isIntroActivityOpnendBefore;
}
private void savePrefsData() {
SharedPreferences pref = getApplicationContext().getSharedPreferences(&quot;myPrefs&quot;,MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(&quot;isIntroOpnend&quot;,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.

&lt;application
android:allowBackup=&quot;true&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:supportsRtl=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;&gt;
&lt;activity android:name=&quot;.IntroActivity&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&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:

@RequiresApi(api = Build.VERSION_CODES.O)
public class MainActivity extends AppCompatActivity {
TextSwitcher textSwitcherage, textSwitcherGender;
Button nextButton, previousButton, nextButton2, previousButton2;
String[] age = {&quot;U-4&quot;,&quot;Kid&quot;,&quot;Teen&quot;,&quot;Adult&quot;,&quot;Old&quot;};
String[] gender = {&quot;Male&quot;,&quot;Female&quot;,&quot;Other&quot;};
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]);
}
}

答案1

得分: 0

尝试添加这行代码:

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

以下是完整的代码:

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

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

英文:

Try adding this line

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

Here's the full code

&lt;application
android:allowBackup=&quot;true&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:supportsRtl=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;&gt;
&lt;activity android:name=&quot;.IntroActivity&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;activity android:name=&quot;.MainActivity&quot;&gt;&lt;/activity&gt;
&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:

确定