在Android中输入类android.support.v7.widget.Toolbar时出错

时间:2016-05-17 14:22:39

标签: android toolbar appcompatactivity

我看了很多SO问题并尝试实施它们。似乎无法想象这一个,它在昨天工作,但现在不再。

的gradle:

android {
compileSdkVersion 23
buildToolsVersion "23.0.0 "

dexOptions {
    javaMaxHeapSize "4g"
}

defaultConfig {
    minSdkVersion 16
    multiDexEnabled true
    targetSdkVersion 23
}
dependencies {
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'com.android.support:design:23+'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'com.android.support:support-v4:24.0.0-alpha2'
compile 'com.github.fernandodev.easyratingdialog:easyratingdialog:1.1.0'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.parse:parse-android:1.+'
compile 'com.squareup:otto:1.3.8'
compile 'com.moengage:moe-android-sdk:6.0.10'
compile 'com.amazonaws:aws-android-sdk-mobileanalytics:2.2.9'


compile 'org.apmem.tools:layouts:1.9@aar'

compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.crashlytics.sdk.android:answers:1.3.6@aar') {
    transitive = true;
}
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
    transitive = true;
}
}

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>

android:background="@color/colorPrimary"
android:elevation="4dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="@dimen/abc_action_bar_default_height_material"
android:theme="@style/ThemeOverlay.AppCompat.Dark"/>

BaseActivity.class

public abstract class BaseActivity extends AppCompatActivity {
private MoEHelper mHelper;
public static MobileAnalyticsManager analytics;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mHelper = new MoEHelper(this);


    try {
        analytics = MobileAnalyticsManager.getOrCreateInstance(
                this.getApplicationContext(),
                "", //Amazon Mobile Analytics App ID
                "" //Amazon Cognito Identity Pool ID
        );
    } catch(InitializationException ex) {
        Log.e(this.getClass().getName(), "Failed to initialize Amazon Mobile Analytics", ex);
    }



}

@Override
protected void onStart() {
    super.onStart();
    mHelper.onStart(this);
}

@Override
protected void onStop() {
    super.onStop();
    mHelper.onStop(this);


}

@Override
protected void onPause() {
    super.onPause();
    mHelper.onPause(this);
    if(analytics != null) {
        analytics.getSessionClient().pauseSession();
        //Attempt to send any events that have been recorded to the Mobile Analytics service.
        analytics.getEventClient().submitEvents();
    }
}

@Override
protected void onResume() {
    super.onResume();
    mHelper.onResume(this);
    if(analytics != null)  {
        analytics.getSessionClient().resumeSession();
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mHelper.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    mHelper.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    mHelper.onNewIntent(this, intent);
}
}

活动类

public abstract class Activity extends BaseActivity
{
protected Toolbar toolbar;

// region Activity Lifecycle

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(getContentView());

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null)
    {
        setSupportActionBar(toolbar);
    }
}

// endregion

// region Getters/Setters

protected String getActionBarTitle()
{
    String actionBarTitle = "";

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null && actionBar.getTitle() != null)
    {
        actionBarTitle = actionBar.getTitle().toString();
    }

    return actionBarTitle;
}

protected void setActionBarTitle(int resourceId)
{
    String actionBarTitle = getString(resourceId);
    setActionBarTitle(actionBarTitle);
}

protected void setActionBarTitle(String actionBarTitle)
{
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null)
    {
        if (!StringUtilities.isNullOrEmpty(actionBarTitle))
        {

            SpannableString spannableString = new SpannableString(actionBarTitle);
            spannableString.setSpan(new TypefaceSpan("sans-serif-light"), 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            actionBar.setTitle(spannableString);
            actionBar.setDisplayShowTitleEnabled(true);
        }
        else
        {
            actionBar.setDisplayShowTitleEnabled(false);
        }
    }
}

// endregion

abstract protected int getContentView();
}

activity_main.xml中

logcat的

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:640)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:904)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.app.Activity.onCreate(Activity.java:24)
        at com.app.DrawerActivity.onCreate(DrawerActivity.java:51)
        at com.app.MainActivity.onCreate(MainActivity.java:86)
        at android.app.Activity.performCreate(Activity.java:6221)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
        at android.app.ActivityThread.access$900(ActivityThread.java:172)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5835)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
        at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

3 个答案:

答案 0 :(得分:1)

这是专门为我工作的。我的问题与主题无关,而是依赖项和targetSdkVersion的正确编译版本。更新了下面的gradle文件。改变了

Original Gradle

compile 'com.android.support:support-v4:24.0.0-alpha2'

更改:

compile 'com.android.support:support-v4:23.0.0'

基本上问题是我的这个版本的库与其他版本和minsdkversion不匹配,所以有些东西没有很好地融合。一旦我更新了所有依赖项(特别是来自谷歌设计库)以相互匹配,它就有效了。

答案 1 :(得分:0)

我过去也遇到过类似的问题。 android:theme是问题,在工具栏上或在AppBarLayout上。

编辑:代码示例

<android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

选项2:

<style name="MyToolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:background">@color/colorPrimary</item>
</style>

答案 2 :(得分:0)

<style name="AppTheme" parent="@style/Theme.AppCompat.NoActionBar">
  <item name="windowActionBar">false</item>
  <item name="android:windowNoTitle">true</item>
</style>

尝试使用此主题剪辑