vendredi 7 mars 2014

TextView returns NPE in for dialog


Vote count:

0




I have a method to show dialog and it contains a textview inside it. So I am trying to call that textview to set its text liketvD.setText("SELECT"). But for some reason the app is giving me NPE. Here is my code


This is prompts.xml



<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >

<TextView
android:id="@+id/tvD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="PICK"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>


And this is the method alertDialog



public void alertDialog() {
final Context context = this;

// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
final TextView tvDialog = (TextView) findViewById(R.id.tvD);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertDialog builder
alertDialogBuilder.setView(promptsView);
// set its text
tvDialog.setText("HELLO");
// set dialog message
alertDialogBuilder.setCancelable(false).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

}


Why am I getting this NPE?



asked 20 secs ago






Aucun commentaire:

Enregistrer un commentaire