按钮单击导致空指针

时间:2014-04-14 18:56:59

标签: java android parse-platform

enter image description here每次点击按钮,应用程序都会崩溃。我很难找到错误,但我确定它必须对onClickListener或onClick函数执行某些操作。

注意:我使用Parse API来处理某些后端内容。但我非常怀疑它与任何Parse的东西有什么关系。

这是我的代码:

public class SignUpActivity extends Activity {

    protected EditText mUserName;
    protected EditText mPassword;
    protected EditText mEmail;
    protected Button mSignUpButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_sign_up);

        mUserName = (EditText) findViewById(R.id.usernameField);
        mPassword = (EditText) findViewById(R.id.passwordField);
        mEmail = (EditText) findViewById(R.id.emailField);
        mSignUpButton = (Button) findViewById(R.id.signupButton);

        mSignUpButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String username = mUserName.getText().toString();
                String password = mPassword.getText().toString();
                String email = mEmail.getText().toString();

                username = username.trim();
                password = password.trim();
                email = email.trim();

                if(username.isEmpty() || password.isEmpty() || email.isEmpty()){
                    AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
                    builder.setMessage(R.string.signup_error_message)
                           .setTitle(R.string.signup_error_title)
                           .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                else{

                    setProgressBarIndeterminateVisibility(true);
                    ParseUser newUser = new ParseUser();
                    newUser.setUsername(username);
                    newUser.setPassword(password);
                    newUser.setEmail(email);
                    newUser.signUpInBackground(new SignUpCallback() {

                        @Override
                        public void done(ParseException e) {
                            setProgressBarIndeterminateVisibility(false);
                            if(e == null){
                                // success
                                Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                            }
                            else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
                                builder.setMessage(e.getMessage())
                                       .setTitle(R.string.signup_error_title)
                                       .setPositiveButton(android.R.string.ok, null);
                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sign_up, menu);
        return true;
    }

}

这是我的Log Cat:

04-14 14:19:05.881: E/AndroidRuntime(1156): FATAL EXCEPTION: main 04-14 14:19:05.881:
E/AndroidRuntime(1156): Process: com.ebad.ribbit, PID: 1156 04-14 14:19:05.881: 
E/AndroidRuntime(1156): java.lang.NullPointerException 04-14 14:19:05.881:
E/AndroidRuntime(1156): at com.ebad.ribbit.SignUpActivity$1.onClick(SignUpActivity.java:40) 04-14 14:19:05.881: 
E/AndroidRuntime(1156): at android.view.View.performClick(View.java:4438) 04-14 14:19:05.881: 
E/AndroidRuntime(1156): at android.view.View$PerformClick.run(View.java:18422) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at android.os.Handler.handleCallback(Handler.java:733) 04-14 14:19:05.881: 
E/AndroidRuntime(1156): at android.os.Handler.dispatchMessage(Handler.java:95) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at android.os.Looper.loop(Looper.java:136) 04-14 14:19:05.881: 
E/AndroidRuntime(1156): at android.app.ActivityThread.main(ActivityThread.java:5017) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at java.lang.reflect.Method.invokeNative(Native Method) 04-14 14:19:05.881: 
E/AndroidRuntime(1156): at java.lang.reflect.Method.invoke(Method.java:515) 04-14 14:19:05.881: 
E/AndroidRuntime(1156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 04-14 14:19:05.881: 
E/AndroidRuntime(1156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 04-14 14:19:05.881:
E/AndroidRuntime(1156): at dalvik.system.NativeStart.main(Native Method)

编辑:

根据要求,这是第40行:

String username = mUserName.getText().toString();

编辑2:

这是添加Log.i(TAG, mUserName == null ? "mUserName is null" : mUserName.getText() == null ? "mUserName.getText() is null" : "nothing is null");后的日志猫:

04-14 15:21:47.955: E/AndroidRuntime(1318): FATAL EXCEPTION: main
04-14 15:21:47.955: E/AndroidRuntime(1318): Process: com.ebad.ribbit, PID: 1318
04-14 15:21:47.955: E/AndroidRuntime(1318): java.lang.NullPointerException
04-14 15:21:47.955: E/AndroidRuntime(1318):     at com.ebad.ribbit.SignUpActivity$1.onClick(SignUpActivity.java:43)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at android.view.View.performClick(View.java:4438)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at android.view.View$PerformClick.run(View.java:18422)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at android.os.Handler.handleCallback(Handler.java:733)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at android.os.Looper.loop(Looper.java:136)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at java.lang.reflect.Method.invoke(Method.java:515)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-14 15:21:47.955: E/AndroidRuntime(1318):     at dalvik.system.NativeStart.main(Native Method)

编辑3:

根据请求,这是我的activity_sign_up.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SignUpActivity" >

    <EditText
        android:id="@+id/userNameField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:hint="@string/username_hint"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/passwordField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/userNameField"
        android:ems="10"
        android:hint="@string/password_hint"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/emailField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/passwordField"
        android:ems="10"
        android:hint="@string/email_hint"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/signupButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/emailField"
        android:layout_below="@+id/emailField"
        android:layout_marginTop="50dp"
        android:text="@string/sign_up_button_label" />

</RelativeLayout>

4 个答案:

答案 0 :(得分:1)

如果mUsernamenull,则在其上调用getText()将导致NullPointerException。在执行此操作之前,您应该检查它是否为空。请尝试更改为以下内容:

        if(mUserName == null || mPassword == null || mEmail == null){
            AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
            builder.setMessage(R.string.signup_error_message)
                   .setTitle(R.string.signup_error_title)
                   .setPositiveButton(android.R.string.ok, null);
            AlertDialog dialog = builder.create();
            dialog.show();
        }

        String username = mUserName.getText().toString();
        String password = mPassword.getText().toString();
        String email = mEmail.getText().toString();

        username = username.trim();
        password = password.trim();
        email = email.trim();

答案 1 :(得分:1)

请这样做:

@Override
public void onClick(View v) {
     if(mUserName != null && mPassword != null && mEmail != null && mUserName.getText() != null && mPassword.getText() != null && mEmail.getText() != null)
     {
          Your original code here.
     }
}

更新: 错字

 mUserName = (EditText) findViewById(R.id.usernameField); should be userNameField?  
 <EditText android:id="@+id/userNameField" ...

答案 2 :(得分:0)

尝试

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

https://developer.android.com/guide/topics/ui/dialogs.html

还要确保在应用清单中声明了SignUpActivity。我想确保所有活动都在清单中设置。

编辑1 如果第40行是问题,那么 mUserName为null 要么 mUserName.getText()为null。因此,事先创建几个检查,并添加一些调试日志,以验证它们不是空的。

确保R.layout.activity_sign_up将usernameField作为id。还要确保在任何其他app / res / layout文件夹(即layout-v14)中,所有R.layout.activity_sign_up都具有edittext。

答案 3 :(得分:0)

如果您的第40行出现此错误,则表示您的EditText中显示为null。

在每个获得字符串的地方尝试。至少它将使您免于从EditText获取空指针异常。

String username = String.valueOf(mUserName.getText());
String password = String.valueOf(mPassword.getText());
String email = String.valueOf(mEmail.getText());