应用程序在启动Activity时崩溃

时间:2017-08-04 19:47:55

标签: android nullpointerexception

应用程序在加载活动时崩溃,但如果我取消注释Make Register文本作为网站链接,则代码可以正常工作,但完全相同的代码副本只是不同的链接会使应用程序崩溃,代码如下。

该活动适用于登录页面,其中包含注册和重置密码的链接

logcat的

08-04 21:34:07.006 11888-11888/net.twistedcircuits.greenroadsmalta 
D/LOGIN PAGE: ______login page loaded
08-04 21:34:07.006 11888-11888/net.twistedcircuits.greenroadsmalta D/AndroidRuntime: Shutting down VM
    08-04 21:34:07.007 11888-11888/net.twistedcircuits.greenroadsmalta E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                         Process: net.twistedcircuits.greenroadsmalta, PID: 11888
                                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{net.twistedcircuits.greenroadsmalta/net.twistedcircuits.greenroadsmalta.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' on a null object reference
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
                                                                                             at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)' on a null object reference
                                                                                             at net.twistedcircuits.greenroadsmalta.LoginActivity.onCreate(LoginActivity.java:56)
                                                                                             at android.app.Activity.performCreate(Activity.java:6912)
                                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) 
                                                                                             at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) 
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                             at android.os.Looper.loop(Looper.java:154) 
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:6692) 
                                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

活动

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

        //lock screen for always Portrait mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Log.d("LOGIN PAGE","______login page loaded");

        loginEmail = (EditText) (findViewById(R.id.LoginEmail_editText));
        loginPassword = (EditText) (findViewById(R.id.LoginPassword_editText));


        //************************ Make Register text as link to website ************************//
/*
        registerLinkText = (TextView) (findViewById(R.id.registerLink_textView));
        registerLinkText.setClickable(true);
        registerLinkText.setMovementMethod(LinkMovementMethod.getInstance());

        register_text = "<a href='http://www.google.com'>No account yet? Create One</a>";
        registerLinkText.setText(fromHtml(register_text));
        registerLinkText.setLinkTextColor(getResources().getColor(R.color.loginTextColor));
*/
        //**********************************************************************************//

        //************************ Make Lost Password text as link to website ************************//
        lostPasswordLinkText = (TextView) (findViewById(R.id.lostPasswordLink_textView));
        lostPasswordLinkText.setClickable(true);
        registerLinkText.setMovementMethod(LinkMovementMethod.getInstance());

        password_text = "<a href='https://www.google.com.mt/search?q=dementia&spell=1&sa=X&ved=0ahUKEwiWyovunL7VAhXDzRQKHa2ND_0QBQgkKAA&biw=1152&bih=956'>Forgot your password?</a>";
        lostPasswordLinkText.setText(fromHtml(password_text));
        lostPasswordLinkText.setLinkTextColor(getResources().getColor(R.color.loginTextColor));
    }
}

1 个答案:

答案 0 :(得分:1)

registerLinkText为空。它尚未绑定到xml布局的视图。

registerLinkText = (TextView) (findViewById(R.id.registerLink_textView));

取消注释以上这一行。