1

我以前问过这个问题,但没有任何痕迹或答案。对不起,如果我错过了什么。

我以编程方式对 EditText 的二维数组进行表格布局。如果我设置背景颜色,订单就会消失。我知道解决方案是九个补丁。我已经制作了几个在 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);

程序编译但挂起。注释掉ninenine 它可以工作,但没有边界。

我不明白的 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'

是源码和编译的九个补丁之间的问题吗?

4

1 回答 1

9

当您收到该错误时,您用于测试的 Android 版本是什么?

setBackground(Drawable background)仅从 API 16 开始可用。如果minSdkVersion低于该值但targetSdkVersion为 16,它将可以编译,但在运行时在较低的 Android 版本上会失败。

在这种情况下,请setBackgroundDrawable(Drawable background)改用。

于 2012-11-19T01:46:08.623 回答