-2

Okay so I'm creating my homescreen where the user has login and sign up option. Now I'm trying to add functionality to the login button to go to the login page. I'm trying to go to email_login_form (Thats the id declared for the login screen) This is the code I put the homescreen :

package com.example.yoshita.testapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


public class HomeScreen extends ActionBarActivity {

LinearLayout email_login_form;
Button loginbutton;
Button signupbutton;

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

    email_login_form= (LinearLayout) findViewById(R.id.email_login_form);
    loginbutton= (Button) findViewById(R.id.loginbtn);
    signupbutton= (Button)findViewById(R.id.signupbutton);
    loginbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           email_login_form.addView(email_login_form);

        }
    });
}

I dont know what the issue is as I'm new to mobile development. And my app crashes the moment I run it on my device. It doesnt show any errors while compiling it tho.

Logcat looks like this>

02-03 15:54:46.457  15603-15603/com.example.yoshita.testapp I/SELinux﹕ Function: selinux_android_load_priority , priority [3] , priority version is VE=GOOGLE_POLICY
02-03 15:54:46.457  15603-15603/com.example.yoshita.testapp D/dalvikvm﹕ Late-enabling CheckJNI
02-03 15:54:46.627  15603-15603/com.example.yoshita.testapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-03 15:54:46.627  15603-15603/com.example.yoshita.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 11752: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-03 15:54:46.627  15603-15603/com.example.yoshita.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 11758: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 9403: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 537: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 559: Landroid/content/res/TypedArray;.getType (I)I
02-03 15:54:46.637  15603-15603/com.example.yoshita.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-03 15:54:46.697  15603-15603/com.example.yoshita.testapp D/AndroidRuntime﹕ Shutting down VM
02-03 15:54:46.697  15603-15603/com.example.yoshita.testapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417f8da0)
02-03 15:54:46.697  15603-15603/com.example.yoshita.testapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.yoshita.testapp, PID: 15603
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yoshita.testapp/com.example.yoshita.testapp.HomeScreen}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2441)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
            at android.app.ActivityThread.access$900(ActivityThread.java:171)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.yoshita.testapp.HomeScreen.onCreate(HomeScreen.java:27)
            at android.app.Activity.performCreate(Activity.java:5582)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
            at android.app.ActivityThread.access$900(ActivityThread.java:171)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
02-03 15:59:04.757  16641-16641/com.example.yoshita.testapp I/SELinux﹕ Function: selinux_android_load_priority , priority [3] , priority version is VE=GOOGLE_POLICY
02-03 15:59:04.767  16641-16641/com.example.yoshita.testapp D/dalvikvm﹕ Late-enabling CheckJNI
02-03 15:59:04.907  16641-16641/com.example.yoshita.testapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-03 15:59:04.907  16641-16641/com.example.yoshita.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 11752: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-03 15:59:04.907  16641-16641/com.example.yoshita.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-03 15:59:04.907  16641-16641/com.example.yoshita.testapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-03 15:59:04.907  16641-16641/com.example.yoshita.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 11758: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-03 15:59:04.907  16641-16641/com.example.yoshita.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-03 15:59:04.907  16641-16641/com.example.yoshita.testapp I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-03 15:59:04.907  16641-16641/com.example.yoshita.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 9403: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-03 15:59:04.907  16641-16641/com.example.yoshita.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
02-03 15:59:04.917  16641-16641/com.example.yoshita.testapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-03 15:59:04.917  16641-16641/com.example.yoshita.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 537: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-03 15:59:04.917  16641-16641/com.example.yoshita.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-03 15:59:04.917  16641-16641/com.example.yoshita.testapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-03 15:59:04.917  16641-16641/com.example.yoshita.testapp W/dalvikvm﹕ VFY: unable to resolve virtual method 559: Landroid/content/res/TypedArray;.getType (I)I
02-03 15:59:04.917  16641-16641/com.example.yoshita.testapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-03 15:59:04.967  16641-16641/com.example.yoshita.testapp D/AndroidRuntime﹕ Shutting down VM
02-03 15:59:04.967  16641-16641/com.example.yoshita.testapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417f8da0)
02-03 15:59:04.977  16641-16641/com.example.yoshita.testapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.yoshita.testapp, PID: 16641
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yoshita.testapp/com.example.yoshita.testapp.HomeScreen}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2441)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
            at android.app.ActivityThread.access$900(ActivityThread.java:171)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.yoshita.testapp.HomeScreen.onCreate(HomeScreen.java:27)
            at android.app.Activity.performCreate(Activity.java:5582)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
            at android.app.ActivityThread.access$900(ActivityThread.java:171)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
02-03 15:59:14.767  16641-16641/com.example.yoshita.testapp I/Process﹕ Sending signal. PID: 16641 SIG: 9

Homescreen XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:id="@+id/background"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".HomeScreen">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:clickable="false"
        android:textAlignment="center"
        android:textSize="30dp"
        android:textStyle="bold|normal"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Homescreen_img"
        android:isScrollContainer="false"
        android:src="@drawable/logo"
        android:layout_marginTop="24dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity= "center"/>

    <Button
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/loginbutton"
        android:layout_marginTop="46dp"
        android:layout_below="@+id/title"
        android:layout_centerHorizontal="true"
        android:gravity= "center"
        android:clickable="true"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sign Up"
        android:id="@+id/signupbutton"
        android:layout_below="@+id/loginbutton"
        android:layout_alignLeft="@+id/loginbutton"
        android:layout_alignStart="@+id/loginbutton"
        android:gravity= "center"
        />

</RelativeLayout>
laalto
  • 150,114
  • 66
  • 286
  • 303
Yoshita
  • 1
  • 1

1 Answers1

0

There is no loginbtn in your layout. The button's id seems to be loginbutton. That's why findViewById() returns null and calling setOnClickListener() on the null causes the NPE.

For that matter, there's no email_login_form view in the layout either. Also the code in your onClick() does not make any sense but it's not the reason for the NPE in your stacktrace.

laalto
  • 150,114
  • 66
  • 286
  • 303