启动以下活动时,我获得了ClassCastException
。
我的布局:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bhupendra.myapplication.NewDialogsActivity"/>
我的活动课程:
public class NewDialogsActivity extends ActionBarActivity {
private SectionsPagerAdapter sectionsPagerAdapter;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
List<Fragment> tabs = new ArrayList<Fragment>();
tabs.add(UserFragment.getInstance());
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), tabs);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
}
public static class SectionsPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return "Users";
}
}
@Override
public void onBackPressed() {
Intent i = new Intent(NewDialogsActivity.this, DialogsActivity.class);
startActivity(i);
finish();
}
}
我在我的代码中使用了这个主题
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
</style>
logcat的
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.support.v7.internal.widget.ActionBarOverlayLayout$LayoutParams cannot be cast to com.android.internal.widget.ActionBarOverlayLayout$LayoutParams
at com.android.internal.widget.ActionBarOverlayLayout.applyInsets(ActionBarOverlayLayout.java:147)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:292)
at android.view.View.measure(View.java:16030)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2196)
at android.view.View.measure(View.java:16030)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1290)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5508)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
有人可以帮我解决这段代码中的错误。
minSdk=14
targetSdk=21
答案 0 :(得分:2)
原因如下:
java.lang.ClassCastException:android。 support.v7 .internal.widget.ActionBarOverlayLayout $ LayoutParams 无法施展 融为一体的机器人强> .internal.widget.ActionBarOverlayLayout $的LayoutParams
所以基本上你不应该混合&#34;本地&#34; ActionBar支持lib的一个。选择并使用其中一个或另一个。很可能您的代码中的import
不正确,因此请检查您的来源import com.android.internal.widget.ActionBarOverlayLayout;
左右,然后删除,然后添加正确的import android.support.v7.internal.widget.ActionBarOverlayLayout;
或使用&#34;整理导入&#34; IDE的功能,然后确保使用支持lib的相关功能(IDE应该询问)
答案 1 :(得分:0)
尝试扩展AppCompatActivity。它正确处理应用栏