Java 通过活动传递date.getTime,在目标活动中使用date.setTime,获取NullPointerException
我想通过活动发送某个日期.getTime(),然后在目标活动日期.setTime()中设置它。这是我的密码: 发送活动:Java 通过活动传递date.getTime,在目标活动中使用date.setTime,获取NullPointerException,java,android,date,Java,Android,Date,我想通过活动发送某个日期.getTime(),然后在目标活动日期.setTime()中设置它。这是我的密码: 发送活动: [...] listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,
[...]
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(RecentSightings.this,
AlertViewOnMap.class);
// Pass data "name" followed by the position
i.putExtra("createdAt", alertsList.get(position).getCreatedAt().getTime());
startActivity(i);
}
});
[...]
package com.fourbox.bocterapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.ParseGeoPoint;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
public class AlertViewOnMap extends Activity {
public Date alertTime;
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(com.fourbox.bocterapp.R.layout.details_design);
alertTime.setTime(getIntent().getLongExtra("createdAt", 0));
[...]
我已经与调试器进行了检查,发送活动正确地发送了长变量(毫秒),但当它达到“alertTime.setTime([…])”时,我得到以下错误:
致命异常:主
java.lang.RuntimeException:无法启动活动>>ComponentInfo{com.fourbox.boctrapp/com.fourbox.boctrapp.AlertViewOnMap}:>>java.lang.NullPointerException
我很难意识到我做错了什么。你能帮我吗
恕我直言。您忘记在接收活动中初始化alertTime变量。
public Date alertTime=新日期() @user3641244如果此答案解决了您的问题,请单击空复选标记将其变为绿色并正式接受此答案。