我试图将我的Array List of Vehicles从主活动传递到我的MapView类,但当我尝试读回ArrayList时,它一直给我NullPointerException。
车辆代码
public class Vehicle implements Parcelable
{
private String name = "";
private String GPSDateTime = "";
private long latitude = 0;
private long longtitude = 0;
private String event = "";
public Vehicle (String vName, String vGpsDateTime, long vLatitude,
long vLongtitude, String vEvent)
{
this.name = vName;
this.GPSDateTime = vGpsDateTime;
this.latitude = vLatitude;
this.longtitude = vLongtitude;
this.event = vEvent;
}
protected Vehicle(Parcel in) {
this.name = in.readString();
this.GPSDateTime = in.readString();
this.latitude = in.readLong();
this.longtitude = in.readLong();
this.event = in.readString();
}
public String getName()
{
return name;
}
public void setName(String vName)
{
name = vName;
}
public String getGpsDateTime()
{
return GPSDateTime;
}
public void setGpsDateTime(String vGpsDateTime)
{
GPSDateTime = vGpsDateTime;
}
public long getLatitude()
{
return latitude;
}
public void setLatitude(Long vLatitude)
{
latitude = vLatitude;
}
public long getLongtitude()
{
return longtitude;
}
public void setLongtitude(Long vLongtitude)
{
longtitude = vLongtitude;
}
public String getEvent()
{
return event;
}
public void setEvent(String vEvent)
{
event = vEvent;
}
@Override
public String toString() {
return name;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(GPSDateTime);
dest.writeLong(latitude);
dest.writeLong(longtitude);
dest.writeString(event);
}
public static final Parcelable.Creator<Vehicle> CREATOR = new Parcelable.Creator<Vehicle>() {
@Override
public Vehicle createFromParcel(Parcel in) {
return new Vehicle(in);
}
@Override
public Vehicle[] newArray(int size) {
return new Vehicle[size];
}
};
}
主要活动类
public class MainActivity extends Activity {
public static String rslt="";
//VehicleList vehicleList = new VehicleList();
public static ArrayList<Vehicle> vehicleList = new ArrayList<Vehicle>();
@Override
protected void onCreate(Bundle savedInstanceState) {
parseReturnStringFromWebASMX(rslt);
Intent intent = new Intent(MainActivity.this, MapView.class);
intent.putParcelableArrayListExtra("vlist", vehicleList);
startActivity(intent);
}
public boolean parseReturnStringFromWebASMX(String input){
boolean ok = false;
String vehicleDelims = "\\|#\\|";
String [] vehicleData = input.split(vehicleDelims);
for(int j=0; j<vehicleData.length; j++){
String delims = "\\$\\#\\$";
String[] tokens = vehicleData[j].split(delims);
if(j!=0){
Vehicle v = parseNewVehicleString(tokens, parse);
ok = this.vehicleList.add(v);
}
}
}
public Vehicle parseNewVehicleString(String[] tokens, String parse)
{
String name = "", datetime = "", event = "";
Long latitude = (long) 0, longtitude = (long) 0;
for(int i=0; i<tokens.length; i++){
//parse = parse + "in here \n";
//parse = parse + tokens[i] + "\n";
if(!tokens[i].equalsIgnoreCase("")){
//parse = parse + "in here 2\n";
if (i==0){
name = (String)tokens[0];
//parse = parse + "Add name " + name + "\n";
}
else if (i==1){
datetime = (String) tokens[1];
//parse = parse + "Add datetime " + datetime + "\n";
}
else if (i==2){
latitude = (Long.parseLong(tokens[2]));
}
else if (i==3){
longtitude = (Long.parseLong(tokens[3]));
}
else if (i==4){
event = tokens[4];
}
}
}
Vehicle v = new Vehicle(name, datetime, latitude, longtitude, event);
return v;
//return parse;
}
}
MapView的
package com.example.myfirstapp;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MapView extends ActionBarActivity {
public static ArrayList<Vehicle> vehicleList = new ArrayList<Vehicle>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_view);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent i = this.getIntent();
vehicleList = i.getParcelableArrayListExtra("vList");//*
final AlertDialog ad1=new AlertDialog.Builder(this).create();
ad1.setMessage(test);
ad1.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map_view, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map_view,
container, false);
return rootView;
}
}
}
每当我尝试访问vehicleList
时,我得到的错误vehicleList = i.getParcelableArrayListExtra("vList");//*
过去几周我一直在拔头发。有人可以帮帮我~~
LogCat的错误记录
02-11 15:23:17.032:E / AndroidRuntime(24050): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.myfirstapp / com.example.myfirstapp.MapView}: java.lang.NullPointerException 02-11 15:23:17.032: E / AndroidRuntime(24050):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2396) 02-11 15:23:17.032:E / AndroidRuntime(24050):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2448) 02-11 15:23:17.032:E / AndroidRuntime(24050):at android.app.ActivityThread.access $ 600(ActivityThread.java:173)02-11 15:23:17.032:E / AndroidRuntime(24050):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1392) 02-11 15:23:17.032:E / AndroidRuntime(24050):at android.os.Handler.dispatchMessage(Handler.java:107)02-11 15:23:17.032:E / AndroidRuntime(24050):at android.os.Looper.loop(Looper.java:194)02-11 15:23:17.032: E / AndroidRuntime(24050):at android.app.ActivityThread.main(ActivityThread.java:5469)02-11 15:23:17.032:E / AndroidRuntime(24050):at java.lang.reflect.Method.invokeNative(Native Method)02-11 15:23:17.032:E / AndroidRuntime(24050):at java.lang.reflect.Method.invoke(Method.java:525)02-11 15:23:17.032: E / AndroidRuntime(24050):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:857) 02-11 15:23:17.032:E / AndroidRuntime(24050):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)02-11 15:23:17.032:E / AndroidRuntime(24050):at dalvik.system.NativeStart.main(Native Method)02-11 15:23:17.032: E / AndroidRuntime(24050):引起:java.lang.NullPointerException 02-11 15:23:17.032:E / AndroidRuntime(24050):at com.example.myfirstapp.MapView.onCreate(MapView.java:35)02-11 15:23:17.032:E / AndroidRuntime(24050):at android.app.Activity.performCreate(Activity.java:5226)02-11 15:23:17.032:E / AndroidRuntime(24050):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1151) 02-11 15:23:17.032:E / AndroidRuntime(24050):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2360)
答案 0 :(得分:2)
你要放
intent.putParcelableArrayListExtra("vlist", vehicleList);
并且正在尝试获取
vehicleList = i.getParcelableArrayListExtra("vList");
你的钥匙不匹配,因为你有一个错字。为避免将来出现这种情况,您应该使用常量。
public static VEHICLE_LIST_EXTRA = "vList";
并在两种情况下访问。