简体   繁体   English

尝试调用虚拟方法Editable

[英]Attempt to invoke virtual method Editable

I get this error whenever I press the login button in the app. 每当我在应用程序中按登录按钮时,都会出现此错误。 Any suggestions? 有什么建议么?

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.android.login.LoginActivity.isEmpty(LoginActivity.java:93) at com.example.android.login.LoginActivity.access$000(LoginActivity.java:20) at com.example.android.login.LoginActivity$1.onClick(LoginActivity.java:47) at android.view.View.performClick(View.java:5191) at android.view.View$PerformClick.run(View.java:20916) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5972) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) java.lang.NullPointerException:尝试在com.example.android.login.LoginActivity.isEmpty(LoginActivity.java:93 )com.example.android.login.LoginActivity.access $ 000(LoginActivity.java:20)com.example.android.login.LoginActivity $ 1.onClick(LoginActivity.java:47)android.view.View.performClick( android.os.Handler.dispatchMessage(Handler.java)上的View.java:5191)android.view.View $ PerformClick.run(View.java:20916)android.os.Handler.handleCallback(Handler.java:739) :95),位于android.os.Looper.loop(Looper.java:145),位于java.lang.reflect.Method.invoke(Native Method),位于android.app.ActivityThread.main(ActivityThread.java:5972)。 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1388)的lang.reflect.Method.invoke(Method.java:372),com.android.internal.os.ZygoteInit.main(ZygoteInit)。 java:1183)

Here is my code: 这是我的代码:

     package com.example.android.login;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;

/**
 * Created by alex on 6/22/2015.
 */
public class LoginActivity extends Activity {

    protected EditText usernameView;
    protected EditText passwordView;
    protected Button login;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        // Set up the login form.
        usernameView = (EditText) findViewById(R.id.email);
        passwordView = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.loginSubmit);

        // Set up the submit button click handler
       login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // Validate the log in data
                boolean validationError = false;
                StringBuilder validationErrorMessage =
                        new StringBuilder(getResources().getString(R.string.error_intro));
                if (isEmpty(usernameView)) {
                    validationError = true;
                    validationErrorMessage.append(getResources().getString(R.string.error_blank_username));
                }
                if (isEmpty(passwordView)) {
                    if (validationError) {
                        validationErrorMessage.append(getResources().getString(R.string.error_join));
                    }
                    validationError = true;
                    validationErrorMessage.append(getResources().getString(R.string.error_blank_password));
                }
                validationErrorMessage.append(getResources().getString(R.string.error_end));

                // If there is a validation error, display the error
                if (validationError) {
                    Toast.makeText(LoginActivity.this, validationErrorMessage.toString(), Toast.LENGTH_LONG)
                            .show();
                    return;
                }

                // Set up a progress dialog
                final ProgressDialog dlg = new ProgressDialog(LoginActivity.this);
                dlg.setTitle("Please wait.");
                dlg.setMessage("Logging in.  Please wait.");
                dlg.show();
                String email=usernameView.getText().toString();
                String password = passwordView.getText().toString();
                Parse.initialize(LoginActivity.this, "rk1rRk43ArNaSp6kxrjwLOlLbwh2n1rDlWxUYB5p", "1Yd2vjpZLq7ofOoQoissgNJ4S6YtFO67ho59QYGT");
                // Call the Parse login method
                ParseUser.logInInBackground(email, password, new LogInCallback() {

                    @Override
                    public void done(ParseUser user, ParseException e) {
                        dlg.dismiss();
                        if (e != null) {
                            // Show the error message
                            Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                        } else {
                            // Start an intent for the dispatch activity
                            Intent intent = new Intent(LoginActivity.this, DispatchActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    }
                });
            }
        });
    }

    private boolean isEmpty(EditText etText) {
        if (etText.getText().toString().trim().length() > 0) {
            return false;
        } else {
            return true;
        }
    }
}

I'm going to guess your usernameView or passwordView is null, which means it is probably not finding those views. 我猜您的usernameViewpasswordView为null,这意味着它可能找不到这些视图。 I'd do a null check and have it print an error if it null, then see what happens. 我会做一个null检查,如果它为null,它会显示一个错误,然后看看会发生什么。

EDIT: In light of your edited console, I can confirm it is, in fact, getting null for those edit views. 编辑:根据您编辑的控制台,我可以确认它实际上是那些编辑视图的null。 You should first check for the underlying issues behind why this might be null. 您应该首先检查为什么它可能为空的潜在问题。 Generally speaking you CAN cast null to anything. 一般来说,您可以将任何内容都设为null。

I suspect the ids you have are present in other layouts, but not in your R.layout.login_activity . 我怀疑您的ID存在于其他布局中,而不存在于R.layout.login_activity This will cause you to get null on these views, even if your IDE will not indicate this as an error (since the id technically exists, just not in your layout). 即使您的IDE不会将其表示为错误,这也会导致您在这些视图上获得null(因为id从技术上来说是存在的,只是不在您的布局中)。

EDIT2: Further edit... This isn't really an issue of invoking a virtual method, it's more about calling a method on a null reference that is technically of that object (due to the cast). EDIT2:进一步编辑...这实际上不是调用虚拟方法的问题,更多的是关于在技术上针对该对象的空引用上调用方法(由于强制转换)。 Null just means that there isn't really an object of that type, so it is not an instance of said object. Null只是意味着实际上没有该类型的对象,因此它不是该对象的实例。 So, you cannot call instantiated methods (non-static methods) on null. 因此,您不能在null上调用实例化方法(非静态方法)。

I hope that is informational about the error you are facing! 我希望这是您所面临的错误的信息! :) :)

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

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