Android的警告对话框无法找到视图 [英] Android Alert Dialog unable to find view

查看:179
本文介绍了Android的警告对话框无法找到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法得到一个AlertDialog来传递回文本调用它的活动。看来问题是没有找到合适的EditText调用findViewByID的时候,但我是新来的Andr​​oid和不知道为什么,可能是。

I'm having trouble getting an AlertDialog to pass text back to the activity that calls it. It seems the issue is that it fails to find the proper EditText when calling findViewByID, but I'm new to Android and don't know why that may be.

在code是如下:

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:

And the corresponding 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被发现?我能做些什么,使这项工作为目的,通过新的字符串返回到活动呢?

Why isn't the editText being found? What can I do to make this work as intended, passing the new string back to the activity?

在先进的感谢。

推荐答案

修改

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

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

的EditText modify_dialog.xml ,所以你需要使用已充入的变量布局(这里 modifyView )找到 ID 不是布局 getActivty()将查找

Your EditText lives in modify_dialog.xml so you need to use the variable that was inflated with that layout (here modifyView) to find the id not the layout that getActivty() will look in.

这篇关于Android的警告对话框无法找到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