简体   繁体   English

MainActivity上的空指针异常错误,无法启动整个应用程序

[英]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 ". 之后,会将输入的用户名带到另一个显示“ 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. 我运行调试,它说这是一个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: 这是我的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. 首先需要在onCreate中调用super.onCreate和setContentView。

@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. 请在onCreate方法中添加以下行。

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

It assumes that the corresponding xml file is activity_main.xml . 假定相应的xml文件为activity_main.xml Make sure to add this statements just above 确保在上面添加此语句

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

Please comment if you get any error. 如果您有任何错误,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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