首页 > 解决方案 > 如何在点击文本视图上打开新活动?

问题描述

我是应用程序开发的新手。当我在 Android Studio 中单击 TextView 时,我正在尝试打开新活动。但我从下面的 android studio 收到错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 
android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null 
 object reference at 
 com.example.etechnomateapp.MainActivity.onCreate(MainActivity.java:41)

布局页面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/screenbg"
tools:context=".loginActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10sp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/img"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20sp"
        android:src="@drawable/shop_logo"
        tools:ignore="ContentDescription"
        />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40sp"
        android:layout_marginBottom="40sp"
        android:gravity="center_horizontal"
        android:text="@string/user_login"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="@color/colorDarkRed"
        android:textSize="22sp"

        />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/userEmailWreapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/colorRed"
        android:layout_marginTop="5dp"
        >
        <EditText
            android:id="@+id/emailAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:layout_marginTop="5dp"
            android:inputType="textEmailAddress"
            android:hint="@string/enter_email"
            android:textColor="@color/colorRed"
            android:textSize="18sp"/>

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/userPasswordWreapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/colorRed"
        android:layout_marginTop="5dp"
        >
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:layout_marginTop="5dp"
            android:hint="@string/enter_password"
            android:textColor="@color/colorRed"
            android:inputType="textPassword"
            android:textSize="18sp"/>
    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp"
        android:text="@string/user_login"
        android:background="@drawable/btnloginbg"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10sp"
        android:layout_marginBottom="10sp"
        android:gravity="center_horizontal"
        android:text="Or"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="@color/colorDarkRed"
        android:textSize="22sp"

        />


    <TextView
        android:id="@+id/forgetPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/forgetPass"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:layout_marginLeft="45dp"
        android:textColor="@color/colorDarkRed"
        android:textSize="16sp"

        />

    <TextView
        android:id="@+id/UserSignup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/signup"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:layout_marginLeft="250dp"
        android:layout_marginTop="-20dp"
        android:textColor="@color/colorDarkRed"
        android:textSize="16sp"

        />
 </LinearLayout>

</RelativeLayout>

主要活动页面

package com.example.etechnomateapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

 Button btnUsersignup, btnUserlogin;

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

    btnUserlogin=findViewById(R.id.btnUserLogin);
    btnUsersignup=findViewById(R.id.btnSignup);

    final TextView register = (TextView) findViewById(R.id.UserSignup);

    btnUsersignup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent= new Intent(MainActivity.this, RegistrationActivity.class);
            startActivity(intent);
        }
    });

    btnUserlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent= new Intent(MainActivity.this, loginActivity.class);
            startActivity(intent);
        }
    });

    register.setOnClickListener(new View.OnClickListener() {  /// errors occuring here 
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(MainActivity.this, RegistrationActivity.class);
            startActivity(myIntent);
             }
          });
     }
 }

标签: android-studioandroid-activity

解决方案


这是activity_main.xmlid 不包含 Textview的结果UserSignup

好像你已经发布了一些其他的 xml。因为您用java代码编写的xml中没有“btnUserLogin”。


推荐阅读