setContentView

时间:2017-06-02 15:53:34

标签: android oncreate illegalstateexception setcontentview

在调用IllegalStateException时,我在onCreate()方法中使用setContentView()

某些特定设备发生异常。大多数情况下,在Android 4.4版设备上。在其他设备上正常工作。

的onCreate():

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

    viewPager = (ViewPager)findViewById(R.id.viewpager);
    tabLayout = (TabLayout)findViewById(R.id.sliding_tabs);

    CategoryAdapter categoryAdapter = new CategoryAdapter(this, getSupportFragmentManager());
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"/>

</android.support.v4.widget.DrawerLayout>

堆栈跟踪:

java.lang.RuntimeException: 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method:0)
at java.lang.reflect.Method.invoke(Method.java:515)
at 


 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run 
 (ZygoteInit.java:1291)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
 at dalvik.system.NativeStart.main(Native Method:0)
Caused by: java.lang.IllegalStateException: 
 at 

 android.support.v7.app.AppCompatDelegateImplV9.createSubDecor 
 (AppCompatDelegateImplV9.java:359)
 at 
 android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor 
(AppCompatDelegateImplV9.java:328)
at 
android.support.v7.app.AppCompatDelegateImplV9.setContentView 
(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView 
(AppCompatActivity.java:140)
at easyapps.com.HomeActivity.onCreate(HomeActivity.java:138)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate 
(Instrumentation.java:1093)
at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2368)

0 个答案:

没有答案