web-dev-qa-db-ja.com

CollapsingToolbarLayoutの膨張エラー

サポートデザインライブラリを最新バージョンに最近更新し、CollapsingToolbarLayoutを持つすべてのアクティビティで次の例外がスローされるようになりました。

    Java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Drawable;I)V in class Landroid/support/v4/graphics/drawable/DrawableCompat; or its super classes (declaration of 'Android.support.v4.graphics.drawable.DrawableCompat' appears in /data/data/com.radioafrica.music/files/instant-run/dex/slice-com.Android.support-support-v4-24.0.0-beta1_f8cf3ba4c70f87f407a745b9fa14a2205d0b587c-classes.dex)

完全なエラーログは次のとおりです。

Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.radioafrica.music/com.radioafrica.music.activity.PlaylistTracks}: Android.view.InflateException: Binary XML file line #31: Binary XML file line #31: Error inflating class Android.support.design.widget.CollapsingToolbarLayout at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2416)
    at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2476)
    at Android.app.ActivityThread.-wrap11(ActivityThread.Java) 
    at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1344) 
    at Android.os.Handler.dispatchMessage(Handler.Java:102) 
    at Android.os.Looper.loop(Looper.Java:148) 
    at Android.app.ActivityThread.main(ActivityThread.Java:5417) 
    at Java.lang.reflect.Method.invoke(Native Method) 
    at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:726) 
    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:616)
Caused by: Java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Drawable;I)V in class Landroid/support/v4/graphics/drawable/DrawableCompat; or its super classes (declaration of 'Android.support.v4.graphics.drawable.DrawableCompat' appears in /data/data/com.radioafrica.music/files/instant-run/dex/slice-com.Android.support-support-v4-24.0.0-beta1_f8cf3ba4c70f87f407a745b9fa14a2205d0b587c-classes.dex) 
    at Android.support.design.widget.CollapsingToolbarLayout.setStatusBarScrim(CollapsingToolbarLayout.Java:663) 
    at Android.support.design.widget.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.Java:197) 
    at Android.support.design.widget.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.Java:132)                                                                         
    at Java.lang.reflect.Constructor.newInstance(Native Method)                                                                          
    at Android.view.LayoutInflater.createView(LayoutInflater.Java:619)                                                                          
    at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:764)                                                                          
    at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:704)                                                                          
    at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:835)                                                                          
    at Android.view.LayoutInflater.rInflateChildren(LayoutInflater.Java:798)                                                                          
    at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:838)                                                                          
    at Android.view.LayoutInflater.rInflateChildren(LayoutInflater.Java:798)                                                                          
    at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:838)                                                                          
    at Android.view.LayoutInflater.rInflateChildren(LayoutInflater.Java:798)                                                                          
    at Android.view.LayoutInflater.inflate(LayoutInflater.Java:515)                                                                         
    at Android.view.LayoutInflater.inflate(LayoutInflater.Java:423)                                                                          
    at Android.view.LayoutInflater.inflate(LayoutInflater.Java:374)                                                                          
    at Android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.Java:280)                                                                          at Android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.Java:140)

私は問題が何であるか見つけることができないようです...

レイアウトファイルは次のとおりです。

