简体   繁体   中英

Null Pointer Exception Error on MainActivity that unable to start the whole app

I'm doing an application whereby there is a login in page and user will be prompt to enter their username and password. After that, it will take the username entered to another new page that will display "Welcome ". In this page, it will show a list of categories of charts that the user can choose to view it. However, when I tried to run it, the app keep crash. And I run the debug, it says that it was a NullPointerException.

The log cat show as above:

07-02 10:25:23.826: E/AndroidRuntime(1173): FATAL EXCEPTION: main
07-02 10:25:23.826: E/AndroidRuntime(1173): Process: com.nyp.exploregowhere, PID: 1173
07-02 10:25:23.826: E/AndroidRuntime(1173): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nyp.exploregowhere/com.nyp.exploregowhere.MainActivity}: java.lang.NullPointerException
07-02 10:25:23.826: E/AndroidRuntime(1173):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at android.os.Looper.loop(Looper.java:137)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at android.app.ActivityThread.main(ActivityThread.java:4998)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invokeNative(Native Method)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invoke(Method.java:515)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at dalvik.system.NativeStart.main(Native Method)
07-02 10:25:23.826: E/AndroidRuntime(1173): Caused by: java.lang.NullPointerException
07-02 10:25:23.826: E/AndroidRuntime(1173):     at com.nyp.exploregowhere.MainActivity.onCreate(MainActivity.java:26)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at android.app.Activity.performCreate(Activity.java:5243)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-02 10:25:23.826: E/AndroidRuntime(1173):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)

And this is my MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) 
{

    userNameET = (EditText)findViewById(R.id.userNameET);
    LoginBT = (Button)findViewById(R.id.LoginBT);

    LoginBT.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(MainActivity.this,LoginActivity.class);
            myIntent.putExtra("username", userNameET.getText().toString());
            startActivityForResult(myIntent,LOGIN_ACTIVITY_RESULT_CODE);
        }

    });


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

The new error codes after changing:

07-02 11:15:50.836: E/AndroidRuntime(1444):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
07-02 11:15:50.836: E/AndroidRuntime(1444):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
07-02 11:15:50.836: E/AndroidRuntime(1444):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
07-02 11:15:50.836: E/AndroidRuntime(1444):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-02 11:15:50.836: E/AndroidRuntime(1444):     at android.os.Looper.loop(Looper.java:137)
07-02 11:15:50.836: E/AndroidRuntime(1444):     at android.app.ActivityThread.main(ActivityThread.java:4998)
07-02 11:15:50.836: E/AndroidRuntime(1444):     at java.lang.reflect.Method.invokeNative(Native Method)
07-02 11:15:50.836: E/AndroidRuntime(1444):     at java.lang.reflect.Method.invoke(Method.java:515)
07-02 11:15:50.836: E/AndroidRuntime(1444):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-02 11:15:50.836: E/AndroidRuntime(1444):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-02 11:15:50.836: E/AndroidRuntime(1444):     at dalvik.system.NativeStart.main(Native Method)

The super.onCreate and the setContentView need to be called first in onCreate.

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

    userNameET = (EditText)findViewById(R.id.userNameET);
    LoginBT = (Button)findViewById(R.id.LoginBT);

    LoginBT.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(MainActivity.this,LoginActivity.class);
            myIntent.putExtra("username", userNameET.getText().toString());
            startActivityForResult(myIntent,LOGIN_ACTIVITY_RESULT_CODE);
        }
    });

}

Please add following lines inside onCreate method.

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

It assumes that the corresponding xml file is activity_main.xml . Make sure to add this statements just above

userNameET = (EditText)findViewById(R.id.userNameET);

Please comment if you get any error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM