开发者

Trying to convert Edit text to String, got error with "name".getText().toString() line

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/submitNameBut开发者_如何学JAVAton"
    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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