我之前已经问过这个问题,但没有追查或回答。对不起,如果我错过了什么。
我以编程方式对EditText的2D数组进行表格布局。如果我设置背景颜色,订单将被清除。我知道解决方案是九个补丁。我已经制作了几个在XML布局中运行良好的应用程序,但在以编程方式应用时挂起应用程序,如下所示:
TableLayout tl = new TableLayout(this);
// tl.setBackgroundColor(0xff498263);
TableRow tr[] = new TableRow[9];
NinePatchDrawable ninenine = (NinePatchDrawable)
getResources().getDrawable(R.drawable.ninenine) ;
// Toast.makeText(this, "getResources is not null",
// Toast.LENGTH_LONG).show();
int x, y;
for (x = 0; x < 9; x++) {
tr[x] = new TableRow(this);
for (y = 0; y < 9; y++) {
entry[x][y] = new EditText(this);
entry[x][y].setText("");
entry[x][y].setWidth(52);
entry[x][y].setEnabled(true);
entry[x][y].setClickable(false);
entry[x][y].setInputType(2);
// entry[x][y].setBackground(ninenine);
entry[x][y].setBackgroundColor(0x880000ff);
程序编译但挂起。评论九号它有效但无国界。
我不明白的log.txt:
11-13 10:33:29.730: I/dalvikvm(2155): Could not find method android.widget.EditText.setBackground, referenced from method com.example.test_layout.MainActivity.onCreate
11-13 10:33:29.730: W/dalvikvm(2155): VFY: unable to resolve virtual method 2897: Landroid/widget/EditText;.setBackground (Landroid/graphics/drawable/Drawable;)V
11-13 10:33:29.730: D/dalvikvm(2155): VFY: replacing opcode 0x6e at 0x00b4
11-13 10:33:29.730: D/dalvikvm(2155): VFY: dead code 0x00b7-0113 in Lcom/example/test_layout/MainActivity;.onCreate (Landroid/os/Bundle;)V
11-13 10:33:29.790: D/dalvikvm(2155): GC_EXTERNAL_ALLOC freed 42K, 45% free 2995K/5379K, external 0K/0K, paused 48ms
11-13 10:33:29.810: D/AndroidRuntime(2155): Shutting down VM
11-13 10:33:29.810: W/dalvikvm(2155): threadid=1: thread exiting with uncaught exception (group=0x40139578)
11-13 10:33:29.810: E/AndroidRuntime(2155): FATAL EXCEPTION: main
11-13 10:33:29.810: E/AndroidRuntime(2155): java.lang.NoSuchMethodError: android.widget.EditText.setBackground
11-13 10:33:29.810: E/AndroidRuntime(2155): at com.example.test_layout.MainActivity.onCreate(MainActivity.java:73)
11-13 10:33:29.810: E/AndroidRuntime(2155): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-13 10:33:29.810: E/AndroidRuntime(2155): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-13 10:33:29.810: E/AndroidRuntime(2155): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-13 10:33:29.810: E/AndroidRuntime(2155): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-13 10:33:29.810: E/AndroidRuntime(2155): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-13 10:33:29.810: E/AndroidRuntime(2155): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 10:33:29.810: E/AndroidRuntime(2155): at android.os.Looper.loop(Looper.java:130)
11-13 10:33:29.810: E/AndroidRuntime(2155): at android.app.ActivityThread.main(ActivityThread.java:3691)
11-13 10:33:29.810: E/AndroidRuntime(2155): at java.lang.reflect.Method.invokeNative(Native Method)
11-13 10:33:29.810: E/AndroidRuntime(2155): at java.lang.reflect.Method.invoke(Method.java:507)
11-13 10:33:29.810: E/AndroidRuntime(2155): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-13 10:33:29.810: E/AndroidRuntime(2155): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-13 10:33:29.810: E/AndroidRuntime(2155): at dalvik.system.NativeStart.main(Native Method)
11-13 10:33:31.810: I/dalvikvm(2155): threadid=4: reacting to signal 3
11-13 10:33:31.830: I/dalvikvm(2155): Wrote stack traces to '/data/anr/traces.txt'
源代码和编译后的九个补丁有问题吗?
答案 0 :(得分:9)
当您收到错误时,您用来测试Android的版本是什么?
setBackground(Drawable background)
仅适用于API 16及更高版本。如果minSdkVersion
低于targetSdkVersion
但setBackgroundDrawable(Drawable background)
为16则会编译正常但在运行时较低的Android版本上会失败。
在这种情况下,请改用{{1}}。