CollapsingToolbarLayout 不起作用:无法实例化以下 类 和 java.lang.NoSuchMethodError

CollapsingToolbarLayout doesn't work: The following classes could not be instantiated and java.lang.NoSuchMethodError

每当我尝试在设备上打开我的应用程序时,CollapsingToolbarLayout 都会崩溃,我不知道为什么会出现这个问题。 这是问题所在:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.projectz.teamrocket.thebusapp/com.android.projectz.teamrocket.thebusapp.activity.DetailViewerActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class android.support.design.widget.CollapsingToolbarLayout

这是 android studio 中抛出的另一个错误(应用程序设计预览)

java.lang.NoSuchMethodError: android.support.v4.graphics.drawable.DrawableCompat.setLayoutDirection(Landroid/graphics/drawable/Drawable;I)V

xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="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="com.android.projectz.teamrocket.thebusapp.activity.DetailViewerActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbarDetailViewer"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


<android.support.v4.view.ViewPager 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/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.android.projectz.sciacallo.thebusapp.com.android.projectz.teamrocket.thebusapp.activity.DetailViewerActivity"
    tools:showIn="@layout/activity_detail_viewer">

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@color/colorPrimaryDark"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff"
        app:layout_behavior="" />

</android.support.v4.view.ViewPager>

<View
    android:id="@+id/shadowView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F2FFFFFF"
    android:visibility="invisible" />

<com.getbase.floatingactionbutton.FloatingActionsMenu
    android:id="@+id/fab_normal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    fab:fab_addButtonColorNormal="@color/colorPrimaryDark"
    fab:fab_addButtonColorPressed="@color/colorPrimary"
    fab:fab_addButtonPlusIconColor="@color/white"
    fab:fab_labelStyle="@style/menu_labels_style">

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_favorit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        fab:fab_icon="@drawable/ic_star_off"
        fab:fab_colorNormal="@color/colorFAB2"
        fab:fab_colorPressed="@color/colorFAB2pressed"
        fab:fab_size="mini"
        fab:fab_title="@string/fab_favorit" />

    <com.getbase.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        fab:fab_icon="@drawable/ic_share"
        fab:fab_colorNormal="@color/colorFAB1"
        fab:fab_colorPressed="@color/colorFAB1pressed"
        fab:fab_size="mini"
        fab:fab_title="@string/fab_share" />

</com.getbase.floatingactionbutton.FloatingActionsMenu>

<!--
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_favorit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="160dp"
    android:layout_marginRight="@dimen/fab_margin"
    android:src="@drawable/ic_star_off"
    android:visibility="invisible"
    app:backgroundTint="@color/colorFAB2"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_share"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="90dp"
    android:layout_marginRight="@dimen/fab_margin"
    android:src="@drawable/ic_message"
    android:visibility="invisible"
    app:backgroundTint="@color/colorFAB1"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_normal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_more"
    app:backgroundTint="@color/colorPrimaryDark"
    app:elevation="6dp"
    app:pressedTranslationZ="12dp" />
-->

build.grandle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion '24.0.0'

defaultConfig {
    applicationId "com.android.projectz.teamrocket.thebusapp"
    minSdkVersion 17
    targetSdkVersion 23
    versionCode 1
    versionName "0.6"
    vectorDrawables.useSupportLibrary = true
}
...

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/commons-net-3.5.jar')
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:support-v4:23.3.0'

compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
compile 'com.github.paolorotolo:appintro:4.1.0'
compile 'com.afollestad.material-dialogs:commons:0.9.1.0'
compile 'jp.wasabeef:recyclerview-animators:1.3.0'
compile 'com.getbase:floatingactionbutton:1.10.1'
testCompile 'junit:junit:4.12'
}

最后是 style.xml 文件:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="AppTheme.ToolbarTheme" parent="ThemeOverlay.AppCompat.Dark" />

<style name="AppTheme.Toolbar" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="InputTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/yellow</item>
</style>

<style name="menu_labels_style">
    <item name="android:background">@drawable/fab_label_background</item>
    <item name="android:textColor">@color/white</item>
</style>

这是 jva 文件:

package com.android.projectz.teamrocket.thebusapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;    
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.Toast;

import com.android.projectz.teamrocket.thebusapp.R;
import com.android.projectz.teamrocket.thebusapp.util.ShareUtil;


public class DetailViewerActivity extends AppCompatActivity implements View.OnClickListener {

private static final String TAG = DetailViewerActivity.class.getSimpleName();
boolean fabStatus = false;
SectionsPagerAdapter sectionsPagerAdapter;
ViewPager viewPager;
private boolean isFavorit = false;
private String busID;
private ShareUtil share;
private Boolean isFabOpen = false;
private Animation fab_open, fab_close, rotate_forward, rotate_backward;
Animation show_fab_1, hide_fab_1;
private com.getbase.floatingactionbutton.FloatingActionButton fabNormal;
private com.getbase.floatingactionbutton.FloatingActionButton fabShare;
private com.getbase.floatingactionbutton.FloatingActionButton fabFavorit;
private View mShadowView;

public Animation fadeIn, fadeOut;


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

