我正在尝试动态更改我分配给应用后台的转换drawable的资源。我可以轻松地更改第一层(通过将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)