[Java for Android App Development] swapnilraj:
I am trying to get a value that user enters into a Dialog, using the recommended DialogFragment class for it, the Dialog constructs and runs fine, but I cannot return the value of the EditText parameter to the parent class, without get a Null pointer exception.

My DialogHost class, this constructs, returns and links the parent to its buttons.

package jo.app.co;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;

public class DialogHost extends DialogFragment {

public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}

NoticeDialogListener mListener;

[MENTION=439709]override[/MENTION]
public void onAttach(Activity activity) {
super.onAttach(activity);

try {

mListener = (NoticeDialogListener) activity;

} catch (ClassCastException e) {

throw new ClassCastException(activity.toString());
}
}

[MENTION=439709]override[/MENTION]
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

LayoutInflater inflater = getActivity().getLayoutInflater();

builder.setView(inflater.inflate(R.layout.dialog_add, null))

.setPositiveButton("Save", new DialogInterface.OnClickListener() {

[MENTION=439709]override[/MENTION]
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick(DialogHost.this);

}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
DialogHost.this.getDialog().cancel();
}
});
return builder.create();
}

}

My MainActivity

package jo.app.co;

import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends FragmentActivity implements DialogHost.NoticeDialogListener {

[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showNoticeDialog();

}

public void showNoticeDialog() {

DialogFragment dialog = new DialogHost();
dialog.show(getFragmentManager(), "DialogHost");
}

[MENTION=439709]override[/MENTION]
public void onDialogPositiveClick(DialogFragment dialog) {

EditText myText = (EditText) findViewById(R.id.item_added);
try {
Log.d ("IN TRY", myText.getText().toString());
}
catch (Exception e) {
Log.e ("IN CATCH", e.toString());
}

}

[MENTION=439709]override[/MENTION]
public void onDialogNegativeClick(DialogFragment dialog) {
Log.d ("INMAIN", "REACHED NEG");
}

}

This is my layout for the add item dialog.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<EditText
android:id="@+id/item_added"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/hint_add_item" />

</LinearLayout>