【问题标题】:the app crashes and the error is in the toolbar in android应用程序崩溃,错误出现在 android 的工具栏中
【发布时间】:2015-12-14 04:22:28
【问题描述】:

我的应用程序在打开时崩溃,logcat 说生成 ToolBar 小部件时出错,但我在旧应用程序上做了它并且它有效,我不知道错误在哪里,请告知我缺少什么

MainActivity.java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

Toolbar tb;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //connect the toolbar to the activity//
    tb = (Toolbar) findViewById(R.id.appbar);
    //tell the app that we will use this toolbar not his own//
    setSupportActionBar(tb);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.igh_eg.bplus.MainActivity">

<include layout="@layout/app_bar" android:id="@+id/appbar"/>
<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/appbar"/>

</RelativeLayout>

appbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/primaryColor"
app:theme="@style/mycustomtoolbar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
xmlns:android="http://schemas.android.com/apk/res/android">

</android.support.v7.widget.Toolbar>

Logcat:

09-17 12:10:30.445    3869-3869/com.igh_eg.bplus E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.igh_eg.bplus, PID: 3869
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.igh_eg.bplus/com.igh_eg.bplus.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
        at android.app.ActivityThread.access$800(ActivityThread.java:163)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5335)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar

【问题讨论】:

  • 你必须使用支持工具栏的主题
  • 你的 style.xml 中的风格是什么?

标签: android android-widget material-design android-toolbar android-logcat


【解决方案1】:

请检查值文件夹中的您的 Styles.xml 是否已定义,并在 Manifiest 中检查应用的主题

你可以从中取样

   <style name="AppTheme" parent="android:Theme.Material.Light">
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <item name="android:colorAccent">@color/accent</item>
    <item name="android:navigationBarColor">@color/primary_dark</item>
    <item name="android:displayOptions">disableHome</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style>

【讨论】:

  • 我正在使用 Theme.AppCompat.NoActionBar 并且它在另一个应用程序上工作
【解决方案2】:

感谢大家的帮助,我刚刚发现 colors.xml 文件被放入了 Values-V22 资源文件而不是正常的资源文件中,我实际上是在 API 19 上构建的

所以每当我构建应用程序时,它都看不到工具栏或任何东西的颜色并崩溃:)

【讨论】:

    【解决方案3】:

    而不是在包含标记处提供 id 更改您的两个布局,如下所示

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       tools:context="com.igh_eg.bplus.MainActivity">
    
    <include layout="@layout/app_bar" />
    
    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appbar"/>
    
    </RelativeLayout>
    

    appbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/appbar" 
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/primaryColor"
      app:theme="@style/mycustomtoolbar"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">
    
    </android.support.v7.widget.Toolbar>
    

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      相关资源
      最近更新 更多