【问题标题】:TextView onclick method crashes the applicationTextView onclick 方法使应用程序崩溃
【发布时间】:2015-12-15 15:38:46
【问题描述】:

我有一个带有 OnClick 方法的 textView 来导航另一个活动。但是,如果我按下该文本,它不会导航。但如果我使用 Button 而不是 TextView,它会完美运行。 TextView 不能使用 OnClick 方法?

forgotpasstxt= (TextView) findViewById(R.id.txtForgotpPass);

 /** Textview on click even.
         *
         */
        forgotpasstxt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), ChangePassword.class);
                startActivityForResult(myIntent, 0);}
  });

XML.登录

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Forgot Password?"
        android:id="@+id/txtForgotpPass"
        android:clickable="true"
        android:onClick="perform_action"
        android:textColor="#ff3b5998"
        android:layout_below="@+id/btnReset"
        android:layout_centerHorizontal="true" />

请帮忙,我不明白为什么一旦用户点击忘记密码的链接,应用程序就会崩溃。

【问题讨论】:

    标签: java android


    【解决方案1】:

    删除这一行:

    android:onClick="perform_action"
    

    您正在以编程方式设置侦听器。

    【讨论】:

    • 删除此链接后是否可以使用。让我检查并调试应用程序。
    • 好的,如果有效,请告诉我 =) 如果您通过代码设置侦听器,则需要删除表单 XML,反之亦然
    • @J.Doe 它说你的代码中有一个空引用。我已经查看了您的代码,没有发现任何问题,您可以重新生成您的 R.java。我很好奇是什么导致了这个问题。
    【解决方案2】:

    您应该删除该行:

    android:onClick="perform_action"
    

    这是因为您将 TextView 设置为执行 onClick 两次。在您的 XML 中,您说要执行一个名为 perform_action 的方法,而在您的代码中,您做得正确。

    由于使用 Java 代码而不是 XML 进行点击更简洁,我建议只删除上面的行,因为 OnClickListener 是正确的。

    【讨论】:

    • 没问题!很高兴帮助:)
    【解决方案3】:

    删除forgotpasstxt.setOnClickListener并添加perform_action方法

       public void perform_action(View view) {
    
    
        Intent myIntent = new Intent(view.getContext(), ChangePassword.class);
        startActivityForResult(myIntent, 0);
    }
    

    【讨论】:

    • 这不应该被用作 ClickListeners 的首选。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多