【发布时间】:2015-10-31 22:55:41
【问题描述】:
我的应用经常因消息而崩溃
your app unfortunately stopped working
我查看了崩溃日志,在日志中发现以下错误
E/AndroidRuntime( 9780): java.lang.RuntimeException: Unable to start receiver com.parse.ParseBroadcastReceiver: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(Context) before using the Parse library.
E/AndroidRuntime( 9780): Caused by: java.lang.RuntimeException: applicationContext is null. You must call Parse.initialize(Context) before using the Parse library.
D/Process ( 9780): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:69012:26 PM
我正在 ionic 框架中构建我的应用程序,并遵循 https://github.com/aaronksaunders/dcww/tree/master/www/js 中的模式。我正在使用https://github.com/grrrian/phonegap-parse-plugin which 说我需要初始化onCreate。我该怎么做,我假设我的应用程序崩溃是由于这个原因。
我的 app.js 是
angular.module('intuch', ['ionic', 'intuch.controllers'])
.run(function ($ionicPlatform, ParseService) {
$ionicPlatform.ready(function () {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
ParseService.initialize().then(function () {
return ParseService.getInstallationId();
}).then(function (_response) {
console.log("Parse Initialized " + _response);
return ParseService.subscribe("broadcast_registered3");
}).then(function (_response) {
return ParseService.registerCallback(function (pnObj) {
alert("in assigned callback " + JSON.stringify(pnObj));
$state.go('app.dashboard');
});
}).then(function (success) {
console.log("Parse callback registered " + success);
}, function error(_error) {
alert(_error);
});
});
})
Parse 服务是
.factory('ParseService', function ($q, $window) {
var ParseConfiguration = {
applicationId: "waDzmmDVt7hNmRCoY50wOFN3lsRoW2xu42WrPYLs",
javascriptKey: "nUROouGGqI4WnbHDlNg5AweSQhmmXD84382xJmuU",
clientKey: "IF2RHNA0slYDq8feUhweewmcK2uEnOqDnK8J7jUf",
USING_PARSE: true,
initialized: false
};
return {
initialize: function () {
console.log("Missing Parse Plugin " + JSON.stringify($window.parsePlugin));
var deferred = $q.defer();
$window.parsePlugin.initialize(ParseConfiguration.applicationId, ParseConfiguration.clientKey, function () {
console.log("Initialized Parse Plugin");
deferred.resolve('success');
}, function (e) {
deferred.reject(e);
});
return deferred.promise;
},
getInstallationId: function () {
var deferred = $q.defer();
$window.parsePlugin.getInstallationId(function (id) {
deferred.resolve(id);
}, function (e) {
deferred.reject(e);
});
return deferred.promise;
},
subscribe: function (_channel) {
var deferred = $q.defer();
$window.parsePlugin.subscribe(_channel, function () {
deferred.resolve(true);
}, function (e) {
deferred.reject(false);
});
return deferred.promise;
},
unsubscribe: function (_channel) {
var deferred = $q.defer();
$window.parsePlugin.unsubscribe(_channel, function () {
deferred.resolve(true);
}, function (e) {
deferred.reject(false);
});
return deferred.promise;
},
getSubscriptions: function () {
var deferred = $q.defer();
$window.parsePlugin.getSubscriptions(function (_channelsArray) {
deferred.resolve(_channelsArray);
}, function (e) {
deferred.reject(false);
});
return deferred.promise;
},
registerCallback: function (_pushCallback) {
var deferred = $q.defer();
$window.parsePlugin.registerCallback('onNotification', function () {
$window.onNotification = function (pnObj) {
_pushCallback && _pushCallback(pnObj);
alert('We received this push notification: ' + JSON.stringify(pnObj));
if (pnObj.receivedInForeground === false) {
// TODO: route the user to the uri in pnObj
}
};
deferred.resolve(true);
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
}
};
})
【问题讨论】:
-
"您必须在使用 Parse 库之前调用 Parse.initialize(Context)。" ...请发布您的应用程序类。我认为您在使用 parse 之前不要调用 Parse.initialize(context)
-
@CodeMonster 添加了代码!
标签: android cordova parse-platform ionic