以下是例外内容:
12-03 17:05:21.578 7214-7424/com.android.servermonitor.app E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-12599
Process: com.android.servermonitor.app, PID: 7214
java.lang.NullPointerException
at com.android.servermonitor.app.GetPushService$3.run(GetPushService.java:85)
at java.lang.Thread.run(Thread.java:841)
现在我把所有代码放在这里:
public class GetPushService extends Service {
private XMPPConnection connection;
private SmackAndroid smackAndroid;
private String[] user_info;
private Handler login_handler,msg_handler;
private ChatManager chatManager;
private NotificationManager notificationManager;
@Override
public int onStartCommand(Intent intent,int args0,int args1){
this.user_info = intent.getStringArrayExtra("USERINFO");
return START_REDELIVER_INTENT;
}
@Override
public void onCreate(){
super.onCreate();
login_handler = new Handler(){
@Override
public void handleMessage(Message msg){
if (msg.what == -1){
Intent intent = new Intent();
intent.putExtra("Login Complete",1);
intent.setAction("com.android.servermonitor.app.LOGIN_COMPLETE");
sendBroadcast(intent);
getPush();
}
}
};
msg_handler = new Handler(){
@Override
public void handleMessage(Message msg){
if (msg.what == 1){
String title = "Notification";
String content = (String)msg.obj;
Intent intent = new Intent(GetPushService.this,LoginActivity.class);
PendingIntent launch = PendingIntent.getActivity(GetPushService.this,0,intent,0);
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(GetPushService.this)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(launch)
.setDefaults(Notification.DEFAULT_VIBRATE);
notificationManager.notify(1,builder.build());
}
}
};
smackAndroid = SmackAndroid.init(this);
new Thread(new Runnable() {
@Override
public void run() {
connection = ConnectManager.connect(user_info[2], Contract.SERVER_PORT);
ConnectManager.login(connection,user_info[0],user_info[1]);
login_handler.sendEmptyMessage(-1);
}
}).start();
}
@Override
public void onDestroy(){
new Thread(new Runnable() {
@Override
public void run() {
connection.disconnect();
smackAndroid.onDestroy();
}
}).start();
super.onDestroy();
}
}
异常发生在:
connection = ConnectManager.connect(user_info [2],Contract.SERVER_PORT);
这种例外每次都不会发生,它经常发生
答案 0 :(得分:0)
添加支票
this.user_info = intent.getStringArrayExtra("USERINFO");
不返回null。这是您的Activity的外部接口,应检查其是否有效输入,或者您必须检查意图的所有填充是否在意图中设置此额外值。