似乎我不是第一个动态创建TableLayOut的人.问题是,当我将TableRow对象添加到TableLayout时,我的android应用程序崩溃并完成了意外错误.
我想做的事?
我想用下一个项目布局创建对话框
-------------------
|Custom |____|____|
|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);
}
}
对话类:
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()!!!
}
}
我检查了应用程序崩溃的下一个原因
>如果我的自定义视图没问题? – 是的,没关系.它对应用程序行为没有任何影响
>如果还有其他原因? – 是的这很奇怪,但只有当我试图将m_row0放入m_layout时app才会崩溃.但是当我在评论代码行时
m_layout.addView(m_row0,新的TableLayout.
LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
一切都开始正常,但没有桌子.
预先感谢您的任何帮助.
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)
解决方法:
您应该使用TableRow.LayoutParams而不是TableLayout.LayoutParams.
标签:android,tablelayout
来源: https://codeday.me/bug/0520/1143920.html