在Dialog中使用listview自定义布局

时间:2014-05-06 07:33:25

标签: java android listview dialog

我想做一个对话框,其中包含带有EditText和两个Button的ListView,一个用于转到其他对话框,另一个用于验证。 我用Listite填充我的ListView,并且数据加载得很好,但是当我将适配器设置为listView时我有错误。

这是我的代码:

public void createDialog(){

    // custom dialog
    ArrayList<String> list_datas = new ArrayList<String>();

    ContractProductQuery contractProductQuery = new ContractProductQuery(this);
    contractProductQuery.open();        
    list_datas = contractProductQuery.getProductInContract(this.producer.getId(), this.point.getId());
    contractProductQuery.close();

    final Dialog dialog = new Dialog(ViewPdf.this);
    dialog.setContentView(R.layout.list_product); <--- i load my custom layout
    final ListView list_view = (ListView) findViewById(R.id.list_prod); <-- this is the listview id of my custom layout that loaded before
    final EditText quantity_product = (EditText) findViewById(R.id.quantity_product); <-- and the EditText to getValue after


    dialog.setTitle("Produit présent dans le contrat");                     
    ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_datas);
    list_view.setAdapter(modeAdapter); -<< IT'S HERE WHERE I HAVE NULLPOINTEREXECPTION

    Button dialog_button_return = (Button) dialog.findViewById(R.id.button_return);     
    dialog_button_return.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    Button dialog_button_validate = (Button) dialog.findViewById(R.id.button_validate);     
    dialog_button_validate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });     
    dialog.show();

}

和tis是我的错误:

05-06 07:24:37.868: E/AndroidRuntime(953): FATAL EXCEPTION: main
05-06 07:24:37.868: E/AndroidRuntime(953): java.lang.NullPointerException
05-06 07:24:37.868: E/AndroidRuntime(953):  at com.pdf.ViewPdf.test(ViewPdf.java:774)
05-06 07:24:37.868: E/AndroidRuntime(953):  at     com.pdf.ViewPdf$2.onClick(ViewPdf.java:174)
05-06 07:24:37.868: E/AndroidRuntime(953):  at android.view.View.performClick(View.java:4084)
05-06 07:24:37.868: E/AndroidRuntime(953):  at android.view.View$PerformClick.run(View.java:16966)
05-06 07:24:37.868: E/AndroidRuntime(953):  at android.os.Handler.handleCallback(Handler.java:615)
05-06 07:24:37.868: E/AndroidRuntime(953):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 07:24:37.868: E/AndroidRuntime(953):  at android.os.Looper.loop(Looper.java:137)
05-06 07:24:37.868: E/AndroidRuntime(953):  at android.app.ActivityThread.main(ActivityThread.java:4745)
05-06 07:24:37.868: E/AndroidRuntime(953):  at java.lang.reflect.Method.invokeNative(Native Method)
05-06 07:24:37.868: E/AndroidRuntime(953):  at     java.lang.reflect.Method.invoke(Method.java:511)
05-06 07:24:37.868: E/AndroidRuntime(953):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-06 07:24:37.868: E/AndroidRuntime(953):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-06 07:24:37.868: E/AndroidRuntime(953):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您必须使用dialog.findViewById,例如:

 final ListView list_view = (ListView) dialog.findViewById(R.id.list_prod);