我想通过 Activity 发送某个date.getTime(),然后在目标 Activity date.setTime()中设置它。这是我的代码:
发送 Activity :
[...]
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);
}
});
[...]
这是接收 Activity :
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));
[...]
我已经检查了调试器,并且发送 Activity 正确发送了长变量(毫秒),但是当它达到“alertTime.setTime([...])”时,我收到以下错误:
block 引用>FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity >>ComponentInfo{com.fourbox.bocterapp/com.fourbox.bocterapp.AlertViewOnMap}: >>java.lang.NullPointerException
我无法意识到自己做错了什么。你能帮我吗?
谨致敬意。
最佳答案
您忘记在接收 Activity 中初始化alertTime变量。 公共(public)日期警报时间 = new Date();
关于java - 通过 Activity 传递 date.getTime,在目标 Activity 中使用 date.setTime,获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434875/