0

我正在尝试动态更改分配给应用程序背景的过渡可绘制资源。我可以轻松更改第一层(通过将 getId 更改为 1 或 0),但是当我尝试更改第二层(通过将 getId 增加到 2)时,应用程序崩溃。任何人都可以帮忙吗?

    TransitionDrawable td = new TransitionDrawable(new Drawable[]{getDrawable(R.drawable.beach),getDrawable(R.drawable.animal)});


    RelativeLayout rl = (RelativeLayout) findViewById(R.id.background);
    if (mInputText.getText().toString().contains("money")){
        rl.setBackground(td);
        td.setCrossFadeEnabled(true);
        td.setDrawableByLayerId(td.getId(2),ContextCompat.getDrawable(this,R.drawable.money));
        td.startTransition(3000);

    }
}

这是logcat的输出

FATAL EXCEPTION: main
                                                 Process: com.joel808.flashchatnewfirebase, PID: 5534
                                                 java.lang.IndexOutOfBoundsException
                                                     at android.graphics.drawable.LayerDrawable.getId(LayerDrawable.java:548)
                                                     at com.joel808.flashchatnewfirebase.MainChatActivity.newtransition(MainChatActivity.java:193)
                                                     at com.joel808.flashchatnewfirebase.MainChatActivity$1.onEditorAction(MainChatActivity.java:119)
                                                     at android.widget.TextView.onEditorAction(TextView.java:5597)
                                                     at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
                                                     at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:364)
                                                     at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:91)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6688)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)                                  
4

0 回答 0