johnbhk
johnbhk

Reputation: 15

Nine patch applied programmatically hangs app

I have asked this question before but without a trace or answer. Sorry if I missed something.

I do a table layout of a 2D array of EditText programmatically. If I set the background color the orders are wiped out. I understand the solution is a nine patch. I have made several which work well in XML layouts but hang the app when applied programmatically as follows:

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);

The program compiles but hangs. Commenting out ninenine it works but no borders.

log.txt which I do not understand:

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'

Is this problem between source and compiled nine patch?

Upvotes: 1

Views: 1377

Answers (1)

Squonk
Squonk

Reputation: 48871

What is the version of Android which you're using to test when you get that error?

setBackground(Drawable background) is only available from API 16 onwards. If the minSdkVersion is lower than that but targetSdkVersion is 16 it will compile OK but will fail on lower Android versions at run-time.

In that case, use setBackgroundDrawable(Drawable background) instead.

Upvotes: 9

Related Questions