`

<Android.support.design.widget.AppBarLayout
    Android:id="@+id/app_bar_layout"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:fitsSystemWindows="true"
    Android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <Android.support.design.widget.CollapsingToolbarLayout
        Android:id="@+id/collapsing_toolbar"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            Android:id="@+id/header"
            Android:layout_width="match_parent"
            Android:layout_height="280dip"
            Android:fitsSystemWindows="true"
            Android:scaleType="centerCrop"
            Android:src="@drawable/no_cover"
            Android:transitionName="avatar"
            app:layout_collapseMode="parallax" />

        <Android.support.v7.widget.Toolbar
            Android:id="@+id/toolbar"
            Android:layout_width="match_parent"
            Android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </Android.support.design.widget.CollapsingToolbarLayout>
</Android.support.design.widget.AppBarLayout>

<RelativeLayout
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <com.github.glomadrian.loadingballs.BallView
        Android:id="@+id/ballView"
        Android:layout_width="100dp"
        Android:layout_height="100dp"
        Android:layout_centerHorizontal="true"
        Android:layout_gravity="center"
        Android:layout_marginTop="150dip"
        lib:ball_colors="@array/color"
        lib:balls="5"
        lib:enable_size_animation="true"
        lib:max_ball_size="12dp"
        lib:min_ball_size="5dp"
        lib:movement_cycle_time="1500"
        lib:size_cycle_time="500" />

    <Android.support.v4.widget.NestedScrollView
        Android:id="@+id/nestedScrollView"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:clipToPadding="false"
        Android:visibility="gone"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:orientation="vertical">

            <Android.support.v7.widget.CardView xmlns:Android="http://schemas.Android.com/apk/res/Android"
                xmlns:card_view="http://schemas.Android.com/apk/res-auto"
                Android:layout_width="match_parent"
                Android:layout_height="wrap_content"
                card_view:cardCornerRadius="0dip"
                card_view:cardElevation="2dip"
                card_view:cardPreventCornerOverlap="true">

                <LinearLayout
                    Android:id="@+id/layout"
                    Android:layout_width="match_parent"
                    Android:layout_height="wrap_content"
                    Android:orientation="vertical"
                    Android:padding="10dip">

                    <TextView
                        Android:id="@+id/name"
                        Android:layout_width="wrap_content"
                        Android:layout_height="wrap_content"
                        Android:textAppearance="@style/TextAppearance.AppCompat.Large"
                        Android:textColor="#fff"
                        Android:layout_marginBottom="10dip"
                        Android:textStyle="bold" />

                    <TextView
                        Android:layout_width="wrap_content"
                        Android:layout_height="wrap_content"
                        Android:id="@+id/owner"
                        Android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                        Android:textColor="#e2e2e2" />
                </LinearLayout>

            </Android.support.v7.widget.CardView>

            <com.radioafrica.music.view.ExpandableHeightListView
                Android:id="@+id/top_tracks"
                Android:layout_width="match_parent"
                Android:layout_height="match_parent"
                Android:divider="#90ffffff"
                Android:dividerHeight="1dip"
                Android:focusable="false"
                Android:footerDividersEnabled="true"
                Android:headerDividersEnabled="true" />

        </LinearLayout>

    </Android.support.v4.widget.NestedScrollView>

</RelativeLayout>

<Android.support.design.widget.FloatingActionButton
    Android:id="@+id/floatingPlayButton"
    style="@style/FabStyle"
    app:layout_anchor="@id/app_bar_layout"
    app:layout_anchorGravity="bottom|right|end" />

<include layout="@layout/mini_player" />

`

アクティビティのコードは次のとおりです。

public class AlbumInfo extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

public static final String ALBUM = "album";

private ExpandableHeightListView expandableHeightListView;
private NestedScrollView nestedScrollView;
private ImageView header;
private BallView ballView;
private LinearLayout layout;
private LinearLayout miniPlayer;
private TextView title, artistTextView;
private ImageButton playPause;
private ImageView artwork;
private CollapsingToolbarLayout collapsingToolbarLayout;

ImageButton previous;
ImageButton next;
TextView name;
TextView owner;
FloatingActionButton fab;

String songTitle;
String singer;
String art;
boolean playing = false;

private ArrayList<Track> trackList;
private Album album;

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(UIEvent uiEvent) {
    miniPlayer.setVisibility(View.VISIBLE);
    miniPlayer.setClickable(true);

    songTitle = uiEvent.title;
    singer = uiEvent.artist;
    art = uiEvent.art;
    playing = uiEvent.playing;

    togglePlayButtonState();

    updateViews();
}

