2015-06-14 30 views
0

我正嘗試使用Parse.com作爲我的應用程序的用戶登錄/註冊活動,並且正在學習如何通過遵循本教程http://www.androidbegin.com/tutorial/android-parse-com-simple-login-and-signup-tutorial/來使用它。但經過我的一切類型的,我的應用程序保持崩潰,並給予NullPointerException異常Android Studio NullPointerException,嘗試使用parse.com

這裏是我的MainActivity

package com.example.ed.parselogintutorial; 

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

import com.parse.Parse; 
import com.parse.ParseAnonymousUtils; 
import com.parse.ParseUser; 


public class MainActivity extends Activity { 

    public void onCreate(Bundle savedInstanceState) { 

     // Enable Local Datastore. 
     Parse.enableLocalDatastore(this); 

     Parse.initialize(this, "T9so2huSfs1xMHwEjx9vSeUuKeyBZsXVyG4QHi7K", "yiQz0RMs9TCkWu8EsdsoVxcPWGlTyAmO20JuEh0X"); 

     super.onCreate(savedInstanceState); 

     //Determine whether the current user is an anonymous user 
     if (ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) { 
      //If user is anonymous, send the user to LoginSignupActivity.class 
      Intent intent = new Intent(MainActivity.this, 
        LoginSignupActivity.class); 
      startActivity(intent); 
      finish(); 
     } else { 
      //If current user is no anonymous user 
      //Get current user data from Parse.com 
      ParseUser currentUser = ParseUser.getCurrentUser(); 
      if (currentUser != null) { 
       //Send logged in users to Welcome.class 
       Intent intent = new Intent(MainActivity.this, Welcome.class); 
       startActivity(intent); 
       finish(); 
      } else { 
       //Send User to LoginSignupActivity.class 
       Intent intent = new Intent(MainActivity.this, 
         LoginSignupActivity.class); 
       startActivity(intent); 
       finish(); 
      } 
     } 
    } 


} 

這裏是堆棧

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ed.parselogintutorial/com.example.ed.parselogintutorial.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.parse.ParseUser.isLinked(java.lang.String)' on a null object reference 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5257) 
     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:903) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

asdsa

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.parse.ParseUser.isLinked(java.lang.String)' on a null object reference 
     at com.parse.ParseAnonymousUtils.isLinked(ParseAnonymousUtils.java:51) 
     at com.example.ed.parselogintutorial.MainActivity.onCreate(MainActivity.java:28) 
     at android.app.Activity.performCreate(Activity.java:5990) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5257) 
     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:903) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我對不起,如果在這裏有一個非常明顯的錯誤,因爲我是Android開發新手,請任何誰之前使用Parse.com或有任何關於此錯誤的知識,請教我如何解決它。先謝謝你。

+0

不確定這是否是問題,因爲我不熟悉Parse,但是您正在從Activity類調用Parse方法,而教程則是從Application類調用它們。 – aProperFox

+0

哦,是的,我錯過了ParseApplication類,感謝您注意它。我將嘗試現在創建ParseApplication。謝謝 – Charas

+0

當我添加了ParseApplication類之後,事實證明這個錯誤:無法啓動活動ComponentInfo {com.example.ed.parselogintutorial/com.example.ed.parselogintutorial.MainActivity}:java.lang.RuntimeException:您必須調用Parse.initialize(context,oauthKey,oauthSecret)在使用Parse庫之前。 如何在MainActivity中調用ParseApplication類?對不起,愚蠢的問題,需要幫助,謝謝。 – Charas

回答

0

閱讀本教程中的以下注釋,給出的代碼存在問題。下面是從評論中的建議:

還早,所以我不是知道什麼他們將在這裏...獲取 擺脫ParseApplication.java,並把這個在您的 mainactivity.java

protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_main); <<----UNDER THIS (left in for location purpose) 

// Add your initialization code here 

Parse.initialize(this, "YOUR_APPLICATION_ID", "YOUR_CLIENT_KEY"); 

ParseUser.enableAutomaticUser(); 

ParseACL defaultACL = new ParseACL(); 

// If you would like all objects to be private by default, remove this 

// line. 

defaultACL.setPublicReadAccess(true); 

ParseACL.setDefaultACL(defaultACL, true); 

// Determine whether the current user is an anonymous user 

if (ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) { <<----(left in for location purpose) 

本質上說,問題是,你不調用的setContentView(R.layout.activity_main);在嘗試調用Parse函數之前。

+0

其實我解決了它,問題是我沒有在應用程序的AndroidManifest中寫下這行:android:name =「ParseApplication」。非常感謝你的幫助。真的很感激它。 – Charas

相關問題