-2
這裏造成的除外內容:在android開發奇怪的例外,也許是處理
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.conne ct(user_info [2],Contract.SERVER_PORT);
此異常不會發生每次,它往往hanppens
其中是聲明user_info? – Blackbelt 2014-12-03 09:20:11
NPE是最容易解決的問題之一!它給了你一些NULL爲空的確切行。如果它是你的變量,它沒有設置。如果是API調用的結果,請閱讀手冊以查看可能的結果並添加檢查條件。 – brummfondel 2014-12-03 09:28:18
它不是一個奇怪的異常,它是一個NullpointerException。這可能是由於初始化,你還沒有做 – 2014-12-03 09:29:12