将文本视图设置为可点击按钮

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

Setting up a text view as a clickable button

问题

以下是您提供的代码部分的翻译:

主活动代码(MainActivity.java):

package com.example.squashsademo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText mTextUsername;
    EditText mTextPassword;
    Button mButtonLogin;
    TextView mTextViewRegister;

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

        mTextUsername = (EditText)findViewById(R.id.edittext_username);
        mTextPassword = (EditText)findViewById(R.id.edittext_password);
        mButtonLogin = (Button)findViewById(R.id.button_login);
        mTextViewRegister = (TextView)findViewById(R.id.textview_register);
        mTextViewRegister.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent registerIntent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(registerIntent);
            }
        });
    }
}

注册活动代码(RegisterActivity.java):

package com.example.squashsademo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends AppCompatActivity {
    EditText mTextUsername;
    EditText mTextPassword;
    EditText mTextCnfPassword;
    Button mButtonRegister;
    TextView mTextViewLogin;

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

        mTextUsername = (EditText)findViewById(R.id.edittext_username);
        mTextPassword = (EditText)findViewById(R.id.edittext_password);
        mTextCnfPassword = (EditText)findViewById(R.id.edittext_cnf_password);
        mButtonRegister = (Button)findViewById(R.id.button_login);
        mTextViewLogin = (TextView)findViewById(R.id.textview_register);
        mTextViewLogin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent LoginIntent = new Intent(RegisterActivity.this, MainActivity.class);
                startActivity(LoginIntent);
            }
        });
    }
}

Android 清单文件(AndroidManifest.xml):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.squashsademo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SquashSADemo">
        <activity android:name=".RegisterActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

以上是您提供的代码的翻译部分,不包含其他内容。

英文:

I am developing a simple application which allows users to login using a username and password, I am trying to set up a "register" link in the form of a TextView which will take the user to the register page of the app. I can't find any error in my coding or logic but when I run the app and click the register link, the app just shuts down.

Here is my MainActivity code -

package com.example.squashsademo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText mTextUsername;
    EditText mTextPassword;
    Button mButtonLogin;
    TextView mTextViewRegister;


//    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mTextUsername = (EditText)findViewById(R.id.edittext_username);
        mTextPassword = (EditText)findViewById(R.id.edittext_password);
        mButtonLogin = (Button)findViewById(R.id.button_login);
        mTextViewRegister = (TextView)findViewById(R.id.textview_register);
        mTextViewRegister.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent registerIntent = new Intent(MainActivity.this,RegisterActivity.class);
                startActivity(registerIntent);
            }
        });
            }
        }

Here is the RegisterActivity I am trying to get to -

package com.example.squashsademo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends AppCompatActivity {
    EditText mTextUsername;
    EditText mTextPassword;
    EditText mTextCnfPassword;
    Button mButtonRegister;
    TextView mTextViewLogin;

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

        mTextUsername = (EditText)findViewById(R.id.edittext_username);
        mTextPassword = (EditText)findViewById(R.id.edittext_password);
        mTextCnfPassword = (EditText) findViewById(R.id.edittext_cnf_password);
        mButtonRegister = (Button)findViewById(R.id.button_login);
        mTextViewLogin = (TextView)findViewById(R.id.textview_register);
        mTextViewLogin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent LoginIntent = new Intent(RegisterActivity.this,MainActivity.class);
                startActivity(LoginIntent);
            }
        });
    }
}

and here is my AndroidManifest -

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.squashsademo&quot;&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/Theme.SquashSADemo&quot;&gt;
        &lt;activity android:name=&quot;.RegisterActivity&quot;&gt;&lt;/activity&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

Can anyone help see why this is not working because from my understanding it should be 将文本视图设置为可点击按钮
Thank you

EDIT - here are the layout files aswell

MainActivity -

&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;
    tools:context=&quot;.MainActivity&quot;
    android:orientation=&quot;vertical&quot;
    android:gravity=&quot;center_horizontal&quot;
    android:background=&quot;@drawable/background&quot;&gt;

    &lt;ImageView
        android:layout_width=&quot;299dp&quot;
        android:layout_height=&quot;80dp&quot;
        app:srcCompat=&quot;@drawable/logo&quot; /&gt;

    &lt;EditText
        android:id=&quot;@+id/edittext_username&quot;
        android:layout_width=&quot;356dp&quot;
        android:layout_height=&quot;57dp&quot;
        android:layout_marginTop=&quot;160dp&quot;
        android:background=&quot;@drawable/custom_input&quot;
        android:drawableLeft=&quot;@drawable/username&quot;
        android:drawablePadding=&quot;12dp&quot;
        android:hint=&quot;@string/username&quot;
        android:textColorHint=&quot;@color/white&quot;/&gt;

    &lt;EditText
        android:id=&quot;@+id/edittext_password&quot;
        android:layout_width=&quot;356dp&quot;
        android:layout_height=&quot;57dp&quot;
        android:layout_marginTop=&quot;20dp&quot;
        android:background=&quot;@drawable/custom_input&quot;
        android:drawableLeft=&quot;@drawable/password&quot;
        android:drawablePadding=&quot;12dp&quot;
        android:hint=&quot;@string/password&quot;
        android:textColorHint=&quot;@color/white&quot;/&gt;

    &lt;Button
        android:id=&quot;@+id/button_login&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;40dp&quot;
        android:layout_marginTop=&quot;20dp&quot;
        android:text=&quot;@string/login&quot;/&gt;

    &lt;LinearLayout
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:orientation=&quot;horizontal&quot;&gt;

        &lt;TextView
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;35dp&quot;
            android:textColor=&quot;#ffffff&quot;
            android:text=&quot;@string/not_registered&quot;/&gt;

        &lt;TextView
            android:id=&quot;@+id/textview_register&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;35dp&quot;
            android:paddingLeft=&quot;10dp&quot;
            android:textStyle=&quot;bold&quot;
            android:textColor=&quot;#ffffff&quot;
            android:text=&quot;@string/register&quot;/&gt;

    &lt;/LinearLayout&gt;





