Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) User Input Getting Text from an EditText

Parag Panchal
Parag Panchal
3,388 Points

Help please - Entering StartButton related java code breaks my app.

adding StartButton related code generates error while running on emulator. Error Message displayed is "Unfortunately, Interactive Story has stopped".

I tried following,

1 declaring mStartButton as private shows that - Private field 'mStartButton' is never assigned. so I declared it public and message goes away.

Here is my MainActivity.java code:

package au.com.relteksys.interactivestory;

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;

public class MainActivity extends Activity {

private EditText mNameField; private Button mStartButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNameField = (EditText)findViewById(R.id.nameEditText);

    mStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name = mNameField.getText().toString();
            Toast.makeText(MainActivity.this, name,    Toast.LENGTH_LONG).show();
        }
    });
}

}

2 Answers

Jack Middlebrook
Jack Middlebrook
19,746 Points

Before the setOnClickListener for mStartButton you need to set the mStartButton.

Below where you set mNameField you can also set the Button so your code should have:

mNameField = (EditText)findViewById(R.id.nameEditText);
mStartButton = (Button) findViewById(R.id.startButton);

Hope that helps.

Parag Panchal
Parag Panchal
3,388 Points

Thanks a lot Jack. It's resolved

Parag Panchal
Parag Panchal
3,388 Points

Thanks a lot Jack. It's resolved.