Array
Array

Reputation: 11

How to open new activity on click text view?

I am very new in application development. I am trying to open new activity when I click on TextView in Android Studio. But i am getting error from android studio which is below

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)

The LAYOUT page

<?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>

The Main Acxtivity Page

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

Upvotes: 1

Views: 158

Answers (1)

MaLa
MaLa

Reputation: 679

This is a result of activity_main.xml not containing Textview by id UserSignup.

It seems like you have posted some other xml. Because there is no "btnUserLogin" in xml you have written in java code.

Upvotes: 1

Related Questions