android - 尝试将编辑文本转换为字符串, "name".getText().toString() 行出错

标签 android android-edittext gettext

我正在尝试使用新窗口并附加编辑文本以从用户那里获取名称。它总是在遇到“”name”.getText().toString()”

行时失败

这是我的主要内容

public class chatter extends Activity {

private String name;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.setNameMenu:
        setname();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

public void setname()
{
    final EditText textNameInput = (EditText) findViewById(R.id.nameBox);
    /*Dialog dialog = new Dialog(chatter.this);
    dialog.setTitle("This is my custom dialog box");
    */
    setContentView(R.layout.custom_dialog);
    //dialog.setCancelable(true);
    Button button = (Button) findViewById(R.id.submitNameButton);
    button.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View v) {
            //String nameinput = textNameInput.getText().toString();
            **//this is where it does not work**
        **Editable debug1 = textNameInput.getText();**  
        String nameinput = debug1.toString();
            name = nameinput;
            finish();
        }
    });
}
}

这是布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<TextView
    android:id="@+id/dialogueText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="please input your name:"
    />
<EditText 
    android:id="@+id/nameBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"       
    android:layout_below="@+id/dialogueText" />
<Button
    android:text=" Set "
    android:id="@+id/submitNameButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/nameBox"
    android:layout_alignParentRight="true"
    />
</RelativeLayout>

我是 android 编程的新手,请解释哪里出了问题。

最佳答案

由于您还没有发布堆栈跟踪信息,所以我猜测是错误,但我认为您得到的是 NullPointerException,因为 textNameInputnull 。您也没有说您在代码中引用的两种布局中的哪一种是您列出的布局,但我猜它是 R.layout.custom_dialog?如果是这样,那么您对 ​​findViewById(R.id.nameBox) 的调用将返回 null,因为它发生在您加载包含 R.id 的布局之前。名称框。加载布局后将调用移至。

关于android - 尝试将编辑文本转换为字符串, "name".getText().toString() 行出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5054580/

相关文章:

android - 有没有办法对 setRotation 进行动画处理,或者应该将其替换为 RotateAnimation?

android - 多 Intent 处理由于方向改变

android - 如何修复 "TextInputLaout with TextInputEditText view I can not implement to show next focus Edittext using imeOption and nextFocustDown attribute"

android - 在android中更改文本编辑错误通知字体

PHP gettext 和 vagrant 运行 ubuntu

android - Android应用程序随机崩溃

android - AndEngine游戏优化

Android Phone EditText 未更新设备上的显示

php - Gettext 缓存的烦恼

django - 找不到 msguniq。 Django 1.8,Windows 7 64 位