简体   繁体   English

自定义对话框上的android tableLayout

[英]android tableLayout on a custom dialog

I am using a custom dialog from my customdialog.xml that contains a tableLayout filled with row.xml. 我正在使用customdialog.xml中的自定义对话框,其中包含一个填充了row.xml的tableLayout。 Normaly this code works on an activity class but, as a dialog, when I add a tableLayout and insert a row, app crashes. Normaly此代码适用于活动类,但作为对话框,当我添加tableLayout并插入行时,应用程序崩溃。 How to reach it? 怎么到达?

Code: 码:

private void customDialog(){

        // custom dialog
                    final Dialog dialog = new Dialog(context);
                    dialog.setContentView(R.layout.customdialog);


                    TableRow row = (TableRow)LayoutInflater.from(this).inflate(R.layout.row, null);

                    ((TextView)row.findViewById(R.id.textNom)).setText("testing...");

                    TableLayout llistaLlistes = (TableLayout)this.findViewById(R.id.llistaLlistes);

                    llistaLlistes.addView(row);

                    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);

                    dialogButton.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });

                    dialog.show();

    }

crash log: 崩溃日志:

11-25 18:39:57.509: E/AndroidRuntime(27839): FATAL EXCEPTION: main
11-25 18:39:57.509: E/AndroidRuntime(27839): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appdev.stpro/com.appdev.stpro.StPROActivity}: java.lang.NullPointerException
11-25 18:39:57.509: E/AndroidRuntime(27839):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at android.os.Looper.loop(Looper.java:137)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at android.app.ActivityThread.main(ActivityThread.java:4441)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at java.lang.reflect.Method.invoke(Method.java:511)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at dalvik.system.NativeStart.main(Native Method)
11-25 18:39:57.509: E/AndroidRuntime(27839): Caused by: java.lang.NullPointerException
11-25 18:39:57.509: E/AndroidRuntime(27839):    at com.appdev.stpro.StPROActivity.customDialog(StockPROActivity.java:236)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at com.appdev.stpro.StPROActivity.onCreate(StockPROActivity.java:212)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at android.app.Activity.performCreate(Activity.java:4465)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-25 18:39:57.509: E/AndroidRuntime(27839):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)

if layout is necessary please let me know. 如果需要布局,请告诉我。

you can always go the otherwa around 你可以随时去其他地方

Create a class and xml for your tablelayout. 为tablelayout创建一个类和xml。 then in the android manifest add this on the activity: 然后在android清单中添加这个活动:

android:theme="@android:style/Theme.Dialog"

now just get the onclicklistener to start the activity instead of the dialog! 现在只需让onclicklistener启动活动而不是对话框!

I had some kind of problems when i tried to programmaticaly add row to a table accordingly to a datatab. 当我尝试使用programmaticaly将数据行添加到表中时,我遇到了一些问题。

I managed to do it with horizontal linear layout created in a loop instead of a table. 我设法在循环而不是表格中创建水平线性布局。

Dialogs in Android work differently than dialogs in core Java. Android中的对话框与核心Java中的对话框不同。 You should extend DialogFragments to create custom layouts. 您应该扩展DialogFragments以创建自定义布局。 The Android Dialog Design Guide shows you how to do dialogs with custom layouts: http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout Android对话框设计指南向您展示如何使用自定义布局进行对话: http//developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM