DDukesterman
DDukesterman

Reputation: 1399

Open another activity in Android with a button

I must be doing something wrong. I have this code in the first activity:

package com.Trenton.waziapp;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class WaziLoginScreen extends Activity implements OnClickListener{

EditText etUsername, etPassword;
Button bLogin;
Class ourClass;

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

    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    bLogin = (Button) findViewById(R.id.bLogin);

}

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

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.bLogin:

        Intent a = new Intent(WaziLoginScreen.this, ProfileScreen.class);
        startActivity(a);
        break;

    }
}

} What am I doing wrong here above to get the other Activity, ProfileScreen to open?

My manifest file contains activity entry like this-

    <activity
        android:name=".ProfileScreen"
        android:label="@string/title_activity_profile_screen">
    </activity>

Upvotes: 3

Views: 9731

Answers (3)

Shridutt Kothari
Shridutt Kothari

Reputation: 7394

Use this following code.

package com.Trenton.waziapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class WaziLoginScreen extends Activity {

EditText etUsername, etPassword;
Button bLogin;
Class ourClass;

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

    etUsername = (EditText) findViewById(R.id.etUsername);
    etPassword = (EditText) findViewById(R.id.etPassword);
    bLogin = (Button) findViewById(R.id.bLogin);

    bLogin.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent a = new Intent(WaziLoginScreen.this, ProfileScreen.class);
        startActivity(a);
    }
 }); 
 }

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

}

Thats it..enjoy friend.

Upvotes: 0

Pragnani
Pragnani

Reputation: 20145

You have forgot to set onclickListener... try this

yourButtonObject.setOnClickListener(this);

Upvotes: 4

Shridutt Kothari
Shridutt Kothari

Reputation: 7394

write following code in your OnCreate method inside your WaziLoginScreen class-

yourButtonObject.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent a = new Intent(WaziLoginScreen.this, ProfileScreen.class);
        startActivity(a);
    }
});

thats it.

Upvotes: 3

Related Questions