You have to use dialog.findViewById, for example:
final ListView list_view = (ListView) dialog.findViewById(R.id.list_prod);
Domanda
I would like to do a dialog which contains a ListView with an EditText and two Button , one to go to other dialog and the other to validate. I fill my ListView with SQLite, and the datas are loaded well but i have an error when i set the adapter to my listView..
This is my code :
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();
}
and tis is my error :
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)
Soluzione
You have to use dialog.findViewById, for example:
final ListView list_view = (ListView) dialog.findViewById(R.id.list_prod);