-1

Been searching all over stack exchange and can’t find answer.

Here is code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    Log.d("checkpoint", "1");
    //if there is a previous successful login, autofill username
    Log.d("checkpoint", "2");
    SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
    Log.d("checkpoint", "3");
    final String restoredUsername = prefs.getString("adminusername","");
    if(!restoredUsername.equalsIgnoreCase(""))
      {
          Log.d("checkpoint", "5");
          final EditText adminUsername = (EditText)findViewById(R.id.adminusername);
          Log.d("checkpoint", "6");
          Log.d("username", restoredUsername);
          adminUsername.setText(restoredUsername);    
          Log.d("checkpoint", "7");
      }


}

Here is the log

07-12 19:23:44.959: W/EGL_emulation(4171): eglSurfaceAttrib not implemented
07-12 19:23:46.150: D/checkpoint(4171): 1
07-12 19:23:46.150: D/checkpoint(4171): 2
07-12 19:23:46.160: D/checkpoint(4171): 3
07-12 19:23:46.170: D/checkpoint(4171): 5
07-12 19:23:46.170: D/checkpoint(4171): 6
07-12 19:23:46.170: D/username(4171): alexatwater
07-12 19:23:46.170: D/AndroidRuntime(4171): Shutting down VM
07-12 19:23:46.170: W/dalvikvm(4171): threadid=1: thread exiting with uncaught exception (group=0xb0d2ab20)
07-12 19:23:46.210: E/AndroidRuntime(4171): FATAL EXCEPTION: main
07-12 19:23:46.210: E/AndroidRuntime(4171): Process: com.alexatwater.elhsapcalc, PID: 4171
07-12 19:23:46.210: E/AndroidRuntime(4171): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alexatwater.elhsapcalc/com.alexatwater.elhsapcalc.AdminActivity}: java.lang.NullPointerException
07-12 19:23:46.210: E/AndroidRuntime(4171):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at android.os.Looper.loop(Looper.java:136)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at java.lang.reflect.Method.invokeNative(Native Method)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at java.lang.reflect.Method.invoke(Method.java:515)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at dalvik.system.NativeStart.main(Native Method)
07-12 19:23:46.210: E/AndroidRuntime(4171): Caused by: java.lang.NullPointerException
07-12 19:23:46.210: E/AndroidRuntime(4171):     at com.alexatwater.elhsapcalc.AdminActivity.onCreate(AdminActivity.java:50)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at android.app.Activity.performCreate(Activity.java:5231)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-12 19:23:46.210: E/AndroidRuntime(4171):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-12 19:23:46.210: E/AndroidRuntime(4171):     ... 11 more

From other posts I saw that if there is a nullpointerexpection being thrown, it is probably wrong layout. And it is. it’s set to R.layout.activity_admin but it really is R.layout.fragment_admin

When i change it in onCreate however to the right one, I also get a crash with this log

07-12 19:17:08.734: E/FragmentManager(4115): No view found for id 0x7f090000 (com.alexatwater.elhsapcalc:id/container) for fragment PlaceholderFragment{b10a3d78 #0 id=0x7f090000}
07-12 19:17:08.734: E/FragmentManager(4115): Activity state:
07-12 19:17:08.744: E/FragmentManager(4115):   Local Activity b1038af0 State:
07-12 19:17:08.744: E/FragmentManager(4115):     mResumed=false mStopped=false mFinished=false
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
Alex Atwater
  • 151
  • 1
  • 15

1 Answers1

1

This question gets asked every day. The dead giveaway is the auto-generated PlaceholderFragment code by Eclipse/ADT followed by user written code that tried to access elements from the layout.

The resource, R.id.adminusername, is in the Fragment layout file, not the Activity layout file. Hence findViewById returns null when called from MainActivity.onCreate.

Move your code to the PlaceholderFragment's override of onCreateView or onStart

selbie
  • 100,020
  • 15
  • 103
  • 173
  • Okay, that makes sense to move it there. But when I do, i get two warnings under checkpoint 2 and checkpoint 5 in the code: Description Resource Path Location Type Cannot make a static reference to the non-static method getPreferences(int) from the type Activity AdminActivity.java /ELHSAPCalc/src/com/alexatwater/elhsapcalc line 168 Java Problem – Alex Atwater Jul 13 '14 at 00:33
  • ^that got rid of errors, but I still am getting the same error with the code moved – Alex Atwater Jul 13 '14 at 00:47