2014-09-22 57 views
0

運行我的小android應用程序時沒有問題。我整合了我的應用程序新功能。我正在使用www.parse.com使用現成腳本的實時推送通知。運行時沒有問題,但是當我關閉屏幕並重新打開時,出現錯誤。您的應用程序意外關閉。我不知道爲什麼,也沒有日誌。集成後意外關閉的應用程序Parse.com

我使用此代碼在我的主要活動之前的OnCreate

Parse.initialize(this, "XXX", "YYY"); 
PushService.setDefaultPushCallback(this, MainActivity.class); 
ParseInstallation.getCurrentInstallation().saveInBackground(); 
ParseAnalytics.trackAppOpened(getIntent()); 

AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.islamiceducationquestions.v1" 
    android:versionCode="13" 
    android:versionName="8.0.1"> 

    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.VIBRATE" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

<!-- 
    IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below 
    to match your app's package name + ".permission.C2D_MESSAGE". 
--> 
<permission android:protectionLevel="signature" 
    android:name="com.islamiceducationquestions.v1.permission.C2D_MESSAGE" /> 

<uses-permission android:name="com.islamiceducationquestions.v1.permission.C2D_MESSAGE" /> 
    <application 
     android:label="@string/app_name" 
     android:icon="@drawable/ic_launcher" 
     android:theme="@android:style/Theme.Holo.Light.DarkActionBar"> 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
<service android:name="com.parse.PushService" /> 
<receiver android:name="com.parse.ParseBroadcastReceiver"> 
    <intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    <action android:name="android.intent.action.USER_PRESENT" /> 
    </intent-filter> 
</receiver> 
<receiver android:name="com.parse.GcmBroadcastReceiver" 
    android:permission="com.google.android.c2dm.permission.SEND"> 
    <intent-filter> 
    <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

    <!-- 
     IMPORTANT: Change "com.parse.starter" to match your app's package name. 
    --> 
    <category android:name="com.islamiceducationquestions.v1" /> 
    </intent-filter> 
</receiver> 
    </application> 

</manifest> 

日誌是:

java.lang.RuntimeException: Unable to start receiver com.parse.ParseBroadcastReceiver: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library. 
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2844) 
at android.app.ActivityThread.access$1700(ActivityThread.java:156) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:157) 
at android.app.ActivityThread.main(ActivityThread.java:5867) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(context, applicationId, clientKey) before using the Parse library. 
at com.parse.Parse.checkContext(Parse.java:583) 
at com.parse.Parse.getApplicationContext(Parse.java:191) 
at com.parse.ManifestInfo.getContext(ManifestInfo.java:241) 
at com.parse.ManifestInfo.getPackageManager(ManifestInfo.java:249) 
at com.parse.ManifestInfo.getPackageInfo(ManifestInfo.java:272) 
at com.parse.ManifestInfo.deviceSupportsGcm(ManifestInfo.java:357) 
at com.parse.ManifestInfo.getPushType(ManifestInfo.java:129) 
at com.parse.PushService.startServiceIfRequired(PushService.java:150) 
at com.parse.ParseBroadcastReceiver.onReceive(ParseBroadcastReceiver.java:19) 
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2833) 
... 10 more 
+0

是什麼 「的OnCreate之前」,其實是什麼意思? – laalto 2014-09-22 17:45:39

+0

super.onCreate(savedInstanceState); – Mgnfcnt 2014-09-22 17:46:58

回答

0

正如你可以在這裏看到Android Parse.com Simple ListView Tutorial你會需要從Application的onCreate而不是從Activity中調用Parse.initialize o r服務。做到這一點是:

public class ParseApplication extends Application { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     // Add your initialization code here 
     Parse.initialize(this, YOUR_APPLICATION_ID, YOUR_CLIENT_KEY); 

    } 

} 

而且在AndroidManifest.xml添加ParseApplication作爲應用標籤名稱:

<application 
     android:label="@string/app_name" 
     android:icon="@drawable/ic_launcher" 
     android:name="ParseApplication" 
     .... 
+0

如果我使用這樣的:當上傳這個apk有一個錯誤; 必須只有一個應用程序 – Mgnfcnt 2014-09-22 18:11:25

+0

@Mgnfcnt:不要在'AndroidManifest.xml'中添加新的應用程序標籤,只需在當前應用程序標籤中添加'android:name =「ParseApplication」標籤 – 2014-09-22 18:13:01

+0

同樣的問題仍在繼續:( – Mgnfcnt 2014-09-22 18:18:56