    DetailViewerActivity.this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left);

    Toolbar toolbarDetail = (Toolbar) findViewById(R.id.toolbarDetailViewer);
    setSupportActionBar(toolbarDetail);
    getSupportActionBar().setTitle("");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_in);

    fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_out);

    share = new ShareUtil();

    //fabNormal = (com.getbase.floatingactionbutton.FloatingActionButton) findViewById(R.id.fab_normal);
    fabShare = (com.getbase.floatingactionbutton.FloatingActionButton) findViewById(R.id.fab_share);
    fabFavorit = (com.getbase.floatingactionbutton.FloatingActionButton) findViewById(R.id.fab_favorit);



    fab_open = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
    fab_close = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_close);
    rotate_forward = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_forward);
    rotate_backward = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_backward);

    mShadowView = findViewById(R.id.shadowView);
    //fabNormal.setOnClickListener(this);
    fabShare.setOnClickListener(this);
    fabFavorit.setOnClickListener(this);

    if (isFavorit) {
        fabFavorit.setSize(com.getbase.floatingactionbutton.FloatingActionButton.SIZE_MINI);
        fabFavorit.setIcon(R.drawable.ic_star_on);
        fabFavorit.setStrokeVisible(false);
        fabFavorit.refreshDrawableState();
    } else {
        fabFavorit.setSize(com.getbase.floatingactionbutton.FloatingActionButton.SIZE_MINI);
        fabFavorit.setIcon(R.drawable.ic_star_off);
        fabFavorit.setStrokeVisible(false);
        fabFavorit.refreshDrawableState();
    }


    sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(sectionsPagerAdapter);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // write your code here
        this.finish();
        DetailViewerActivity.this.overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_right);
    }
    return true;
}

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id) {
        case R.id.fab_share: // fab per lo sharing della tratta:
            /**
             * testo:
             * Stò guardando questa tratta: <nome-tratta>
             * anche te puoi farlo, scarica gratuitamente l'app...
             */
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, TAG + ": test_share_fab_share_button_tragitto");
            startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_title)));
            break;
        case R.id.fab_favorit: // fab per il set favorit della tratta
            /**
             * qui bisogna fare i controlli con il database interno per verificare la
             * veridicità dei dati: se la tratta è tra i favorit dell'utente
             */
            if (isFavorit == true) {
                isFavorit = false;
                fabFavorit.setIcon(R.drawable.ic_star_on);
                Snackbar snackbar = Snackbar
                        .make(v, "Percorso salvato nei preferiti", Snackbar.LENGTH_LONG)
                        .setAction("INDIETRO", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Snackbar snackbar1 = Snackbar.make(view, "Percorso eliminato", Snackbar.LENGTH_SHORT);
                                snackbar1.show();
                                fabStatus = false;
                                fabFavorit.setImageResource(R.drawable.ic_star_off);
                                fabFavorit.refreshDrawableState();
                            }
                        });

                snackbar.show();
                //Toast.makeText(DetailViewerActivity.this, String.valueOf(isFavorit), Toast.LENGTH_SHORT).show();
            } else {
                isFavorit = true;
                fabFavorit.setIcon(R.drawable.ic_star_off);
                //Toast.makeText(DetailViewerActivity.this, String.valueOf(isFavorit), Toast.LENGTH_SHORT).show();
            }
            fabFavorit.refreshDrawableState();
            break;
    }
}



public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase();
            case 1:
                return getString(R.string.title_section2).toUpperCase();
            case 2:
                return getString(R.string.title_section3).toUpperCase();
        }
        return null;
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            this.finish();
            DetailViewerActivity.this.overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_right);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Create a new TextView and set its text to the fragment's section
        // number argument value.
        View view = null;
        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 1: /* visualizzazione dei dettaglio del viaggio */
                view = getTravelView(inflater);
                break;
            case 2: /* visualizzazione dei servizi disponibili (città) */
                view = getServiceView(inflater);
                break;
            case 3: /* visualizzazione delle pubblicità dei qualiasi cosa (supermercati, scarpe, viagra ...) */
                view = getAdvertisingView(inflater);
                break;
        }
        return view; //ritornare un layout
    }

    public View getTravelView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.travel_layout, null);
    }

    public View getServiceView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.service_layout, null);
    }

    public View getAdvertisingView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.advertising_layout, null);
    }
}

public boolean isBusFavorit() {
    return isFavorit;
}

public void setFavorit(boolean fav) {
    isFavorit = fav;
}
}
compile ('com.android.support:support-v4:23.4.0'){
    force = true;
}

在您的 gradle 文件中使用它。

像这样更新清单

compile 'com.android.support:support-v4:23.3.0'
    {
        force = true;
    }

请注意,由于设计库依赖于支持 v4 和 AppCompat 支持库,因此当您添加设计库依赖项时,它们将自动包含在内。所以你应该只使用相同的 versions.Its 冲突问题 -

compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'

compile 'com.android.support:design:23.3.0' //23.4.0


//try to delete these below libraries and see if it works
compile 'com.android.support:appcompat-v7:23.3.0' //23.4.0
compile 'com.android.support:support-v4:23.3.0' //23.4.0

好的,我通过更改 SDK 的版本解决了问题,我将 sdk 库版本从 23.4.0 更改为 24.0.0,它可以工作。 我关注这个 post @KarandeepAtwal