在我的gcm安卓应用程序中,应用程序成功从服务器接收数据但是我在GCMIntentservice的onMessage()函数中进行了一些更改然后应用程序崩溃。这里我给了前一个并更改了onMessage()函数。请帮助找到错误
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
String message1 = intent.getExtras().getString("price1");
String message2 = intent.getExtras().getString("price2");
String message3 = intent.getExtras().getString("price3");
String message4 = intent.getExtras().getString("price4");
DatabaseHandler1 db = new DatabaseHandler1(getApplicationContext());
int count = db.getRowCount_1();
if(count > 0){
db.resetTables_1();
}
db.addUser_1(message,message1,message2,message3,message4);
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
它正在工作。然后做了一些改变。
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("price");
//String test1 = "instru";
if (message.equals("instru")){
String message1 = intent.getExtras().getString("price1");
String message2 = intent.getExtras().getString("price2");
String message3 = intent.getExtras().getString("price3");
String message4 = intent.getExtras().getString("price4");
String message5 = intent.getExtras().getString("price5");
DatabaseHandler1 db = new DatabaseHandler1(getApplicationContext());
int count = db.getRowCount_1();
if(count > 0){
db.resetTables_1();
}
db.addUser_1(message1,message2,message3,message4,message5);
}
else if(message.equals("signal")){
//else {
String message1 = intent.getExtras().getString("date");
String message2 = intent.getExtras().getString("name");
String message3 = intent.getExtras().getString("buy");
String message4 = intent.getExtras().getString("stop");
String message5 = intent.getExtras().getString("tv");
String message6 = intent.getExtras().getString("tp");
String message7 = intent.getExtras().getString("res");
String message_type = intent.getExtras().getString("type");
String message8 = "temp3";
if(message_type.equals("1")){
message8 = "Intra Day";
}else if(message_type.equals("2")){
message8 = "Short Term";
}else if(message_type.equals("4")){
message8 = "Short Term";
}
//DatabaseHandler1 db = new DatabaseHandler1(getApplicationContext());
DatabaseHandler1 db = new DatabaseHandler1(getApplicationContext());
int count = db.getRowCount_2();
if(count > 0){
db.resetTables_2();
}
db.addUser_2(message1,message2,message3,message4,message5,message6,message7,message8);
}
displayMessage(context, message);
// notifies user
generateNotification(context, message);
}
但是这会在运行时显示以下错误 logcat的
03-25 02:27:00.831: E/AndroidRuntime(6258): FATAL EXCEPTION: IntentService[GCMIntentService-211966293333-2]
03-25 02:27:00.831: E/AndroidRuntime(6258): java.lang.NullPointerException
03-25 02:27:00.831: E/AndroidRuntime(6258): at com.androidhive.pushnotifications.GCMIntentService.onMessage(GCMIntentService.java:52)
03-25 02:27:00.831: E/AndroidRuntime(6258): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
03-25 02:27:00.831: E/AndroidRuntime(6258): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
03-25 02:27:00.831: E/AndroidRuntime(6258): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 02:27:00.831: E/AndroidRuntime(6258): at android.os.Looper.loop(Looper.java:137)
03-25 02:27:00.831: E/AndroidRuntime(6258): at android.os.HandlerThread.run(HandlerThread.java:61)
03-25 02:27:03.370: E/UnRegister Receiver Error(6258): > Receiver not registered: com.google.android.gcm.GCMBroadcastReceiver@41794160
的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidhive.pushnotifications"
android:versionCode="1"
android:versionName="1.0" >
<!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.androidhive.pushnotifications.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<!-- Main activity. -->
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- Register Activity -->
<activity
android:name=".RegisterActivity"
android:label="@string/app_name" >
</activity>
<!-- Main Activity -->
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_main" >
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.androidhive.pushnotifications" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>