Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash of android application on dynamic tablelayout creation

It seems to be I'm not the first person who have problems with dynamic creation of the TableLayOut. The problem is that when I add TableRow object into TableLayout my android application crashs and finishs with unexpected error.

What I want to do? I want to create dialog with next items layout

-------------------
|Custom |____|____| <-- Something like property grid
|graphic|____|____|
|view   |____|____|
|       |____|____|
-------------------

Close Button

In order to do it I'm using next method.

1. Creation of the DialogLayout class
2. Creation of the Dialog class

public class DialogLayout extends LinearLayout
{
   private CustomView m_view;
   private TableLayout m_layout;
   private TableRow m_row0;
   private TextView m_propName;
   private EditText m_propValue;

   public DialogLayout(Context context)
   {
        super(context);
        Init(context);
   }

   private void Init(Context context)
   {
      setOrientation(LinearLayout.HORIZONTAL);

      m_layout = new TableLayout(context);
      m_layout.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      m_layout.setStretchAllColumns(true);

      m_propName= new TextView(context);
      m_propName.setText("PropName:");

      m_propValue = new EditText(context);
      m_propValue.setText("--");

      m_row0 = new TableRow(context);
      m_row0.addView(m_propName);
      m_row0.addView(m_propValue);      
      m_layout.addView(m_row0, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

m_view= new CustomView(context);
        int h = (int) Math.min(m_layout.getHeight(), 600);
        if (h < 600)
            h = 600;
        LinearLayout.LayoutParams adapt = new LayoutParams(300, h);
        m_view.setLayoutParams(adapt);

        addView(m_view);
        addView(m_layout);

        setFocusable(true);
        setFocusableInTouchMode(true);
   }

}

Dialog class :

public class Dialog
{
        private Builder m_dialog = null;
        private DialogLayout m_layout = null;

        public Dialog(Context context)
        {
            m_layout = new DialogLayout(context);
            m_dialog = new AlertDialog.Builder(context);
            m_dialog.setTitle("My dialog");
            m_dialog.setView(m_layout);
            m_dialog.setNegativeButton("Close button", null);
        }

        public void show()
        {
        m_dialog.show(); // Seems to be it does not correctly calls m_dialog.show()!!!
        }
}

I checked next reasons of the application crash

  1. If my custom view is ok? - yes it is ok. It is not have any influence on the application behavior
  2. If there are any other reasons? - yes. It is strange, but app crashes only when I'm trying to put m_row0 into m_layout. But when I'm commenting code line

    m_layout.addView(m_row0, new TableLayout. LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Everything begin to work normal, but without the table.

Thank you in advance for any help.

LogCat:

12-12 15:00:45.899: W/dalvikvm(26467): threadid=1: thread exiting with uncaught exception (group=0x40e56930)
12-12 15:00:45.909: E/AndroidRuntime(26467): FATAL EXCEPTION: main
12-12 15:00:45.909: E/AndroidRuntime(26467): java.lang.ArithmeticException: divide by zero
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.TableLayout.mutateColumnsWidth(TableLayout.java:587)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.TableLayout.shrinkAndStretchColumns(TableLayout.java:576)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.TableLayout.measureVertical(TableLayout.java:474)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.TableLayout.onMeasure(TableLayout.java:439)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.View.measure(View.java:15513)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.View.measure(View.java:15513)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.View.measure(View.java:15513)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.View.measure(View.java:15513)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.View.measure(View.java:15513)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.View.measure(View.java:15513)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.View.measure(View.java:15513)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.View.measure(View.java:15513)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1064)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.Choreographer.doFrame(Choreographer.java:532)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.os.Handler.handleCallback(Handler.java:725)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.os.Looper.loop(Looper.java:137)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at java.lang.reflect.Method.invoke(Method.java:511)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-12 15:00:45.909: E/AndroidRuntime(26467):    at dalvik.system.NativeStart.main(Native Method)
like image 982
user1896908 Avatar asked Dec 12 '12 07:12

user1896908


1 Answers

You should use TableRow.LayoutParams instead of TableLayout.LayoutParams.

like image 89
s.maks Avatar answered Sep 30 '22 10:09

s.maks