TextView.setOnClickListener没有跳转到新活动

huangapple go评论80阅读模式
英文:

TextView.setOnClickListener is not moving to new activity

问题

我正在尝试学习如何制作一个类似Instagram的应用程序。

我已经创建了登录和注册活动。

我现在的问题是,我有一个文本视图,应该是可以点击的,然后切换到另一个活动。

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

这段代码应该将我切换到RegistrationActivity,但当我点击文本视图时,屏幕会闪烁但仍然停留在登录页面。然后当我再次点击文本视图时,应用程序会崩溃,并显示以下错误。

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)

这个错误是指这段代码在注册活动中引起的。

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

但是这个按钮是用来注册账号的。但是在应用程序中,XML页面仍然是登录活动,并没有改变为注册活动。

编辑:
sign_up_btn 变量是这样的:

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

编辑2:
有人要求看一下活动的 XML 视图文件,所以这里是它们的内容。

activity_login.xml

<!-- 这里是 activity_login.xml 的内容 -->

activity_registration.xml

<!-- 这里是 activity_registration.xml 的内容 -->

编辑3:
这是 LoginActivity 的主要代码:

public class LoginActivity extends AppCompatActivity {
    // 这里是 LoginActivity 的内容
}

以及这是 RegistrationActivity 的代码:

public class RegistrationActivity extends AppCompatActivity {
    // 这里是 RegistrationActivity 的内容
}

如果您有任何问题,请随时问我。

英文:

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 &#39;void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)&#39; 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

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

    &lt;ImageView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;100dp&quot;
        android:src=&quot;@drawable/instagram_logo&quot;
        android:layout_marginBottom=&quot;40dp&quot;/&gt;

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

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

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_gravity=&quot;center&quot;
        android:layout_marginTop=&quot;16dp&quot;
        android:text=&quot;Not a member, Sign up now!&quot;
        android:textColor=&quot;#ffffff&quot;
        android:textSize=&quot;16dp&quot;
        android:id=&quot;@+id/sign_up_btn&quot; /&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_gravity=&quot;center&quot;
        android:layout_marginTop=&quot;16dp&quot;
        android:text=&quot;Forgot Password?&quot;
        android:textColor=&quot;#ffffff&quot;
        android:textSize=&quot;16dp&quot;
        android:id=&quot;@+id/forgot_pass_btn&quot;/&gt;


&lt;/LinearLayout&gt;

And activity_registration.xml

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

    &lt;ImageView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;100dp&quot;
        android:src=&quot;@drawable/instagram_logo&quot;
        android:layout_marginBottom=&quot;40dp&quot;/&gt;

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

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


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

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

    &lt;Button
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginTop=&quot;30dp&quot;
        android:id=&quot;@+id/signing_up_btn&quot;
        android:text=&quot;Sign Up&quot;
        android:textColor=&quot;#cccccc&quot;
        android:background=&quot;@color/colorPurple&quot;
        android:textAllCaps=&quot;false&quot;/&gt;

    &lt;TextView
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_gravity=&quot;center&quot;
        android:layout_marginTop=&quot;16dp&quot;
        android:text=&quot;Already have an Account?&quot;
        android:textColor=&quot;#ffffff&quot;
        android:textSize=&quot;16dp&quot;
        android:id=&quot;@+id/go_to_login_btn&quot;/&gt;

&lt;/LinearLayout&gt;

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

    }

}

答案1

得分: 1

EDIT: 在我充分理解您的问题后,我建议您使用 onClick 方法。

在您的 XML 代码中为按钮添加以下内容:
android:onClick=&quot;gotoLogin&quot;,现在这将显示红色的点击线并实现该方法,然后转到 Java 文件并添加您想要为该点击操作执行的代码。通过这种方式,您无需在 Java 文件中添加 ID 资源。

请让我知道这是否有效。

英文:

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=&quot;gotoLogin&quot; 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

答案2

得分: 1

你的问题很明确。RegistrationActivity 被启动。然而,在 RegistrationActivity.onCreate() 中,你执行了以下操作:

setContentView(R.layout.activity_login);

因此,你再次使用了来自 LoginActivity 而不是 RegistrationActivity 的布局 XML 文件。这就是为什么屏幕会“闪烁”,但似乎没有发生变化。实际上发生了变化,但布局与之前的 Activity 完全相同。这也解释了为什么调用 setOnClickListener() 失败,因为你使用了错误的布局 XML 文件。

英文:

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.

答案3

得分: 0

从代码中删除finish();,并且在XML中将文本视图的可点击属性设置为false。

英文:

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

huangapple
  • 本文由 发表于 2020年7月26日 20:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63099971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定