&lt;/LinearLayout&gt;

RegisterActivity -

&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:background=&quot;@drawable/background&quot;
    android:orientation=&quot;vertical&quot;
    android:gravity=&quot;center_horizontal&quot;
    tools:context=&quot;.RegisterActivity&quot;&gt;

    &lt;ImageView
        android:layout_width=&quot;298dp&quot;
        android:layout_height=&quot;80dp&quot;
        app:srcCompat=&quot;@drawable/logo&quot; /&gt;

    &lt;EditText
        android:id=&quot;@+id/edittext_username&quot;
        android:layout_width=&quot;356dp&quot;
        android:layout_height=&quot;57dp&quot;
        android:layout_marginTop=&quot;160dp&quot;
        android:background=&quot;@drawable/custom_input&quot;
        android:drawableLeft=&quot;@drawable/username&quot;
        android:drawablePadding=&quot;12dp&quot;
        android:hint=&quot;@string/username&quot;
        android:textColorHint=&quot;@color/white&quot;/&gt;

    &lt;EditText
        android:id=&quot;@+id/edittext_password&quot;
        android:layout_width=&quot;356dp&quot;
        android:layout_height=&quot;57dp&quot;
        android:layout_marginTop=&quot;20dp&quot;
        android:background=&quot;@drawable/custom_input&quot;
        android:drawableLeft=&quot;@drawable/password&quot;
        android:drawablePadding=&quot;12dp&quot;
        android:hint=&quot;@string/password&quot;
        android:textColorHint=&quot;@color/white&quot; /&gt;

    &lt;EditText
        android:id=&quot;@+id/edittext_cnf_password&quot;
        android:drawablePadding=&quot;12dp&quot;
        android:background=&quot;@drawable/custom_input&quot;
        android:layout_width=&quot;356dp&quot;
        android:layout_height=&quot;57dp&quot;
        android:layout_marginTop=&quot;20dp&quot;
        android:drawableLeft=&quot;@drawable/password&quot;
        android:hint=&quot;@string/confirm_password&quot;
        android:textColorHint=&quot;@color/white&quot;/&gt;

    &lt;Button
        android:id=&quot;@+id/button_register&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;40dp&quot;
        android:layout_marginTop=&quot;20dp&quot;
        android:text=&quot;@string/register&quot;/&gt;

    &lt;LinearLayout
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:orientation=&quot;horizontal&quot;&gt;

        &lt;TextView
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;35dp&quot;
            android:textColor=&quot;#ffffff&quot;
            android:text=&quot;@string/already_registered&quot;/&gt;

        &lt;TextView
            android:id=&quot;@+id/textview_login&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;35dp&quot;
            android:paddingLeft=&quot;10dp&quot;
            android:textStyle=&quot;bold&quot;
            android:textColor=&quot;#ffffff&quot;
            android:textSize=&quot;16dp&quot;
            android:text=&quot;@string/login&quot;/&gt;

    &lt;/LinearLayout&gt;


&lt;/LinearLayout&gt;

here are the log files aswell -

2020-10-21 20:56:22.881 11328-11328/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.squashsademo, PID: 11328
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.squashsademo/com.example.squashsademo.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method &#39;void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)&#39; on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3654)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8167)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method &#39;void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)&#39; on a null object reference
        at com.example.squashsademo.RegisterActivity.onCreate(RegisterActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:7963)
        at android.app.Activity.performCreate(Activity.java:7952)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3629)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)&#160;
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)&#160;
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)&#160;
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)&#160;
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)&#160;
        at android.os.Handler.dispatchMessage(Handler.java:107)&#160;
        at android.os.Looper.loop(Looper.java:237)&#160;
        at android.app.ActivityThread.main(ActivityThread.java:8167)&#160;
        at java.lang.reflect.Method.invoke(Native Method)&#160;
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)&#160;
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)&#160;

答案1

得分: 0

以下代码行抛出了空指针异常:

mTextViewLogin = (TextView)findViewById(R.id.textview_register);

在注册页面的布局中,没有名为 textview_register 的 ID,这就是你遇到问题的原因。

导致异常的原因是:java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)

可以尝试使用以下代码进行修复:

mTextViewLogin = (TextView)findViewById(R.id.textview_login);

而不是:

mTextViewLogin = (TextView)findViewById(R.id.textview_register);

你可以看到你试图将 XML 视图与一个无效的 ID 进行关联。

英文:

Below line of code is throwing NullPoinerException

mTextViewLogin = (TextView)findViewById(R.id.textview_register);

in the register activity layout, there is no id as textview_register that's why you are getting the issue

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method &#39;void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)&#39; on a null object reference

possible fix using below line

mTextViewLogin = (TextView)findViewById(R.id.textview_login);

instead of

mTextViewLogin = (TextView)findViewById(R.id.textview_register);

you can see you are trying to link XML view to java with an invalid id

huangapple
  • 本文由 发表于 2020年10月22日 01:14:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64468535.html
匿名

发表评论

匿名网友

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

确定