2016-01-08 82 views
0

所以,我試圖設置一個條件,它發送用戶登錄屏幕,如果他們沒有登錄,但輸出用戶名到日誌,並保持當前屏幕,如果他們登錄。問題是,我得到一個空指針異常getUsername爲空,但聲明不被我的if (getCurrentUser == null)條件捕獲。我哪裏錯了?預先感謝您的幫助。getUsername返回null,而CurrentUser不是

/* 
* 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(); 
          } 
         } 
        }); 


       } 
      } 
     }); 

    } 
} 
+0

您是否在應用程序中使用解析數據庫? –

+0

你在哪裏初始化了解析 –

+0

在我使用的添加用戶頁面中 import com.parse.ParseException; import com.parse.ParseUser; import com.parse.SignUpCallback;我使用 Parse.initialize(this); –

回答

2

似乎你正在使用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

+0

它仍然示數上與Log.i聲明:( –

+0

@PaxanaNonGrata行:我已經編輯了答案試試吧..如果你CCAN後的logcat也 –

+0

了java.lang.RuntimeException:無法啓動活動ComponentInfo {融爲一體。 parse.starter/com.parse.starter.MainActivity}:java.lang.NullPointerException:嘗試在null對象引用 –

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); 
    } 

這爲我工作。你可以試試看

+0

我不能運行它,因爲它無法解決.length –

+0

嘗試轉換爲字符串,然後使用length(); – Arun

+0

currentUser.toString.length(); – Arun

0

如果「getcurrentuser」是數據類型的字符串,那麼你應該使用「.equals」字符串datatype.like的功能:

串一個;

  1. 如果(a.equals(空))
  2. 如果(a.equals( 「空」))

    上述條件一個會爲你工作,如果它仍然無法正常工作然後嘗試:

    1. if(a.length == 0):這會給你字符串「a」中字符的長度。