Button.setOnClickListener(this); error

10,077

Solution 1

Move the setContentView(R.layout.main) call before the initializing of your button. This should help. Good luck!

Solution 2

Try this: place the setContentView(R.layout.main) above btn_Login = (Button)findViewById(R.id.button_login);

I guess this would solve your problem. Give a try

Solution 3

I'll use other example, but you can adapt your Java android class: The attribute android:onClick can define one method that will be called when clicks happen. This might use reflection, calling Class methods.

Button Definition on your activity_main.xml:

<Button
    android:id="@+id/mapshow_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/txtMsg"
    android:layout_alignLeft="@+id/editLog"
    android:layout_alignRight="@+id/txtLogitude"
    android:text="@string/lblBtnMap"
    android:textSize="10sp" 
    android:onClick="clickMap"/>

create a method into your activity class:

public void clickMap(View v) {
    //TODO: do something    
}
Share:
10,077
Petrus K.
Author by

Petrus K.

Graduate of Malmö University in Telecommunications Engineering (B.Sc.) Links: - LinkedIn profile - GitHub repo

Updated on November 20, 2022

Comments

  • Petrus K.
    Petrus K. over 1 year

    This is the code:

    package com.elfapp;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        private Button btn_Login;
        private EditText et_UserName;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            btn_Login = (Button)findViewById(R.id.button_login);
            btn_Login.setOnClickListener(this);
    
            et_UserName = (EditText)findViewById(R.id.editText_userName);
    
            setContentView(R.layout.main);
        }
    
        public void onClick(View v) {
            if (v.equals(btn_Login)) {
                    // skriver ut en toast när man klickar på knappen
                //Toast.makeText(MainActivity.this, "Ansluter till server...", Toast.LENGTH_SHORT).show();
    
                    // används i debuggern för att påvisa att programmet exekverat hit
                //Log.v("ThisApp", "onClick Successful");
    
                    // TODO skickar det som står i et_UserName till controller (genom TCP/IP), som ska kolla om användaren finns
                Intent intent = new Intent(this, goListView);
                this.startActivity(intent);
            }
        }
    
    }
    

    The program crashes when I reach the btn_Login.setOnClickListener(this); statement and I don't have much of a clue about what to do.. (not used to the Eclipse debugger..)

    • Nick Maina
      Nick Maina almost 13 years
      Can you show us the output of the log cat?
  • Petrus K.
    Petrus K. almost 13 years
    Thanks Rosalie, it works now! :) I had to make sure a few other things work too, hence my late response, anyway thanks for your fast reply! :)