如何将文本设置到对话框中的EditText中?

7
你可以看到,我的问题是在showDialog()之前无法动态设置edittext的值。
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
 import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {
private static final int DIALOG_TEXT_ENTRY = 7;

private int user_id;
private int dialogChoice;
private String mobileNum;
private EditText input2 ;
private EditText input1 ;
public TextView textView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alert_dialog_text_entry);
    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
     mobileNum =tm.getLine1Number();
    // input1.setText(mobileNum);   
     textView.setText("hello");
    showDialog(DIALOG_TEXT_ENTRY);

}
@Override
 protected Dialog onCreateDialog(int i) {


    switch (i) {

    case DIALOG_TEXT_ENTRY:
            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
            input2 = (EditText) textEntryView.findViewById(R.id.number_edit);
            input1 = (EditText) textEntryView.findViewById(R.id.username_edit);
            textView = (TextView) textEntryView.findViewById(R.id.username_view);
            return new AlertDialog.Builder(MainActivity.this)
               // .setIconAttribute(android.R.attr.accountType)
                .setTitle(R.string.alert_dialog_text_entry)
                .setView(textEntryView)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                          Intent i = new Intent(MainActivity.this, IccActivity.class);
                          startActivity(i);
                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                })
                .create();
    }
    return null;
 }

}

textView.setText("hello"); this line kills the app.

thanks in advance.

4个回答

1

你应该将这行代码 textView.setText("hello"); 放到 onCreateDialog() 方法中,因为你在初始化之前设置了值。


谢谢。它可以工作了。我之前一直在尝试在获取引用之前分配一个值。 - akd
现在你应该接受这个答案,因为它会保持未回答状态。 - Vineet Shukla

1

您尚未初始化textView。在执行textView.setText()之前,您需要在onCreate()中这样做

textView =(TextView)textEntryView.findViewById(R.id.username_view);


0

我猜你遇到了NullPointerException?你应该先设置你的'textView'字段。


0

查找对话框中的TexView文本,并设置TextView的文本 尝试这个link


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接