private void updateViews() {
    Picasso.with(this).load(art).fit().centerCrop().placeholder(R.drawable.no_cover).error(R.drawable.no_cover).into(artwork);

    if (NakedGroove.getMediaType().equals(NakedGroove.RADIO)) {
        artistTextView.setText(Html.fromHtml(singer));
        title.setText(songTitle);
    } else {
        artistTextView.setText(singer);
        title.setText(songTitle);
    }
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(TracksEvent tracksEvent) {
    trackList = tracksEvent.tracks;

    if (trackList.size() != 0) {
        ArtistTracksAdapter artistTracksAdapter = new ArtistTracksAdapter(AlbumInfo.this, R.layout.album_tracks_layout, trackList);
        expandableHeightListView.setAdapter(artistTracksAdapter);

        if (ballView != null) {
            ballView.setVisibility(View.GONE);
            nestedScrollView.setVisibility(View.VISIBLE);
        }
    }
}

private Target target = new Target() {

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        header.setImageBitmap(bitmap);
        setColors(bitmap);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        Picasso.with(AlbumInfo.this).load(album.getAlbumArt()).into(target);
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.no_cover);
        header.setImageBitmap(bitmap);
        setColors(bitmap);
    }
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    initActivityTransitions();
    setStatusBarColor();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.album);

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

    assert getSupportActionBar() != null;
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    album = getIntent().getParcelableExtra(ALBUM);

    fetchAlbumTracks(album.getId());

    expandableHeightListView = (ExpandableHeightListView) findViewById(R.id.top_tracks);
    nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);
    header = (ImageView) findViewById(R.id.header);
    ballView = (BallView) findViewById(R.id.ballView);
    name = (TextView) findViewById(R.id.name);
    owner = (TextView) findViewById(R.id.owner);
    layout = (LinearLayout) findViewById(R.id.layout);
    fab = (FloatingActionButton) findViewById(R.id.floatingPlayButton);
    collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);

    title = (TextView) findViewById(R.id.title);
    artistTextView = (TextView) findViewById(R.id.artist);
    previous = (ImageButton) findViewById(R.id.notification_base_previous);
    playPause = (ImageButton) findViewById(R.id.notification_base_play);
    next = (ImageButton) findViewById(R.id.notification_base_next);
    artwork = (ImageView) findViewById(R.id.notification_base_image);
    miniPlayer = (LinearLayout) findViewById(R.id.mini_player);

    collapsingToolbarLayout.setTitle(album.getTitle());
    collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(Android.R.color.transparent));

    owner.setText(album.getArtist());
    name.setText(album.getTitle());

    fab.setOnClickListener(this);
    fab.setRippleColor(getResources().getColor(R.color.app_theme_dark));

    miniPlayer.setOnClickListener(this);
    previous.setOnClickListener(this);
    next.setOnClickListener(this);
    playPause.setOnClickListener(this);

    expandableHeightListView.setExpanded(true);
    expandableHeightListView.setOnItemClickListener(this);

    if (album.getAlbumArt().length() != 0)
        Picasso.with(this).load(album.getAlbumArt()).into(target);
    else
        Picasso.with(this).load(R.drawable.no_cover).into(target);

    if (NakedGroove.isPlaying() || NakedGroove.isPaused()) {
        miniPlayer.setVisibility(View.VISIBLE);
        togglePlayButtonState();
    } else {
        miniPlayer.setVisibility(View.GONE);
    }
}

private void setStatusBarColor() {
    Window window = getWindow();
    if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21)
        window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    if (Build.VERSION.SDK_INT >= 21)
        getWindow().setStatusBarColor(getResources().getColor(Android.R.color.transparent));
}

private void fetchAlbumTracks(String id) {
    startService(new Intent(this, RefresherService.class).setAction(RefresherService.FETCH_ALBUM_TRACKS).putExtra(RefresherService.ALBUM_ID, id));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case Android.R.id.home:
            super.onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onPause() {
    super.onPause();
}

@Override
protected void onResume() {
    super.onResume();

    togglePlayButtonState();

    if (NakedGroove.getMediaType() != null && !NakedGroove.getMediaType().equals(NakedGroove.NOTHING)) {
        updateViews();
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(ALBUM, album.getTitle());
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    NakedGroove.setCurrentPlaylist(trackList);
    NakedGroove.setCurrentIndex(position);

    if (Build.VERSION.SDK_INT >= 16)
        startService(new Intent(this, PlayerService.class).setAction(PlayerService.ACTION_URL));
    else
        startService(new Intent(this, MusicService.class).setAction(MusicService.ACTION_URL));
}

private void playPauseMusic() {
    if (NakedGroove.isPlaying()) {
        if (Build.VERSION.SDK_INT >= 16)
            startService(new Intent(this, PlayerService.class).setAction(PlayerService.ACTION_PAUSE));
        else
            startService(new Intent(this, MusicService.class).setAction(MusicService.ACTION_PAUSE));
    } else {
        if (Build.VERSION.SDK_INT >= 16)
            startService(new Intent(this, PlayerService.class).setAction(PlayerService.ACTION_PLAY));
        else
            startService(new Intent(this, MusicService.class).setAction(MusicService.ACTION_PLAY));
    }
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.mini_player:
            startActivity(new Intent(this, MusicPlayer.class));
            break;
        case R.id.notification_base_play:
            playPauseMusic();
            break;
        case R.id.notification_base_next:
            if (Build.VERSION.SDK_INT >= 16)
                startService(new Intent(this, PlayerService.class).setAction(PlayerService.ACTION_SKIP));
            else
                startService(new Intent(this, MusicService.class).setAction(MusicService.ACTION_SKIP));
            break;
        case R.id.notification_base_previous:
            if (Build.VERSION.SDK_INT >= 16)
                startService(new Intent(this, PlayerService.class).setAction(PlayerService.ACTION_PREVIOUS));
            else
                startService(new Intent(this, MusicService.class).setAction(MusicService.ACTION_PREVIOUS));
            break;
        case R.id.floatingPlayButton:
            if (trackList.size() != 0) {
                NakedGroove.setCurrentPlaylist(trackList);
                NakedGroove.setCurrentIndex(0);

                if (Build.VERSION.SDK_INT >= 16)
                    startService(new Intent(this, PlayerService.class).setAction(PlayerService.ACTION_URL));
                else
                    startService(new Intent(this, MusicService.class).setAction(MusicService.ACTION_URL));
            }
            break;
    }
}

private void togglePlayButtonState() {
    if (playing) {
        playPause.setImageResource(R.drawable.ic_music_pause);
    } else {
        playPause.setImageResource(R.drawable.ic_music_play);
    }
}

private void initActivityTransitions() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Lollipop) {
        ChangeBounds transition = new ChangeBounds();
        transition.excludeTarget(Android.R.id.statusBarBackground, true);
        getWindow().setEnterTransition(transition);
        getWindow().setReturnTransition(transition);
    }
}

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

