【发布时间】:2013-12-19 08:07:12
【问题描述】:
我创建了 3 个活动
在第一个活动中,当我单击 startNotification 时有两个按钮启动通知
并记下开始时间。当我们单击通知时,它会打开 secondActivity 并发送记录
时间。当我从那里转到第三个活动时,当我现在转到第二个活动时,它给出的错误为NullPointerException。
点击按钮
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String MyText = "Reminder"; Notification mNotification = new Notification(R.drawable.ic_launcher, MyText, System.currentTimeMillis() ); int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff); String MyNotificationTitle = "Medicine!"; String MyNotificationText = "Don't forget to take your medicine!"; MyIntent = new Intent(MainActivity.this,SecondActivity.class); MyIntent.putExtra("s1",System.currentTimeMillis());
//PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),UNIQUE_INT_PER_CALL,MyIntent,0);
PendingIntent contentIntent = PendingIntent.getActivity(context,iUniqueId,MyIntent,0);
//A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, contentIntent);
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
第二次活动
Intent i=this.getIntent(); if(i!=null) { **data=i.getStringExtra("summar");**
if(data.equalsIgnoreCase("MainActivity"))
{
z=i.getExtras().getLong("s1");
int minutesZ = (int) ((z /(1000*60)) % 60);
int minutesZ1 = (int) ((System.currentTimeMillis() /(1000*60)) % 60);
Log.v("SecondTime",""+minutesZ);
Log.v("CurrentTime",""+minutesZ1);
x1=i.getExtras().getLong("s1");
p=System.currentTimeMillis()-x1;
int minutes = (int) ((p /(1000*60)) % 60);
text1.setText(""+minutes);
}
if(data.equalsIgnoreCase("ThirdActivity"))
{
//do something here
Toast.makeText(SecondActivity.this,"hiii",2000).show();
}
}
第三次活动
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=new Intent(ThirdActivity.this,SecondActivity.class);
startActivity(i);
i.putExtra("summar","ThirdActivity");
}
});
【问题讨论】:
-
兄弟,请提供logcat错误。
-
12-03 08:49:50.975:E/AndroidRuntime(3922):致命异常:主要 12-03 08:49:50.975:E/AndroidRuntime(3922):原因:java.lang .NullPointerException 12-03 08:49:50.975: E/AndroidRuntime(3922): at com.javacodegeeks.android.androidtimerexample.SecondActivity.onCreate(SecondActivity.java:30)
-
将这两行相互更改:startActivity(i); i.putExtra("summar","ThirdActivity");
-
错误实际上出现在这些行 data=i.getStringExtra("summar"); z=i.getExtras().getLong("s1"); x1=i.getExtras().getLong("s1");
-
@Shayan pourvatan 谢谢兄弟
标签: android nullpointerexception fatal-error