英文:
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<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.
<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. 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 = {"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]);
}
}
答案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
<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...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论