简体   繁体   中英

TextView.setOnClickListener is not moving to new activity

So i am trying to learn how to make an instagram like app.

And i created the login and signup activities.

My issue now is that i have a textview that is supposed to be clickable and switching me to a different activity.

sign_up_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
            Intent signUpIntent = new Intent(LoginActivity.this, RegistrationActivity.class);
            startActivity(signUpIntent);
        }
    });

This piece of code should switch me to the RegistrationActivity but when i click on the textview it flashes the screen but stays on the login page. Then when i click on the textview again it crashes the app and gives me this error.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.ssmc.instagram.RegistrationActivity.onCreate(RegistrationActivity.java:58)

The error is referring to this piece of code in the registration activity.

signing_up_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            register();
        }
    });

But this is the button thats meant to register an account. but in the app the xml page is still the login activity and it hasnt changed to the registration activity.

Edit: The sign_up_btn variable is this:

sign_up_btn = (TextView) findViewById(R.id.sign_up_btn);

Edit2: Some asked to see the xml view files of the activities. so here they are.

activity_login.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/login_container"
    android:padding="40dp"
    android:background="@drawable/animation_color"
    tools:context=".LoginActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:src="@drawable/instagram_logo"
        android:layout_marginBottom="40dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Username"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        android:backgroundTint="#fff"
        android:drawableLeft="@drawable/ic_profile"
        android:inputType="textPersonName"
        android:id="@+id/login_user_name"
        android:drawablePadding="10dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Password"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        android:backgroundTint="#fff"
        android:drawableLeft="@drawable/ic_lock"
        android:inputType="textPassword"
        android:id="@+id/login_password"
        android:drawablePadding="10dp" />
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:id="@+id/login_btn"
        android:text="@string/log_in"
        android:textColor="#cccccc"
        android:background="@color/colorPurple"
        android:textAllCaps="false"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="16dp"
        android:text="Not a member, Sign up now!"
        android:textColor="#ffffff"
        android:textSize="16dp"
        android:id="@+id/sign_up_btn" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="16dp"
        android:text="Forgot Password?"
        android:textColor="#ffffff"
        android:textSize="16dp"
        android:id="@+id/forgot_pass_btn"/>


</LinearLayout>

And activity_registration.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/login_container"
    android:padding="40dp"
    android:background="@drawable/animation_color"
    tools:context=".LoginActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:src="@drawable/instagram_logo"
        android:layout_marginBottom="40dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Email"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        android:backgroundTint="#fff"
        android:drawableLeft="@drawable/ic_email"
        android:inputType="textPersonName"
        android:id="@+id/register_user_email"
        android:drawablePadding="10dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Username"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        android:backgroundTint="#fff"
        android:drawableLeft="@drawable/ic_profile"
        android:inputType="textPersonName"
        android:id="@+id/register_user_name"
        android:drawablePadding="10dp"/>


    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Password"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        android:backgroundTint="#fff"
        android:drawableLeft="@drawable/ic_lock"
        android:inputType="textPassword"
        android:id="@+id/register_password"
        android:drawablePadding="10dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Confirm Password"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        android:backgroundTint="#fff"
        android:drawableLeft="@drawable/ic_lock"
        android:inputType="textPassword"
        android:id="@+id/register_password_confirm"
        android:drawablePadding="10dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:id="@+id/signing_up_btn"
        android:text="Sign Up"
        android:textColor="#cccccc"
        android:background="@color/colorPurple"
        android:textAllCaps="false"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="16dp"
        android:text="Already have an Account?"
        android:textColor="#ffffff"
        android:textSize="16dp"
        android:id="@+id/go_to_login_btn"/>

</LinearLayout>

Edit3: Here is the main code for the LoginActivity:

public class LoginActivity extends AppCompatActivity {

    LinearLayout mLoginContainer;
    AnimationDrawable mAnimationDrawable;

    EditText username_et, password_et;
    ProgressDialog mProgressDialog;
    Button login_btn;

    TextView sign_up_btn, forgot_pass_btn;


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

        mLoginContainer = (LinearLayout) findViewById(R.id.login_container);
        mAnimationDrawable = (AnimationDrawable) mLoginContainer.getBackground();
        mAnimationDrawable.setEnterFadeDuration(5000);
        mAnimationDrawable.setExitFadeDuration(2000);

        username_et = (EditText) findViewById(R.id.login_user_name);
        password_et = (EditText) findViewById(R.id.login_password);
        mProgressDialog = new ProgressDialog(this);
        login_btn = (Button) findViewById(R.id.login_btn);

        sign_up_btn = (TextView) findViewById(R.id.sign_up_btn);
        forgot_pass_btn = (TextView) findViewById(R.id.forgot_pass_btn);

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogIn();
            }
        });

        sign_up_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent signUpIntent = new Intent(LoginActivity.this, RegistrationActivity.class);
                signUpIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(signUpIntent);
            }
        });

        forgot_pass_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                
            }
        });

    }
}

and this is the code for RegistrationActivity:

public class RegistrationActivity extends AppCompatActivity {

    LinearLayout mRegistrationContainer;
    AnimationDrawable mAnimationDrawable;

    EditText user_email_et, user_name_et, password_et, password_confirm_et;
    ProgressDialog mProgressDialog;
    Button signing_up_btn;
    TextView go_to_login_btn;

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

        mRegistrationContainer = (LinearLayout) findViewById(R.id.login_container);
        mAnimationDrawable = (AnimationDrawable) mRegistrationContainer.getBackground();
        mAnimationDrawable.setEnterFadeDuration(5000);
        mAnimationDrawable.setExitFadeDuration(2000);

        user_email_et = (EditText) findViewById(R.id.register_user_email);
        user_name_et = (EditText) findViewById(R.id.register_user_name);
        password_et = (EditText) findViewById(R.id.register_password);
        password_confirm_et = (EditText) findViewById(R.id.register_password_confirm);
        mProgressDialog = new ProgressDialog(this);
        signing_up_btn = (Button) findViewById(R.id.signing_up_btn);
        go_to_login_btn = (TextView) findViewById(R.id.go_to_login_btn);

        signing_up_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register();
            }
        });

        go_to_login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                startActivity(new Intent(RegistrationActivity.this, LoginActivity.class));
            }
        });

    }

}

EDIT: After understanding your question to my best I'll suggest you to use onClick method.

Add the following in your xml code for the button android:onClick="gotoLogin" now this will show red click on the line and implement the method now head to the Java file and add the code you want to do for that click action. By this you're exempted to add the id resource in the java file.

Do let me know if this worked

Your problem is clear. The RegistrationActivity is launched. However, in RegistrationActivity.onCreate() you do this:

setContentView(R.layout.activity_login);

So you are using the layout XML file from the LoginActivity again instead of the layout file from RegistrationActivity . That is why the screen "flashes" but doesn't appear to change. It is changing, but the layout is identical to the previous Activity . It also explains why the call to setOnClickListener() fails, because you are using the wrong layout XML file.

Remove the finish(); from the code and also in XML set the text view clickable to false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM