尝试将编辑文本转换为字符串,“name”.getText().toString() 行出现错误

发布于 2024-10-18 11:59:04 字数 2490 浏览 1 评论 0原文

我正在尝试使用新窗口并附加编辑文本来从用户那里获取名称。当它到达““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中编程的新手,请解释一下发生了什么错误的。

I am trying to use the new window and attaching edittext to get name from the user. It just always fail when it hits the line " "name".getText().toString()"

Here is my main

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();
        }
    });
}
}

Here is the layout

<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>

I am new to programing in android, please kindly explain what went wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-10-25 11:59:04

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

Since you haven't posted a stacktrace I'm guessing at the error, but I'm thinking you are getting a NullPointerException as textNameInput is null. You also haven't said which of the two layouts that you refer to in your code is the one that you have listed, but I'm guessing its R.layout.custom_dialog? If so, then your call to findViewById(R.id.nameBox) will return null as it happens before you have loaded the layout that contains R.id.nameBox. Move the call to after you load the layout.

⒈起吃苦の倖褔 2024-10-25 11:59:04

您在调用 setContentView(R.layout.custom_dialog); 之前调用 final EditText textNameInput = (EditText) findViewById(R.id.nameBox);

You're calling final EditText textNameInput = (EditText) findViewById(R.id.nameBox); before you have called setContentView(R.layout.custom_dialog);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文