-2

I'm trying to make a app like twitter and my app crashes due to the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

This is my LoginActivity code:

package com.ghayoorshan.nestedlayout1;
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.TextView;
import android.view.View.OnClickListener;

public class LoginActivity extends AppCompatActivity {

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

        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
        // Button button = (Button)findViewById(R.id.link_to_register);
        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
        // Switching to Register screen
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });
    }
}

The error occurs in the following line:

registerScreen.setOnClickListener(new View.OnClickListener()...

Logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ghayoorshan.nestedlayout1/com.ghayoorshan.nestedlayout1.LoginActivity}: 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:2552)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2618)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5701)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
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.ghayoorshan.nestedlayout1.LoginActivity.onCreate(LoginActivity.java:18)
        at android.app.Activity.performCreate(Activity.java:6294)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2618) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:207) 
        at android.app.ActivityThread.main(ActivityThread.java:5701) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
03-04 02:41:33.060 24340-24340/com.ghayoorshan.nestedlayout1 I/Process: Sending signal. PID: 24340 SIG: 9

What is causing the error?

Jack A.
  • 4,245
  • 1
  • 20
  • 34

2 Answers2

1

Your layout xml "activity_login.xml" does not contain TextView with id link_to_register or its id would be different. Check "activity_login.xml" to find the exact issue.

Passiondroid
  • 1,573
  • 1
  • 16
  • 28
0

@Passiondroid this is my XML code i think every thing is right here

<!-- Link to Registration Screen -->
            <TextView android:id="@+id/link_to_register"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dip"
                android:layout_marginBottom="40dip"
                android:text="New to Twitter? Register here"
                android:gravity="center"
                android:textSize="20dip"
                android:textColor="#0b84aa"/>