getUsername返回null,而CurrentUser则不是

时间:2016-01-08 05:16:44

标签: android parse-platform

所以,我试图设置一个条件,如果他们没有登录,它会将用户发送到登录屏幕,但是将用户名输出到日志并保留在当前屏幕上登录。问题是我从getUsername获取空指针异常为空,但该语句不被我的if (getCurrentUser == null)条件捕获。我哪里错了?提前感谢您的帮助。

/*
 * Copyright (c) 2015-present, Parse, LLC.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */
package com.parse.starter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
import com.parse.ParseUser;



public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

  protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ParseAnalytics.trackAppOpenedInBackground(getIntent());

      ParseUser currentUser = ParseUser.getCurrentUser();
      if (currentUser == null) {
          Intent intent = new Intent(this, LoginActivity.class);
          intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
          intent.addFlags(intent.FLAG_ACTIVITY_CLEAR_TASK);
          startActivity(intent);
      }

      else {
          Log.i(TAG, currentUser.getUsername());
        }
  }

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

      public boolean onOptionsItemSelected (MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
            return true;
          }

          return super.onOptionsItemSelected(item);
        }
      }

这里是我用来创建新用户的代码

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);            //warn the user they left some necessary info out
                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 {
                    ParseUser newUser = new ParseUser();
                    newUser.setUsername(username);
                    newUser.setPassword(password);
                    newUser.setEmail(email);
                    newUser.signUpInBackground(new SignUpCallback() {
                        @Override
                        public void done(ParseException e) {
                            if (e == null) {
                                // Success!
                                Intent intent = new Intent(SignupActivity.this, MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

                                startActivity(intent);

                            } else {
                                AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);            //warn the user they left some necessary info out
                                builder.setMessage(e.getMessage())
                                        .setTitle(R.string.signup_error_title)
                                        .setPositiveButton(android.R.string.ok, null);

                                AlertDialog dialog = builder.create();
                                dialog.show();
                            }
                        }
                    });


                }
            }
        });

    }
}

3 个答案:

答案 0 :(得分:3)

您似乎正在使用ParseUser.enableAutomaticUser();,从onCreate

中移除此行代码
your_app extends Application{
}

使用ParseUser.enableAutomaticUser();

也可以自动为您创建匿名用户,而无需网络请求,以便您可以在应用程序启动时立即开始与您的用户合作。在应用程序启动时启用自动匿名用户创建时,ParseUser.getCurrentUser()永远不会是null。在第一次保存用户或与用户有关系的任何对象时,将自动在云中创建用户。在此之前,用户的对象ID将为null。启用自动用户创建功能可以轻松地将数据与您的用户相关联。例如,在Application.onCreate()方法中,您可以写:

https://parse.com/docs/android/guide#users-anonymous-users

答案 1 :(得分:0)

我遇到了类似的问题,我所做的是

if ((currentUser == null) && (currentUser.length == 0)) {
      Intent intent = new Intent(this, LoginActivity.class);
      intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
      intent.addFlags(intent.FLAG_ACTIVITY_CLEAR_TASK);
      startActivity(intent);
  }
这对我有用。你可以尝试一下

答案 2 :(得分:0)

如果" getcurrentuser"是数据类型字符串然后你应该使用" .equals" string datatype.like的函数:

字符串a;

  1. 如果(a.equals(空))
  2. 如果(a.equals("空&#34))

    上述条件之一对您有用,如果它仍然无效,请尝试:

    1. if(a.length == 0):这将为您提供字符串中的字符长度" a"。