Search code examples
javaandroidandroid-xmlandroid-event

Android Event Listener's initialize crashes app


I am using the AVD Emulator to run apk. I have created a simple activity, which has two buttons, however whenever I write code for any of them, the app won't start, excluding to initialize them.

If I declare the actionlistener like below (code between asterisks) it causes the apk to throw an error on start up. I have attempted to place " android:onClick="btnLoginClicked" in the xml, however I get the same result.

Anyone able to tell me why this is? I get the feeling I'm missing something really simple.

package uk.ac.aber.cs22120.fuzzyNinja.pathFinder;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
//import android.view.View;
import android.widget.*;

public class ActivityLogin extends Activity {

    private Button btnLogin; 
    private ProgressBar progressBar_Login;
    //    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        progressBar_Login = (ProgressBar) findViewById(R.id.progressBar_Login);

***************************
        //btnLogin.setOnClickListener(btnLoginClickListener);
***************************

        setContentView(R.layout.activity_login);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_login, menu);
        return true;
    }
*************************
    //private OnClickListener btnLoginClickListener = new OnClickListener(){
    //  public void onClick(View v){
    //          
    //  }
    //};
*************************
}

The Following is my XML for this Activity: https://www.dropbox.com/s/d91t6xqrusi1s2t/activity_login.xml


Solution

  • Try this..

    Declear your setContentView(R.layout.activity_login); below super.onCreate before initializing any button, textview, etc.,

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            btnLogin = (Button) findViewById(R.id.btnLogin);
            progressBar_Login = (ProgressBar) findViewById(R.id.progressBar_Login);
    
    ***************************
            //btnLogin.setOnClickListener(btnLoginClickListener);
    ***************************        
        }