我有一个mainactivity和一个按钮“add”,应该发送到第二个Activity(它有一个tablayout和fragments)。下面的代码是launchsecondactivty()方法。
Intent intent = new Intent(this, SecondActivity.class);
String et_title = title.getText().toString();
String et_room = room.getText().toString();
String et_code = code.getText().toString();
intent.putExtra(EXTRA_MESSAGE, et_title);
intent.putExtra(EXTRA_MESSAGE, et_room);
intent.putExtra(EXTRA_MESSAGE, et_code);
startActivityForResult(intent, ADD_REQUEST);
这是我的第二个activity oncreate()方法。
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
这将非常有效。但当我为片段添加代码时,
tabLayout = (TabLayout) findViewById(R.id.tab_days);
tabMon = (TabItem) findViewById(R.id.itm_mon);
tabTue = (TabItem) findViewById(R.id.itm_tue);
tabWed = (TabItem) findViewById(R.id.itm_wed);
tabThu = (TabItem) findViewById(R.id.itm_thu);
tabFri = (TabItem) findViewById(R.id.itm_fri);
tabSat = (TabItem) findViewById(R.id.itm_sat);
tabSun = (TabItem) findViewById(R.id.itm_sun);
viewPager = (ViewPager) findViewById(R.id.viewpager);
pagerAdapter = new com.example.timetable2.PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
if(tab.getPosition() == 0) {
pagerAdapter.notifyDataSetChanged();
}else if(tab.getPosition() == 1) {
pagerAdapter.notifyDataSetChanged();
}else if(tab.getPosition() == 2) {
pagerAdapter.notifyDataSetChanged();
}else if(tab.getPosition() == 3) {
pagerAdapter.notifyDataSetChanged();
}else if(tab.getPosition() == 4) {
pagerAdapter.notifyDataSetChanged();
}else if(tab.getPosition() == 5) {
pagerAdapter.notifyDataSetChanged();
}else if(tab.getPosition() == 6){
pagerAdapter.notifyDataSetChanged();
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
}
这是事故日志。
2020-12-13 02:16:47.064 6449-6449/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.timetable2, PID: 6449
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timetable2/com.example.timetable2.SecondActivity}: java.lang.ClassCastException: androidx.viewpager2.widget.ViewPager2 cannot be cast to androidx.viewpager.widget.ViewPager
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3654)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
Caused by: java.lang.ClassCastException: androidx.viewpager2.widget.ViewPager2 cannot be cast to androidx.viewpager.widget.ViewPager
at com.example.timetable2.SecondActivity.onCreate(SecondActivity.java:36)
at android.app.Activity.performCreate(Activity.java:7963)
at android.app.Activity.performCreate(Activity.java:7952)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3629)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:8167)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
这不管用了。请帮助我的项目。这里是初学者((
1条答案
按热度按时间3ks5zfa01#
我知道我在评论中帮了忙,但要回答这个问题,这里有一个简单的解决方案:
java.lang.ClassCastException
当您试图将一个类强制转换为另一个类时抛出。在你的情况下,你是铸造ViewPager2
至ViewPager
. 所以在xml文件中,需要使用androidx.viewpager.widget.ViewPager
. ?