尝试将编辑文本转换为字符串,“name”.getText().toString() 行出现错误
我正在尝试使用新窗口并附加编辑文本来从用户那里获取名称。当它到达““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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您尚未发布堆栈跟踪,我猜测该错误,但我认为您会收到
NullPointerException
因为textNameInput
为null
。您还没有说您在代码中引用的两种布局中的哪一种是您列出的,但我猜它是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
astextNameInput
isnull
. 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 itsR.layout.custom_dialog
? If so, then your call tofindViewById(R.id.nameBox)
will returnnull
as it happens before you have loaded the layout that containsR.id.nameBox
. Move the call to after you load the layout.您在调用
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 calledsetContentView(R.layout.custom_dialog);