-1

I have bitmap scaled for show in TextView but how to add in TableLayout with row and coloumn?

    tbl = (TableLayout)findViewById(R.id.TableLayout1);
tblrow = new TableRow(this);


public void bukagambar(){
    bit = Bitmap.createScaledBitmap(bi, lebar, lebar, true);
    int lebar_tile = bit.getWidth()/ukurangrid;
    for(short baris=0 ;baris<ukurangrid;baris++){
        for(short kolom=0 ;kolom<ukurangrid;kolom++){
            bitm= Bitmap.createBitmap(bit, kolom*lebar_tile, baris*lebar_tile, lebar_tile, lebar_tile);
            bimg.setImageBitmap(bitm);
            tblrow.addView(bimg);
        }
        tbl.addView(tblrow);
    }   
}

and i got force close,sorry for bad english


logcat this one??

05-17 16:45:47.239: E/Trace(5339): error opening trace file: No such file or directory (2)
05-17 16:45:47.649: D/dalvikvm(5339): GC_FOR_ALLOC freed 3137K, 58% free 3606K/8387K, paused 31ms, total 31ms
05-17 16:45:54.479: D/dalvikvm(5339): GC_FOR_ALLOC freed 607K, 46% free 4554K/8387K, paused 18ms, total 27ms
05-17 16:45:54.489: D/AndroidRuntime(5339): Shutting down VM
05-17 16:45:54.489: W/dalvikvm(5339): threadid=1: thread exiting with uncaught exception (group=0x40b22300)
05-17 16:45:54.509: E/AndroidRuntime(5339): FATAL EXCEPTION: main
05-17 16:45:54.509: E/AndroidRuntime(5339): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.view.ViewGroup.addView(ViewGroup.java:3249)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.view.ViewGroup.addView(ViewGroup.java:3194)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.view.ViewGroup.addView(ViewGroup.java:3170)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at skripsi.slidame4.Slidame4.bukagambar(Slidame4.java:126)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at skripsi.slidame4.Slidame4.onOptionsItemSelected(Slidame4.java:89)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.app.Activity.onMenuItemSelected(Activity.java:2534)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:977)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:81)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:940)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.widget.AbsListView.performItemClick(AbsListView.java:1088)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2861)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.widget.AbsListView$1.run(AbsListView.java:3535)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.os.Handler.handleCallback(Handler.java:615)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.os.Looper.loop(Looper.java:137)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at android.app.ActivityThread.main(ActivityThread.java:4931)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at java.lang.reflect.Method.invoke(Method.java:511)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
05-17 16:45:54.509: E/AndroidRuntime(5339):     at dalvik.system.NativeStart.main(Native Method)
05-17 16:45:56.509: I/Process(5339): Sending signal. PID: 5339 SIG: 9

Slidame4.java code

public class Slidame4 extends Activity {

private Bitmap bit,bi,bitm;
private TableRow tblrow ;
private ImageView bimg;
private kotak hitam;
private TextView v ;
private TableLayout tbl;
public int ukurangrid;
private GameBoard coba;
private int lebar;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Resources r = getResources();
    bi = BitmapFactory.decodeResource(r, R.drawable.gambar2);
    tbl = (TableLayout)findViewById(R.id.TableLayout1);
    tblrow = new TableRow(this);
    v= new TextView(this);
    bimg = new ImageView(this);
    coba = new GameBoard();
    Display display = getWindowManager().getDefaultDisplay();
    lebar = (display.getWidth());       
    setContentView(R.layout.activity_slidame4);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.slidame4, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
    case R.id.hint:
        Toast.makeText(getApplicationContext(), ""+lebar, Toast.LENGTH_SHORT).show();
        break;
    case R.id.highscore:
        Toast.makeText(getApplicationContext(), "HIGH SCORE ", 3).show();
        break;
    case R.id.grid3:
        ukurangrid = 3;
        bukagambar();
        item.setChecked(true);
        break;
    case R.id.grid4:
        ukurangrid = 4;
        bukagambar();
        item.setChecked(true);
        break;
    case R.id.grid5:
        ukurangrid = 5;
        bukagambar();
        item.setChecked(true);
        break;

    default:
        return super.onOptionsItemSelected(item);

    }
        return false;

}

public void bukagambar(){
    tblrow.removeAllViews();
    bit = Bitmap.createScaledBitmap(bi, lebar, lebar, true);
    int lebar_tile = bit.getWidth()/ukurangrid;
    for(short baris=0 ;baris<ukurangrid;baris++){
        for(short kolom=0 ;kolom<ukurangrid;kolom++){
            bitm= Bitmap.createBitmap(bit, kolom*lebar_tile, baris*lebar_tile, lebar_tile, lebar_tile);
            bimg.setImageBitmap(bitm);
            tblrow.addView(bimg);
        }
    tbl.addView(tblrow);    
    }


}

I added removeallview but still get force close,

DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • post your logcat. On second thought: how big is your bitmap? How big is your grid's size? It looks like you're gonna have a lot of memory management problems – DigCamara May 16 '13 at 22:15
  • i set 'lebar' with width my screen get from 'getdefaultdisplay' and i got value 320. logcat?how to get that? sorry i newbie in android – user2375628 May 17 '13 at 09:28
  • Yep. That's the one. Now we'll need to see Slidame4.java (the error is on line 126) – DigCamara May 17 '13 at 14:50
  • line 126 `tblrow.addView(bimg);` – user2375628 May 17 '13 at 16:51
  • The error is telling you that the view (bimg) already belongs to another view. >>You must call removeView() on the child's parent first. – DigCamara May 17 '13 at 16:58

1 Answers1

0

Remove this line

bimg = new ImageView(this);

and add it under

int lebar_tile = bit.getWidth()/ukurangrid;
bimg = new ImageView(tblrow);

(your ImageView would be created as the child of the table row.

DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • cant put that syntac, im try `this.tbl.removeView(tblrow)` `this.tblrow.removeView(bimg);`; still force close – user2375628 May 17 '13 at 17:32
  • error in `vg.removeview(bimg)` `05-18 00:48:01.519: E/AndroidRuntime(11483): FATAL EXCEPTION: main 05-18 00:48:01.519: E/AndroidRuntime(11483): java.lang.NullPointerException 05-18 00:48:01.519: E/AndroidRuntime(11483): at skripsi.slidame4.Slidame4.bukagambar(Slidame4.java:116) ` its another way to show image in textview? – user2375628 May 17 '13 at 17:50
  • still forceclose `bimg = new ImageView(tblrow);` cant put tblrow in that way – user2375628 May 17 '13 at 18:25
  • really? can you post the logcat now that you've changed it? – DigCamara May 17 '13 at 18:27
  • logcat `E/AndroidRuntime(11483): FATAL EXCEPTION: main` and i got another way to show that image with some people source code and it sucess lol – user2375628 May 17 '13 at 21:28
  • Sorry, I didn't understand your last comment. Did you get it to work? – DigCamara May 17 '13 at 21:41
  • didn't work, forceclose `Fatal exception:main`(maybe im wrong in making imageview as child or else), but i got another way to show them, and its work.i got form code.google.com – user2375628 May 17 '13 at 21:57
  • Trouble is: FATAL EXCEPTION:main can have many reasons. You need to include a couple of lines more (at least) to really know what the problem is. But if you got it to work some other way, good for you! – DigCamara May 17 '13 at 21:59
  • thx for attention, i got many source code for showing them from code.google, now im trying to moving them with accelometer – user2375628 May 17 '13 at 22:02