5

似乎我不是第一个遇到动态创建 TableLayOut 问题的人。问题是,当我将 TableRow 对象添加到 TableLayout 中时,我的 android 应用程序崩溃并以意外错误结束。

我想做的事?我想用下一个项目布局创建对话框

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

关闭按钮

为了做到这一点,我正在使用下一个方法。

1. DialogLayout 类的创建
2. Dialog 类的创建

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()!!!
        }
}

我检查了应用程序崩溃的下一个原因

  1. 如果我的自定义视图没问题?- 是的,没关系。它对应用程序行为没有任何影响
  2. 如果还有其他原因?- 是的。这很奇怪,但只有当我尝试m_row0放入m_layout. 但是当我评论代码行时

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

一切都开始正常工作,但没有桌子。

预先感谢您的任何帮助。

日志猫:

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

2 回答 2

6

您应该使用TableRow.LayoutParams而不是TableLayout.LayoutParams.

于 2013-04-03T08:08:12.050 回答
-1

你有一个数学问题:

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

这说明了一些你被零除的地方。

由于您不需要进行很多数学运算,我猜这个问题来自这里:

int h = (int) Math.min(m_layout.getHeight(), 600);

所以我的猜测是m_layout.getHeight()返回零。

于 2013-04-03T08:13:50.693 回答