@Override
protected void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
protected void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}

private void setColors(Bitmap bitmap) {
    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
        @Override
        public void onGenerated(Palette palette) {
            if (palette.getVibrantSwatch() != null) {
                Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();
                float[] vibrant = vibrantSwatch.getHsl();

                nestedScrollView.setBackgroundColor(Color.HSVToColor(vibrant));
                layout.setBackgroundColor(Color.HSVToColor(vibrant));
                collapsingToolbarLayout.setContentScrimColor(Color.HSVToColor(vibrant));
                collapsingToolbarLayout.setStatusBarScrimColor(Color.HSVToColor(vibrant));
            } else if (palette.getDarkVibrantSwatch() != null) {
                Palette.Swatch darkVibrantSwatch = palette.getDarkVibrantSwatch();
                float[] darkVibrant = darkVibrantSwatch.getHsl();

                nestedScrollView.setBackgroundColor(Color.HSVToColor(darkVibrant));
                layout.setBackgroundColor(Color.HSVToColor(darkVibrant));
                collapsingToolbarLayout.setContentScrimColor(Color.HSVToColor(darkVibrant));
                collapsingToolbarLayout.setStatusBarScrimColor(Color.HSVToColor(darkVibrant));
            } else if (palette.getDarkMutedSwatch() != null) {
                Palette.Swatch darkMutedSwatch = palette.getDarkMutedSwatch();
                float[] darkMuted = darkMutedSwatch.getHsl();

                nestedScrollView.setBackgroundColor(Color.HSVToColor(darkMuted));
                layout.setBackgroundColor(Color.HSVToColor(darkMuted));
                collapsingToolbarLayout.setContentScrimColor(Color.HSVToColor(darkMuted));
                collapsingToolbarLayout.setStatusBarScrimColor(Color.HSVToColor(darkMuted));
            } else {
                nestedScrollView.setBackgroundColor(getResources().getColor(R.color.DeepGrey));
                layout.setBackgroundColor(getResources().getColor(R.color.DeepGrey));
                collapsingToolbarLayout.setContentScrimColor(getResources().getColor(R.color.DeepGrey));
                collapsingToolbarLayout.setStatusBarScrimColor(getResources().getColor(R.color.DeepGrey));
            }

            supportStartPostponedEnterTransition();
        }
    });
}

}

21
Alex Kombo

私はここで同様の投稿に答えました クラスCollapsingToolbarLayoutを膨らませるエラー

私はすでに受け入れられた答えがあることを知っていますが、それは機能していません(または機能していません)。

このエラーの調査に数時間費やしましたが、私が思いついたのはこれです。

まず、Android Nougat Android 7.0(API 24)compileSdkVersion 24が利用可能になりました。これを行うには、更新する必要があります。 sdk。 ステップ1

enter image description here

2つ目は、gradleファイルを更新することです(インターネット接続が必要です)。 ステップ2

enter image description here

App.gradleのこの部分も更新します

enter image description here

3番目(オプション)クリーンプロジェクト、ビルドプロジェクト(両方とも[ビルド]タブの下)

これが誰かを助けて、数時間の研究を節約することを願っています。

乾杯/ハッピーコーディング

それが役に立てば幸い。

30
ralphgabb

ソリューションが見つかりました。追加するだけ

app:statusBarScrim="@null"

collapsingToolbarLayoutに

36
Igor

サポートライブラリが競合する別の依存関係があったことがわかりました。依存関係を削除すると、プロジェクトが正常にビルドおよび実行されました。

5
Alex Kombo

すべてのサポートライブラリを24.0.0-beta1 バージョン。次に、cleanrebuild、およびrunをもう一度プロジェクトに追加します。これで問題が解決するはずです。

1
S. Brukhanda

com.Android.support:design依存関係を他のサポートライブラリと同じバージョンに更新してみてください。私は自分のプロジェクトでこの問題を解決しました。サポートデザインライブラリを使用するライブラリを使用しているため、build.gradleに明示的に追加しませんでした。 GradleタスクandroidDependenciesは、他のライブラリの更新後もサポートデザインのバージョンがまだ低いことを示していたため、明示的に追加し、魅力のように機能しました。

0
Anton Tarasov