My app is crashing in android studio, and I cannot figure out what's wrong when looking at my logs

Bryce Wharton

So I'm trying to build a login app in Android Studio and when I test it the app crashes for some reason. I'm only decent at looking through the log to figure out the issue but I cannot do it for this exception error. Pasted below is my log.

02-09 16:18:09.482 2467-2467/? I/art: Not late-enabling -Xcheck:jni         (already on)
02-09 16:18:09.585 2467-2467/edu.dtcc.bwharto9.loginform W/System:  ClassLoader referenced unknown         path: /data/app/edu.dtcc.bwharto9.loginform-2/lib/x86
02-09 16:18:09.590 2467-2467/edu.dtcc.bwharto9.loginform D/AndroidRuntime: Shutting down VM
02-09 16:18:09.590 2467-2467/edu.dtcc.bwharto9.loginform E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.dtcc.bwharto9.loginform, PID: 2467
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{edu.dtcc.bwharto9.loginform/edu.dtcc.bwharto9.loginform.MainActivity}:                
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2090)
at edu.dtcc.bwharto9.loginform.MainActivity.<init>(MainActivity.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) `

Here is also my Java

    package edu.dtcc.bwharto9.loginform;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {'

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

    EditText userName = (EditText)findViewById(R.id.userName);
    EditText password = (EditText)findViewById(R.id.password);
    public void login() {
        if (userName.getText().toString().equals("student") && password.getText().toString().equals("password"))
        {
            Toast.makeText(getApplicationContext(), "You have sucessfully logged in!", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Login error! Please check your username or password!", Toast.LENGTH_LONG).show();
        }
    }

}

XML

 <?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="edu.dtcc.bwharto9.loginform.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Please enter your username and Password "
        android:id="@+id/instructionsText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/userName"
        android:layout_below="@+id/instructionsText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:hint="Username" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/password"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:hint="Password"
        android:password="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log In"
        android:id="@+id/btnLogIn"
        android:layout_below="@+id/password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        android:onClick="login" />
</RelativeLayout>

Thanks to everyone who helps me out, I'm in a complete fix!

CommonsWare
EditText userName = (EditText)findViewById(R.id.userName);
EditText password = (EditText)findViewById(R.id.password);

This will not work. You are calling findViewById() from a field initializer. You cannot reliably call methods inherited from Activity here in general. And, for findViewById(), you cannot get results until after setContentView() has been called.

Replace those lines with:

EditText userName;
EditText password;

Then, replace your onCreate() with:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    userName = (EditText)findViewById(R.id.userName);
    password = (EditText)findViewById(R.id.password);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot figure out what's wrong with beautifulsoup4 in my python 3 script

From Dev

Can't figure out what's wrong with my SQL syntax

From Dev

How do I figure out what is wrong with my Outlook signatures?

From Java

my d3 line chart looks weird, I can't figure out what's wrong

From Dev

my android app keeps crashing when i run the emulator

From Dev

My app keeps crashing in Android Studio

From Dev

My app keeps crashing in Android Studio

From Dev

How can I find out what's wrong with my RAM?

From Dev

Can't figure out what goes wrong with my PHP code when trying to input information into database

From Dev

Can't figure out what's wrong with my if statement in JavaScript [Update Sept 1st It appears my for loop is running twice, how do I fix?]

From Dev

Segmentation Fault - How do I figure out what is wrong with my code? - C

From Dev

Can't figure out what is wrong with my code

From Dev

Can't figure out what is wrong with my merge sort code

From Dev

I'm developing in Android Studio, When I run my app on my S4 2 copies are created on my phone

From Dev

I cannot figure out how to correctly use notifyDataSetChanged within my app

From Dev

Can't figure out what's wrong in Pygame app

From Dev

Getting incorrect output when I implement merge sort with threads, can't figure out what's wrong

From Dev

Getting incorrect output when I implement merge sort with threads, can't figure out what's wrong

From Dev

Looking to pinpoint the what's wrong in my SQL query

From Dev

My iOS app is getting kicked out of the suspended state too quickly. I can't figure out what's hogging the CPU in the background

From Dev

Can anyone that knows how to analyze crash/dump files help me figure out what is crashing my pc?

From Dev

My android app keeps on crashing

From Dev

Android Studio: App is crashing after running it on my smartphone

From Dev

My code is broken with arrays, but I cannot figure it out

From Dev

My code is broken with arrays, but I cannot figure it out

From Dev

what causes my app crashing when coming back to screen?

From Dev

Why is my app crashing when I set DisplayHomeAsUpEnabled to true?

From Dev

App crashing when I come back in my TableViewController

From Dev

Cannot find out what's wrong with my require_once call to YouTube API

Related Related

  1. 1

    Cannot figure out what's wrong with beautifulsoup4 in my python 3 script

  2. 2

    Can't figure out what's wrong with my SQL syntax

  3. 3

    How do I figure out what is wrong with my Outlook signatures?

  4. 4

    my d3 line chart looks weird, I can't figure out what's wrong

  5. 5

    my android app keeps crashing when i run the emulator

  6. 6

    My app keeps crashing in Android Studio

  7. 7

    My app keeps crashing in Android Studio

  8. 8

    How can I find out what's wrong with my RAM?

  9. 9

    Can't figure out what goes wrong with my PHP code when trying to input information into database

  10. 10

    Can't figure out what's wrong with my if statement in JavaScript [Update Sept 1st It appears my for loop is running twice, how do I fix?]

  11. 11

    Segmentation Fault - How do I figure out what is wrong with my code? - C

  12. 12

    Can't figure out what is wrong with my code

  13. 13

    Can't figure out what is wrong with my merge sort code

  14. 14

    I'm developing in Android Studio, When I run my app on my S4 2 copies are created on my phone

  15. 15

    I cannot figure out how to correctly use notifyDataSetChanged within my app

  16. 16

    Can't figure out what's wrong in Pygame app

  17. 17

    Getting incorrect output when I implement merge sort with threads, can't figure out what's wrong

  18. 18

    Getting incorrect output when I implement merge sort with threads, can't figure out what's wrong

  19. 19

    Looking to pinpoint the what's wrong in my SQL query

  20. 20

    My iOS app is getting kicked out of the suspended state too quickly. I can't figure out what's hogging the CPU in the background

  21. 21

    Can anyone that knows how to analyze crash/dump files help me figure out what is crashing my pc?

  22. 22

    My android app keeps on crashing

  23. 23

    Android Studio: App is crashing after running it on my smartphone

  24. 24

    My code is broken with arrays, but I cannot figure it out

  25. 25

    My code is broken with arrays, but I cannot figure it out

  26. 26

    what causes my app crashing when coming back to screen?

  27. 27

    Why is my app crashing when I set DisplayHomeAsUpEnabled to true?

  28. 28

    App crashing when I come back in my TableViewController

  29. 29

    Cannot find out what's wrong with my require_once call to YouTube API

HotTag

Archive