我尝试使用列表视图以显示我的日期,但出现异常。当我运行时,我的应用程序正在抛出运行时异常。请建议我必须做些什么才能摆脱这个执行:这是我的异常
08-06 12:17:09.278: E/AndroidRuntime(3709): FATAL EXCEPTION: main
08-06 12:17:09.278: E/AndroidRuntime(3709): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gridtestproject/com.example.gridtestproject.TestActivity}: java.lang.NullPointerException
08-06 12:17:09.278: E/AndroidRuntime(3709): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-06 12:17:09.278: E/AndroidRuntime(3709): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-06 12:17:09.278: E/AndroidRuntime(3709): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-06 12:17:09.278: E/AndroidRuntime(3709): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-06 12:17:09.278: E/AndroidRuntime(3709): at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 12:17:09.278: E/AndroidRuntime(3709): at android.os.Looper.loop(Looper.java:137)
08-06 12:17:09.278: E/AndroidRuntime(3709): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-06 12:17:09.278: E/AndroidRuntime(3709): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 12:17:09.278: E/AndroidRuntime(3709): at java.lang.reflect.Method.invoke(Method.java:511)
08-06 12:17:09.278: E/AndroidRuntime(3709): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-06 12:17:09.278: E/AndroidRuntime(3709): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-06 12:17:09.278: E/AndroidRuntime(3709): at dalvik.system.NativeStart.main(Native Method)
08-06 12:17:09.278: E/AndroidRuntime(3709): Caused by: java.lang.NullPointerException
08-06 12:17:09.278: E/AndroidRuntime(3709): at com.example.gridtestproject.TestActivity.onCreate(TestActivity.java:47)
08-06 12:17:09.278: E/AndroidRuntime(3709): at android.app.Activity.performCreate(Activity.java:5104)
08-06 12:17:09.278: E/AndroidRuntime(3709): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-06 12:17:09.278: E/AndroidRuntime(3709): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-06 12:17:09.278: E/AndroidRuntime(3709): ... 11 more
这是主类文件
public class TestActivity extends Activity {
ListView onewayListView,ReturnListView;
OneWayFlightResult onewaydata;
ReturnFlightResult returndata;
static final String FlightNumber = ""; // parent node
static final String FlightCompanyName = "";
static final String FlightTime = "";
static final String FlightStop ="";
static final String FlightCost ="";
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
ArrayList<HashMap<String, String>> flightData = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < 12; i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(FlightCompanyName, "SpiceJet");
map.put(FlightNumber, "SG-123");
map.put(FlightTime, "6:00 - 7:00");
map.put(FlightStop, "1h 30m | Non Stop");
map.put(FlightCost, "Rs 20,000");
// adding HashList to ArrayList
flightData.add(map);
}
onewayListView=(ListView)findViewById(R.id.lvDepartures);
ReturnListView=(ListView)findViewById(R.id.lvArrivals);
// Getting adapter by passing xml data ArrayList
onewaydata=new OneWayFlightResult(this, flightData);
onewayListView.setAdapter(onewaydata);
// returndata=new ReturnFlightResult(this, flightData);
// onewayListView.setAdapter(returndata);
}
}
这是我的 OneWayResult 类代码
public class OneWayFlightResult extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public OneWayFlightResult(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row, null);
TextView flightTime = (TextView)vi.findViewById(R.id.flightTime); // title
TextView flightCompanyName = (TextView)vi.findViewById(R.id.flightCompanyName); // title
TextView flightNumber = (TextView)vi.findViewById(R.id.flightNumber); // title
ImageView flightLogo = (ImageView)vi.findViewById(R.id.flightLogo);
HashMap<String, String> flight = new HashMap<String, String>();
flight = data.get(position);
flightTime.setText(flight.get(TestActivity.FlightTime));
flightCompanyName.setText(TestActivity.FlightCompanyName);
flightNumber.setText(TestActivity.FlightNumber);
return vi;
}
}