0

New to android here. Looked through the other answers, but still couldn't figure out what was wrong... hoping for some help here. :)

Android Studio says there is a null object reference at line 28 which is the login.setOnClick... line.

Again, sorry if this code is a mess, I am kinda winging it!

Thanks in advance!

public class LoginScreen extends ActionBarActivity {

    // EditText mEdit;
    // TextView mText;

    private static String artist = "Artist";

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

        Button login = (Button) findViewById(R.id.button1);
        final EditText user = (EditText) findViewById(R.id.userName);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // mText = (TextView)findViewById(R.id.welcome);
                // mText.setText("Welcome "+mEdit.getText().toString()+"!");

                artist = user.getText().toString();

                Intent intent = new Intent(getApplicationContext(), SongSelection.class);
                startActivity(intent);
            }
        });
    }

    public static String getArtist(){
        return artist;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
  • Have a read through [this](http://stackoverflow.com/q/218384/658663) – beresfordt Mar 28 '15 at 20:59
  • 3
    "Android Studio says there is a null object reference at line 28 which is the login.setOnClick... line." -- then `login` is presumably `null`. Perhaps you do not have a widget named `@+id/button1` in `res/layout/activity_login_screen.xml`. – CommonsWare Mar 28 '15 at 21:00
  • Is this reported by lint (AS highlights the code even before you try to run your application) or is this an app crash (reported by AS when trying to run application)? – jskierbi Mar 28 '15 at 21:06

1 Answers1

0

findViewById() method returns null in case Activity is missing view with ID passed. Check whether your layout activity_login_screen contains view with id button1

jskierbi
  • 1,635
  • 1
  • 14
  • 22
  • Yep! You guys are right, it was the wrong ID name! Thanks for the quick responses! :) – Sam Michael Mar 28 '15 at 21:09
  • Hi @SamMichael if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – jskierbi Apr 22 '15 at 13:45