android - getUsername 返回 null,而 CurrentUser 不是

标签 android parse-platform

因此,我正在尝试设置一个条件,如果用户未登录,它会将用户发送到登录屏幕,但如果用户已登录,则将用户名输出到日志并停留在当前屏幕上。问题是我从 getUsername 得到一个空指针异常是 null,但是我的 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();
                            }
                        }
                    });


                }
            }
        });

    }
}

最佳答案

看来你正在使用 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

关于android - getUsername 返回 null,而 CurrentUser 不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34669996/

相关文章:

android - Multidex 抛出 java.lang.NoClassDefFoundError、RxJava、Retrolambda

android - setOutputFormat 和 setAudioEncoder 之间有什么关系吗

ios - 快速从 Parse 中检索用户信息

ios - 如何在Parse iOS中避免数据重复?

android - 如何更改 parse.com android 中的 url?

android - 如何将 SlidingPaneLayout 的 Listview 部分的宽度设置为 100%

android - 与自定义行为一起使用时滑动刷新布局问题

java - Android Content Provider 数据库泄漏问题

javascript - Parse.User.signUp() 中的第一个参数是什么?

iOS 快速获取用户家乡最新FBSDK