这段代码的作用是将用户注册到服务器以获取gcm,并将我的服务器注册到mysql数据库中。它还会写入用户首选项。在gcm服务器和我的服务器上完成创建用户后,应用程序意外停止。请帮帮我?
package com.package.name;
import static com.package.name.CommonUtilities.SENDER_ID;
import static com.package.name.CommonUtilities.SERVER_URL;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class RegisterActivity extends Activity {
// alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Internet detector
ConnectionDetector cd;
// UI elements
EditText txtName;
EditText txtEmail;
// Register button
Button btnRegister;
// Session Manager Class
SessionManager session;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// Session Manager
session = new SessionManager(getApplicationContext());
cd = new ConnectionDetector(getApplicationContext());
session.checkLogin();
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(RegisterActivity.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
// stop executing code by return
return;
}
// Check if GCM configuration is set
if (SERVER_URL == null || SENDER_ID == null || SERVER_URL.length() == 0
|| SENDER_ID.length() == 0) {
// GCM sernder id / server url is missing
alert.showAlertDialog(RegisterActivity.this, "Configuration Error!",
"Please set your Server URL and GCM Sender ID", false);
// stop executing code by return
return;
}
txtName = (EditText) findViewById(R.id.txtName);
txtEmail = (EditText) findViewById(R.id.txtEmail);
btnRegister = (Button) findViewById(R.id.btnRegister);
/*
* Click event on Register button
* */
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// Read EditText dat
String name = txtName.getText().toString();
String email = txtEmail.getText().toString();
// Check if user filled the form
if(name.trim().length() > 0 && email.trim().length() > 0){
// Launch Main Activity
Intent i = new Intent(getApplicationContext(), MainActivity.class);
// Registering user on our server
// Sending registraiton details to MainActivity
i.putExtra("name", name);
i.putExtra("email", email);
session.registerEntry();
SharedPreferences pref = getApplicationContext().getSharedPreferences("A4APrefs", 0); // 0 - for private mode
Editor editor = pref.edit();
Random r = new Random();
int pairId = r.nextInt(999999-100000) + 100000;
editor.putInt("conId", pairId); // Storing integer
editor.commit(); // commit changes
startActivity(i);
finish();
}else{
// user doen't filled that data
// ask him to fill the form
alert.showAlertDialog(RegisterActivity.this, "Registration Error!", "Please enter your details", false);
}
}
});
}
}
为什么此活动在运行一次后关闭?我对Android很新,对此并不了解。
错误LogCat如下所示。
06-19 23:26:03.007: E/GCMRegistrar(29350): internal error: retry receiver class not set yet
06-19 23:26:03.071: E/URL(29350): > http://some.com/gcm/register.php
06-19 23:26:04.049: E/AndroidRuntime(29350): FATAL EXCEPTION: IntentService[GCMIntentService-950252240713-2]
06-19 23:26:04.049: E/AndroidRuntime(29350): java.lang.NullPointerException
06-19 23:26:04.049: E/AndroidRuntime(29350): at com.package.name.GCMIntentService.generateNotification(GCMIntentService.java:90)
06-19 23:26:04.049: E/AndroidRuntime(29350): at com.package.name.GCMIntentService.onMessage(GCMIntentService.java:48)
06-19 23:26:04.049: E/AndroidRuntime(29350): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
06-19 23:26:04.049: E/AndroidRuntime(29350): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
06-19 23:26:04.049: E/AndroidRuntime(29350): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 23:26:04.049: E/AndroidRuntime(29350): at android.os.Looper.loop(Looper.java:154)
06-19 23:26:04.049: E/AndroidRuntime(29350): at android.os.HandlerThread.run(HandlerThread.java:65)
06-19 23:26:05.056: E/UnRegister Receiver Error(29350): > Receiver not registered: com.google.android.gcm.GCMBroadcastReceiver@413d8ab8
根据LogCat,这一行是GCMIntent正在创建错误:
String[] separated = mmessage.split("::");
String epurl = separated[2];
String message = separated[1];
String ntitle = separated[0];
我认为只有当这些字符串中包含“::”时才能解决这些字符串,你能帮助我吗?
答案 0 :(得分:1)
如果您在指定的行上获得NullPointerException
,唯一可能的解释是mmessage
为空。
如果问题是mmessage
字符串没有足够的"::"
分隔符,则异常应为ArrayIndexOutOfBoundsException
,并且它应该位于其他行之一。< / p>
简单的解决方案是检查mmessage
是否为null,如果是,则不执行该代码序列。但是我希望它是null的事实表明代码中其他地方存在更大的问题。