gpt4 book ai didi

java - 在 Android 应用程序中获取 ClassCastException

转载 作者:行者123 更新时间:2023-11-30 08:57:39 27 4
gpt4 key购买 nike

启动以下 Activity 时,我收到了 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"/>

我的 Activity 类:

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>

日志

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

最佳答案

原因如下:

java.lang.ClassCastException: android.support.v7.internal.widget.ActionBarOverlayLayout$LayoutParams cannot be cast to com.android.internal.widget.ActionBarOverlayLayout$LayoutParams

所以基本上您不应该将“原生”ActionBar 与支持库的 ActionBar 混合使用。选择并使用其中一个。很可能你的代码中的 import 不正确,因此请检查你的源代码是否有 import com.android.internal.widget.ActionBarOverlayLayout; 左右并删除然后添加正确的 import android.support.v7.internal.widget.ActionBarOverlayLayout; 或者使用你的 IDE 的“organize imports”功能,然后确保使用支持库的相关功能(IDE 会询问)

关于java - 在 Android 应用程序中获取 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28047380/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com