NullPointerExcep错误,用parcelable传送对象,结果对象为空。
05-15 07:42:35.306: I/ActivityThread(421): Pub com.anddoes.timer: com.anddoes.timer.TimerProvider
05-15 07:42:47.036: I/System.out(421): 0:1
05-15 07:42:47.246: V/tag(421): log---------->onCreate!
05-15 07:42:47.296: V/tag(421): log---------->onStart!
05-15 07:42:47.296: V/tag(421): log---------->onResume!
05-15 07:42:47.316: I/System.out(421): handle!
05-15 07:42:48.342: I/System.out(421): handle!
05-15 07:42:48.356: V/tag(421): log---------->onPause!
05-15 07:42:48.466: D/Test(421): mTimer:null
05-15 07:42:48.476: D/AndroidRuntime(421): Shutting down VM
05-15 07:42:48.476: W/dalvikvm(421): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-15 07:42:48.506: E/AndroidRuntime(421): FATAL EXCEPTION: main
05-15 07:42:48.506: E/AndroidRuntime(421): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anddoes.timer/com.anddoes.timer.TimerAlertFullScreen}: java.lang.NullPointerException
05-15 07:42:48.506: E/AndroidRuntime(421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-15 07:42:48.506: E/AndroidRuntime(421): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-15 07:42:48.506: E/AndroidRuntime(421): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-15 07:42:48.506: E/AndroidRuntime(421): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-15 07:42:48.506: E/AndroidRuntime(421): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 07:42:48.506: E/AndroidRuntime(421): at android.os.Looper.loop(Looper.java:123)
05-15 07:42:48.506: E/AndroidRuntime(421): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-15 07:42:48.506: E/AndroidRuntime(421): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 07:42:48.506: E/AndroidRuntime(421): at java.lang.reflect.Method.invoke(Method.java:507)
05-15 07:42:48.506: E/AndroidRuntime(421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-15 07:42:48.506: E/AndroidRuntime(421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-15 07:42:48.506: E/AndroidRuntime(421): at dalvik.system.NativeStart.main(Native Method)
05-15 07:42:48.506: E/AndroidRuntime(421): Caused by: java.lang.NullPointerException
05-15 07:42:48.506: E/AndroidRuntime(421): at com.anddoes.timer.TimerAlertFullScreen.setTitle(TimerAlertFullScreen.java:75)
05-15 07:42:48.506: E/AndroidRuntime(421): at com.anddoes.timer.TimerAlertFullScreen.updateLayout(TimerAlertFullScreen.java:95)
05-15 07:42:48.506: E/AndroidRuntime(421): at com.anddoes.timer.TimerAlertFullScreen.onCreate(TimerAlertFullScreen.java:65)
05-15 07:42:48.506: E/AndroidRuntime(421): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-15 07:42:48.506: E/AndroidRuntime(421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-15 07:42:48.506: E/AndroidRuntime(421): ... 11 more
代码:
package com.anddoes.timer;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import com.anddoes.timer.Timer1;
import com.anddoes.timer.TimerAlertFullScreen;
import com.anddoes.timer.Timers;
import com.anddoes.timer.Log;
public class TimerAlertFullScreen extends Activity {
// These defaults must match the values in res/xml/settings.xml
protected static final String SCREEN_OFF = "screen_off";
protected Timer1 mTimer;
// Receives the ALARM_KILLED action from the AlarmKlaxon,从AlarmKlaxon接收关闭闹钟动作,
// and also ALARM_SNOOZE_ACTION / ALARM_DISMISS_ACTION from other applications或者贪睡动作/关闭动作从其他的应用。
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(Timers.TIMER_DISMISS_ACTION)) {
dismiss(false);
} else {
Timer1 timer = intent.getParcelableExtra(Timers.TIMER_INTENT_EXTRA);
if (timer != null && mTimer.id == timer.id) {
dismiss(true);
}
}
}
};
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
Timer1 mTimer = (Timer1)getIntent().getParcelableExtra(Timers.TIMER_INTENT_EXTRA);
requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
// Turn on the screen unless we are being launched from the AlarmAlert
// subclass.
if (!getIntent().getBooleanExtra(SCREEN_OFF, false)) {
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
}
updateLayout();
// Register to get the alarm killed/snooze/dismiss intent.
IntentFilter filter = new IntentFilter(Timers.TIMER_KILLED);
filter.addAction(Timers.TIMER_DISMISS_ACTION);
registerReceiver(mReceiver, filter);
}
private void setTitle() {
android.util.Log.d("Test", "mTimer:"+mTimer);
String label = mTimer.getLabelOrDefault(this);
TextView title = (TextView) findViewById(R.id.alertTitle);
title.setText(label);
}
private void updateLayout() {
LayoutInflater inflater = LayoutInflater.from(this);
setContentView(inflater.inflate(R.layout.timer_alert, null));
/* dismiss button: close notification */
findViewById(R.id.dismiss).setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
dismiss(false);
}
});
/* Set the title from the passed in timer */
setTitle();
}
private NotificationManager getNotificationManager() {
return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
// Dismiss the alarm.
private void dismiss(boolean killed) {
Log.i(killed ? "Tiemr killed" : "Timer dismissed by user");
// The service told us that the alarm has been killed, do not modify
// the notification or stop the service.
if (!killed) {
// Cancel the notification and stop playing the alarm
NotificationManager nm = getNotificationManager();
nm.cancel(mTimer.id);
stopService(new Intent(Timers.TIMER_ALERT_ACTION));
}
finish();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
if (Log.LOGV) Log.v("TimerAlert.onDestroy()");
// No longer care about the alarm being killed.
unregisterReceiver(mReceiver);
}
@Override
public void onBackPressed() {
// Don't allow back to dismiss. This method is overriden by AlarmAlert
// so that the dialog is dismissed.
return;
}
}