为工具栏膨胀类时出错

时间:2016-04-06 08:04:12

标签: android styles android-toolbar

我正在使用我的应用程序的工具栏它在棒棒糖版本中工作正常。但在之前的版本中,应用程序崩溃了。我尝试将样式更改为 Theme.AppCompat.NoActionBar但它没有用。

样式:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>
<style name="AppThemes" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/windowBackground</item>
</style>

XmlToolbar:

<?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/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

logcat的:

java.lang.RuntimeException: Unable to start activity ComponentInfo{nidhinkumar.webviewss/nidhinkumar.webviewss.ParseMainAct}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        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:829)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
        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
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:816)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:305)
        at android.app.Activity.setContentView(Activity.java:1966)
        at nidhinkumar.webviewss.ParseMainAct.onCreate(ParseMainAct.java:36)
        at android.app.Activity.performCreate(Activity.java:5286)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)

ParseMainAct:

public class ParseMainAct extends Activity{
private static String TAG = ParseMainAct.class.getSimpleName();

private Toolbar mToolbar;
private ListView listView;
private List<Message> listMessages = new ArrayList<>();
private MessageAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.parseact_main);
    listView = (ListView) findViewById(R.id.list_view);
    mToolbar = (Toolbar) findViewById(R.id.toolbar);


    adapter = new MessageAdapter(this);


    listView.setAdapter(adapter);

    Intent intent = getIntent();




}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String message = intent.getStringExtra("message");

    Message m = new Message(message, System.currentTimeMillis());
    listMessages.add(0, m);
    adapter.notifyDataSetChanged();
}

private class MessageAdapter extends BaseAdapter {

    LayoutInflater inflater;

    public MessageAdapter(Activity activity) {
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return listMessages.size();
    }

    @Override
    public Object getItem(int position) {
        return listMessages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = inflater.inflate(R.layout.parselist_row, null);
        }

        TextView txtMessage = (TextView) view.findViewById(R.id.message);
        TextView txtTimestamp = (TextView) view.findViewById(R.id.timestamp);

        Message message = listMessages.get(position);
        txtMessage.setText(message.getMessage());

        CharSequence ago = DateUtils.getRelativeTimeSpanString(message.getTimestamp(), System.currentTimeMillis(),
                0L, DateUtils.FORMAT_ABBREV_ALL);

        txtTimestamp.setText(String.valueOf(ago));

        return view;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement

    return super.onOptionsItemSelected(item);
}

}

parse_main.xml:

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/my_tool_bar" />
</LinearLayout>

<ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"></ListView>

3 个答案:

答案 0 :(得分:1)

你必须为lollipop下面的设备制作两个styles.xml文件,其他的是用于设备棒棒糖及以上版本的styles.xml(v-21)。 style.xml的示例

<style name="MyAppThemes" parent="MyTheme.Base">
<!-- Customize your theme here. -->
</style>

<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
 <item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/windowBackground</item>
</style>

style.xml的样本(v-21)

 <style name="MyAppThemes" parent="MyAppThemes.Base">
<item name="android:windowActionBar">false</item>
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/windowBackground</item>
</style>

现在在AndroidMainfest.xml中更改应用程序主题

 android:theme="@style/MyAppThemes">

我希望它能解决你的问题。

答案 1 :(得分:0)

您应该在布局中创建toolbar.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="xxxxxx">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <include
            android:id="@+id/main_menu_activity_toolbar"
            layout="@layout/XmlToolbar" />

然后给你的活动充气:

mToolbar = (Toolbar) findViewById(R.id.main_menu_activity_toolbar);

修改

在基本应用程序主题中使用NoActionBar

<!-- Base application theme. -->

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>

答案 2 :(得分:0)

可能在您的应用AppTheme或“AppThemes”中使用过。您看到您已将父主题设为Theme.AppCompat.Light.DarkActionBar。这意味着您拥有此主题的活动有ActionBar

只要有ToolBar,就无法使用ActionBar

只需更改Theme.AppCompat.Light.DarkActionBar

Theme.AppCompat.Light.NoActionBar。你很高兴。

希望这会对你有所帮助。

<强>更新

ToolBar设为ActionBar

mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);