我的应用程序崩溃后退按钮(不幸的是,Frienday已经停止了。)

时间:2017-02-17 00:00:13

标签: android nullpointerexception back

我正在尝试创建一个用户可以登录和注销的应用程序 当他们登录时,会向他们显示一个活动,显示他们的电子邮件和姓名(来自数据库)。

我最近添加了一个导航栏选项。当用户进入导航栏并选择一个页面(关于,设置等)时,他们将转到该活动。如果按下后退按钮转到登录的Activity(LoginSuccess.java),应用程序崩溃显示"不幸的是,Frienday已停止运行。"

我试图调试它,它导致我在LoginSuccess.java中出现NullPointerException。我尝试删除LoginSuccess.java文件中的项目,这些项目会显示用户的姓名和电子邮件。如果删除这些,后退按钮将正常工作。

LoginSuccess.java文件

TextView name, email;

protected void onCreate(Bundle savedInstanceState) {
    ...
    name = (TextView) findViewById(R.id.name);
    email = (TextView) findViewById(R.id.name);
    Bundle bundle = getIntent().getExtras();
    name.setText(bundle.getString("name"));
    email.setText("Email: " + bundle.getString("email"));
}

login.php文件(用于数据库)

<?php
    require "init.php";

    $user_name = $_POST["user_name"];
    $password = $_POST["password"];

    $sql = "SELECT name,email FROM user_info WHERE user_name LIKE   '".$user_name."' AND password LIKE '".$password."';";

    $result = mysqli_query($con, $sql);
    $response = array();

    # Check if user already exists.
    if (mysqli_num_rows($result) > 0)
    {
        $row = mysqli_fetch_row($result);
        $name = $row[0];
        $email = $row[1];
        $code = "login_success";
        array_push($response, array ("code" => $code, "name" => $name, "email" => $email));
        echo json_encode($response);
    }
    else
    {
        $code = "login_failed";
        $message = "User not found... Please try again!";
        array_push($response, array("code" => $code, "message" => $message));
        echo json_encode($response);
    }

    mysqli_close($con);

?>

logcat的

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.goril.friendays, PID: 32166
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.goril.friendays/com.goril.friendays.LoginSuccess}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                  at android.app.ActivityThread.access$1100(ActivityThread.java:222)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:158)
                  at android.app.ActivityThread.main(ActivityThread.java:7237)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                  at com.goril.friendays.LoginSuccess.onCreate(LoginSuccess.java:25)
                  at android.app.Activity.performCreate(Activity.java:6876)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
                  at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:158) 
                  at android.app.ActivityThread.main(ActivityThread.java:7237) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我几天都在努力解决这个问题。如果有人可以帮助我,我很乐意欣赏它:)。我在Android网站教程中实现了后退选项。

教程: https://developer.android.com/training/implementing-navigation/ancestral.html

2 个答案:

答案 0 :(得分:1)

当您按下时,getIntent()不存在于上一个启动该活动的活动中,因此它将为空。

你可以这样抓住

Bundle extras = getIntent().getExtras();
if (extras != null) {
    name.setText(bundle.getString("name"));
    email.setText("Email: " + bundle.getString("email"));
}

答案 1 :(得分:0)

我想你已经有了解释,  试着读一下:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

bundle是null,你试图在null对象上获取一个字符串。  你需要检查是否为空。