Android Alert Dialog无法找到视图

时间:2013-09-18 16:49:41

标签: java android android-edittext android-dialogfragment

我无法使用AlertDialog将文本传回给调用它的活动。似乎问题是它在调用findViewByID时找不到合适的EditText,但我是Android新手,不知道为什么会这样。

代码如下:

public class ModifyDialogFragment extends DialogFragment {

/* The activity that creates an instance of this dialog fragment must
 * implement this interface in order to receive event callbacks.
 * Each method passes the DialogFragment in case the host needs to query it. */
public interface MDialogListener {
    public void onMDialogPositiveClick(String newValue);
}

// Use this instance of the interface to deliver action events
MDialogListener mListener;

String mEntryName = "";
EditText mEditText;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    final View modifyView = inflater.inflate(R.layout.modify_dialog, null);

    builder.setView(modifyView);

    final EditText editText = (EditText) getActivity().findViewById(R.id.modificationText);



           builder.setPositiveButton(R.string.modify, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   mListener.onMDialogPositiveClick(editText.getText().toString());
               }
           });
           builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });

    // Create the AlertDialog object and return it
    return builder.create();
}

// Override the Fragment.onAttach() method to instantiate the ModifyDeleteDialogListener
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the MDDialogListener so we can send events to the host
        mListener = (MDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement MDialogListener");
    }
}

和相应的modify_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/modificationText"
    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"/>

为什么找不到editText?我可以做些什么来使这个工作按预期工作,将新字符串传递回活动?

4 个答案:

答案 0 :(得分:10)

更改

final EditText editText = (EditText) getActivity().findViewById(R.id.modificationText);

final EditText editText = (EditText) modifyView.findViewById(R.id.modificationText);

您的EditText位于modify_dialog.xml,因此您需要使用因layout(此处modifyView)而膨胀的变量来查找id而不是layout getActivty()将查看的{{1}}。

答案 1 :(得分:2)

您对广告进行了充气,​​并且您拥有此builder.setView(modifyView);

所以初始化edittext replace

 final EditText editText = (EditText)getActivity().findViewById(R.id.modificationText);

通过

 final EditText editText = (EditText) modifyViewfindViewById(R.id.modificationText);

findViewById查找当前膨胀布局中提供了id的视图。您不需要getActivity而是使用已充实的视图对象来初始化您的EditText。

public final Activity getActivity ()

Added in API level 11
Return the Activity this fragment is currently associated with.

答案 2 :(得分:1)

那是因为您正在使用modifyView.findView ...

从活动中搜索视图

答案 3 :(得分:0)

就是这个

EditText editText = (EditText) modifyView.findViewById(R.id.modificationText);

问题是什么?

在活动中,您只需findViewById您想要的任何视图,这些视图指的是活动布局。 但是在dialogdialogFragments或自定义视图中,您必须手动引用当前布局,例如

view.findViewById(...)

由于您的代码指的是其父活动,因此应用尝试在活动中查找ID,但实际上您的ID属于自定义视图modifyView

因此,这是您的代码找到该ID的正确方法。