27

When my SplashActivity opens the LoginActivity my app crashes.

The following is my SplashActivity.java:

package com.example.android.appName;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        }, 1500);
    }
}

and my LoginActivity.java:

package com.example.android.appName;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

public class LoginActivity extends AppCompatActivity {
    private EditText usernameField = (EditText)findViewById(R.id.username),
                     passwordField = (EditText)findViewById(R.id.password);
    private TextView error = (TextView)findViewById(R.id.error);
    private ProgressBar progress = (ProgressBar)findViewById(R.id.progress);

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.login_menu, menu);
        return true;
    }  

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (android.os.Build.VERSION.SDK_INT > 5
                && keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            onBackPressed();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit(MenuItem item) {
        finish();
    }

    public void signIn(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

Part of AndroidManifest.xml:

<activity android:name=".SplashActivity"
    android:theme="@style/NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".LoginActivity"
    android:label="@string/title_activity_login" />

Error in logcat:

04-16 23:24:16.124 4015-4015/com.example.android.appName E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.android.appName, PID: 4015
 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.appName/com.example.android.appName.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2993)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
     at android.app.ActivityThread.access$1000(ActivityThread.java:197)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:145)
     at android.app.ActivityThread.main(ActivityThread.java:6872)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
     at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68)
     at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:145)
     at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
     at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:42)
     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:186)
     at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:168)
     at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:508)
     at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:180)
     at com.example.android.appName.LoginActivity.<init>(LoginActivity.java:20)
     at java.lang.reflect.Constructor.newInstance(Native Method)
     at java.lang.Class.newInstance(Class.java:1690)
     at android.app.Instrumentation.newActivity(Instrumentation.java:1080)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2983)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248) 
     at android.app.ActivityThread.access$1000(ActivityThread.java:197) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:145) 
     at android.app.ActivityThread.main(ActivityThread.java:6872) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
George Mulligan
  • 11,813
  • 6
  • 37
  • 50

2 Answers2

82

An Activity is not fully initialized and ready to look up views until after setContentView(...) is called in onCreate().

Only declare the fields like the following:

private EditText usernameField, passwordField;
private TextView error;
private ProgressBar progress;

and then assign the values in onCreate:

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

    usernameField = (EditText)findViewById(R.id.username);
    passwordField = (EditText)findViewById(R.id.password);
    error = (TextView)findViewById(R.id.error);
    progress = (ProgressBar)findViewById(R.id.progress);
}

Might not be part of the problem but as an extra bit of advice a Timer runs the TimerTask on a background thread and that should be avoided in this case. Replace the Timer with a Handler instead to run it on the UI thread.

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
}, 1500);
George Mulligan
  • 11,813
  • 6
  • 37
  • 50
  • This didn't fix the problem, but thanks for the `Handler` though :) –  Apr 16 '16 at 15:50
  • It sure is! Thanks a lot :P On a side note, I really hope that Android Studio will someday give more useful and clearer error messages than this... –  Apr 16 '16 at 16:09
  • 1
    I agree that particular stack trace is not very helpful in determining how to fix the problem. Most other error messages outside of lifecycle callbacks and project configuration are easier to understand. – George Mulligan Apr 16 '16 at 16:12
  • 3
    "An Activity is not fully initialized and ready to look up views until after setContentView(...) is called in onCreate()." SPOT ON . Thank you – McLan Feb 06 '17 at 16:14
  • Even after assigning the values to the id of my button in `onCreate()`, I still receive the same error @GeorgeMulligan – EnigmaTech Aug 30 '18 at 10:36
1

Views defined in xml file can only be accessed in java code, after setting the ContentView as the required xml file using:

setContentView(R.layout.xml_file_name);

So 1st call above method inside onCreate method and then initialize the View instances inside onCreate or inside the methods in which the instance will be used.

joe_chip
  • 2,468
  • 1
  • 12
  • 23
Gowtham Gowda
  • 1,780
  • 1
  • 10
  • 7