我的应用不断崩溃(致命异常)

时间:2018-08-15 18:34:25

标签: android android-studio crash android-tablayout crash-reports

我的应用程序通常以初始屏幕打开,但无法进入主屏幕并崩溃! 我正在尝试进行布局(代码中没有错误)

这是通过调试导致的错误:

  

E / Android运行时:致命异常:主要                     流程:com.foot.ws.worldcup,PID:10249                     java.lang.RuntimeException:无法启动活动ComponentInfo {com.foot.ws.worldcup / com.foot.ws.worldcup.MainActivity}:android.view.InflateException:二进制XML文件行#11:二进制XML文件行11 :错误膨胀类android.support.design.widget.AppBarLayout                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2904)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2979)                         在android.app.ActivityThread.-wrap11(未知来源:0)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1643)                         在android.os.Handler.dispatchMessage(Handler.java:105)                         在android.os.Looper.loop(Looper.java:180)                         在android.app.ActivityThread.main(ActivityThread.java:6944)                         在java.lang.reflect.Method.invoke(本机方法)                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)                      原因:android.view.InflateException:二进制XML文件第11行:二进制XML文件第11行:错误膨胀了类android.support.design.widget.AppBarLayout                      原因:android.view.InflateException:二进制XML文件行#11:错误膨胀了类android.support.design.widget.AppBarLayout                      造成原因:java.lang.reflect.InvocationTargetException                         在java.lang.reflect.Constructor.newInstance0(本地方法)                         在java.lang.reflect.Constructor.newInstance(Constructor.java:334)                         在android.view.LayoutInflater.createView(LayoutInflater.java:647)                         在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)                         在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)                         在android.view.LayoutInflater.rInflate(LayoutInflater.java:863)                         在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:515)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:423)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:374)                         在com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:422)                         在android.app.Activity.setContentView(Activity.java:2665)                         在com.foot.ws.worldcup.MainActivity.onCreate(MainActivity.java:19)                         在android.app.Activity.performCreate(Activity.java:6986)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1232)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2857)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2979)                         在android.app.ActivityThread.-wrap11(未知来源:0)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1643)                         在android.os.Handler.dispatchMessage(Handler.java:105)                         在android.os.Looper.loop(Looper.java:180)                         在android.app.ActivityThread.main(ActivityThread.java:6944)                         在java.lang.reflect.Method.invoke(本机方法)                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)                      原因:java.lang.IllegalArgumentException:您需要在设计库中使用Theme.AppCompat主题(或后代)。                         在android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)                         在android.support.design.widget.AppBarLayout。(AppBarLayout.java:158)                         在java.lang.reflect.Constructor.newInstance0(本地方法)                         在java.lang.reflect.Constructor.newInstance(Constructor.java:334)                         在android.view.LayoutInflater.createView(LayoutInflater.java:647)                         在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)                         在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)                         在android.view.LayoutInflater.rInflate(LayoutInflater.java:863)                         在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:515)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:423)                         在android.view.LayoutInflater.inflate(LayoutInflater.java:374)                         在com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:422)                         在android.app.Activity.setContentView(Activity.java:2665)                         在com.foot.ws.worldcup.MainActivity.onCreate(MainActivity.java:19)                         在android.app.Activity.performCreate(Activity.java:6986)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1232)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2857)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2979)                         在android.app.ActivityThread.-wrap11(未知来源:0)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1643)                         在android.os.Handler.dispatchMessage(Handler.java:105)                         在android.os.Looper.loop(Looper.java:180)                         在android.app.ActivityThread.main(ActivityThread.java:6944)                         在java.lang.reflect.Method.invoke(本机方法)                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                         com.android.internal.os.ZygoteInit.main(ZygoteInit.java:835)

这是主要内容:(包括进口商品)

public class MainActivity extends FragmentActivity {
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tabLayout = findViewById(R.id.tablayout_id);
    appBarLayout = findViewById(R.id.appbarid);
    viewPager = findViewById(R.id.viewpager_id);
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.AddFragment(new FragmentQuiz(),"Quiz");
    adapter.AddFragment(new FragmentExplore(), "Explore");
    adapter.AddFragment(new FragmentStore(), "Store");

    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
}}

xml:

<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">


<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    app:elevation="0dp"
    android:id="@+id/appbarid">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:src="@drawable/image" />

</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabGravity="fill"
    app:tabMode="fixed"
    android:id="@+id/tablayout_id"
    app:tabTextColor="@color/tabtextcolor"
    app:tabIndicatorColor="@color/tabindicatorcolor"
    android:background="@color/tab"/>
<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewpager_id"
    >
</android.support.v4.view.ViewPager>

1 个答案:

答案 0 :(得分:0)

您需要在 AndroidManifest.xml 中为您的应用程序定义主题,例如:

<application
.....
        android:theme="@style/Theme">

并在 values / styles.xml 文件中定义此样式,例如:

<style name="Theme" parent="Theme.AppCompat.Light">
.....
</style>