eitharus
eitharus

Reputation: 15

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

Upvotes: 0

Views: 1982

Answers (2)

GrIsHu
GrIsHu

Reputation: 23638

You can always initialize the events on your views after inflating your layout in your onCreate() method without it your application will not be able to find the views which you are trying.

Try out the below code and set your setcontentView() method before initializing your views.

public class ActivityLogin extends Activity {

private Button btnLogin; 
private ProgressBar progressBar_Login;


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

}

@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){
    //your logic here. 

  }
};

}

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

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);
***************************        
    }

Upvotes: 2

Related Questions