英文:
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 -
<?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>
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 -
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="@drawable/background">
<ImageView
android:layout_width="299dp"
android:layout_height="80dp"
app:srcCompat="@drawable/logo" />
<EditText
android:id="@+id/edittext_username"
android:layout_width="356dp"
android:layout_height="57dp"
android:layout_marginTop="160dp"
android:background="@drawable/custom_input"
android:drawableLeft="@drawable/username"
android:drawablePadding="12dp"
android:hint="@string/username"
android:textColorHint="@color/white"/>
<EditText
android:id="@+id/edittext_password"
android:layout_width="356dp"
android:layout_height="57dp"
android:layout_marginTop="20dp"
android:background="@drawable/custom_input"
android:drawableLeft="@drawable/password"
android:drawablePadding="12dp"
android:hint="@string/password"
android:textColorHint="@color/white"/>
<Button
android:id="@+id/button_login"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:text="@string/login"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textColor="#ffffff"
android:text="@string/not_registered"/>
<TextView
android:id="@+id/textview_register"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:text="@string/register"/>
</LinearLayout>
</LinearLayout>
RegisterActivity -
<?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:background="@drawable/background"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".RegisterActivity">
<ImageView
android:layout_width="298dp"
android:layout_height="80dp"
app:srcCompat="@drawable/logo" />
<EditText
android:id="@+id/edittext_username"
android:layout_width="356dp"
android:layout_height="57dp"
android:layout_marginTop="160dp"
android:background="@drawable/custom_input"
android:drawableLeft="@drawable/username"
android:drawablePadding="12dp"
android:hint="@string/username"
android:textColorHint="@color/white"/>
<EditText
android:id="@+id/edittext_password"
android:layout_width="356dp"
android:layout_height="57dp"
android:layout_marginTop="20dp"
android:background="@drawable/custom_input"
android:drawableLeft="@drawable/password"
android:drawablePadding="12dp"
android:hint="@string/password"
android:textColorHint="@color/white" />
<EditText
android:id="@+id/edittext_cnf_password"
android:drawablePadding="12dp"
android:background="@drawable/custom_input"
android:layout_width="356dp"
android:layout_height="57dp"
android:layout_marginTop="20dp"
android:drawableLeft="@drawable/password"
android:hint="@string/confirm_password"
android:textColorHint="@color/white"/>
<Button
android:id="@+id/button_register"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:text="@string/register"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textColor="#ffffff"
android:text="@string/already_registered"/>
<TextView
android:id="@+id/textview_login"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:paddingLeft="10dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="16dp"
android:text="@string/login"/>
</LinearLayout>
</LinearLayout>
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 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' 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 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' 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) 
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) 
答案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 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论