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

KWASI NSIAH
KWASI NSIAH
10,578 Points

App Crashes on launch

App started to crush on launch after i added the OnClickListener code

package com.is.SN.story;

import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

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



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

    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

KWASI NSIAH
KWASI NSIAH
10,578 Points

I found the problem, there was a disconnect between the xml id and the Java. Thats why it was bugging out. Thank you for you help though.

Hello,

You need to put the lines

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

within your onCreate() method after the setContentView() call. You can leave the declarations outside the method, leaving them as member variables, but they must have their value assigned after setContentView is called. Currently, these two variables are getting assigned null because Android cannot find a view with the id of nameEditText or startButton because it does not know where to look for them. After making these changes, your code should look similiar to

package com.is.SN.story;

import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
     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 = (Button)findViewById(R.id.startButton);


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

             }
        });

    }

}

Please let us know if this helps or if you need more assistance. If you need more assistance, please provide your updated code so we can help you from the point you currently are at.

Hi,

I put in this exact same code and mine is still crashing.

My code automatically puts a line through the "ActionBarActivity" in the line 'public class MainActivity extends ActionBarActivity {'.

I also had some other errors I clicked Alt+Enter and they seemed to go away but the app is still crashing.

ActionBarActivity has been deprecated and AppCompatActivity should be used instead. For your crash, I'd recommend creating a new topic where you post your source code so people can help to find your issue since it sounds like it might be different from this issue.