NullPointerExcep错误,用parcelable传送对象,结果对象为空。

zhangh1136 2012-05-15 03:46:55
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;
}
}





...全文
289 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangh1136 2012-05-16
  • 打赏
  • 举报
回复
怎么没人回复啊!!求解啊!求大神帮忙。 谢谢
zhangh1136 2012-05-15
  • 打赏
  • 举报
回复
Intent intent = new Intent(StartActivity.this, TimerAlertFullScreen.class);
intent.putExtra(Timers.TIMER_INTENT_EXTRA, mTimer);
startActivity(intent);
代码补充,求大神解答。
zhangh1136 2012-05-15
  • 打赏
  • 举报
回复
package com.anddoes.timer;

import android.content.Context;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.BaseColumns;

public class Timer1 implements Parcelable {

//////////////////////////////
// Parcelable apis
//////////////////////////////
public static final Parcelable.Creator<Timer1> CREATOR
= new Parcelable.Creator<Timer1>() {
public Timer1 createFromParcel(Parcel p) {
return new Timer1(p);
}

public Timer1[] newArray(int size) {
return new Timer1[size];
}
};

public int describeContents() {
return 0;
}

public void writeToParcel(Parcel p, int flags) {
p.writeInt(id);
p.writeInt(vibrate ? 1 : 0);
p.writeString(label);
p.writeParcelable(alert, flags);
p.writeInt(silent ? 1 : 0);
}
//////////////////////////////
// end Parcelable apis
//////////////////////////////

//////////////////////////////
// Column definitions列定义
//////////////////////////////
public static class Columns implements BaseColumns {
/**
* The content:// style URL for this table
*/
public static final Uri CONTENT_URI =
Uri.parse("content://com.anddoes.timer/timer");

/**
* True if alarm should vibrate
* <P>Type: BOOLEAN</P>
*/
public static final String VIBRATE = "vibrate";

/**
* Message to show when alarm triggers
* Note: not currently used
* <P>Type: STRING</P>
*/
public static final String MESSAGE = "message";


/**
* Audio alert to play when alarm triggers
* <P>Type: STRING</P>
*/
public static final String ALERT = "alert";

/**
* These save calls to cursor.getColumnIndexOrThrow()
* THEY MUST BE KEPT IN SYNC WITH ABOVE QUERY COLUMNS
*/
public static final int TIMER_ID_INDEX = 0;
public static final int TIMER_VIBRATE_INDEX = 1;
public static final int TIMER_MESSAGE_INDEX = 2;
public static final int TIMER_ALERT_INDEX = 3;
}
//////////////////////////////
// End column definitions
//////////////////////////////

// Public fields
public int id;
public boolean vibrate;
public String label;
public Uri alert;
public boolean silent;

public Timer1(Cursor c) {
id = c.getInt(Columns.TIMER_ID_INDEX);
vibrate = c.getInt(Columns.TIMER_VIBRATE_INDEX) == 1;
label = c.getString(Columns.TIMER_MESSAGE_INDEX);
String alertString = c.getString(Columns.TIMER_ALERT_INDEX);
if (Timers.TIMER_ALERT_SILENT.equals(alertString)) {
if (Log.LOGV) {
Log.v("Timer is marked as silent");
}
silent = true;
} else {
if (alertString != null && alertString.length() != 0) {
alert = Uri.parse(alertString);
}

// If the database alert is null or it failed to parse, use the
// default alert.
if (alert == null) {
alert = RingtoneManager.getDefaultUri(
RingtoneManager.TYPE_ALARM);
}
}
}

public Timer1(Parcel p) {
id = p.readInt();
vibrate = p.readInt() == 1;
label = p.readString();
alert = (Uri) p.readParcelable(null);
silent = p.readInt() == 1;
}

// Creates a default alarm at the current time.
public Timer1() {
id = -1;
vibrate = true;
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
}

public String getLabelOrDefault(Context context) {
if (label == null || label.length() == 0) {
return context.getString(R.string.default_label);
}
return label;
}
}

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