我试图使用列表视图,所以显示我的日期,但我得到例外。当我运行我的应用程序是抛出运行时异常。请告诉我我必须做什么来摆脱这个execption: 这是我的例外
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;
}
}
答案 0 :(得分:5)
您遗失setContentView
。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // missing
setContentView(R.layout.mylayout); // missing
...// rest of the code
}
您正在尝试初始化列表视图
onewayListView=(ListView)findViewById(R.id.lvDepartures);
您尚未将内容设置为活动。因此,如果您尝试初始化,则会获得NullPointerException
。
您可以findViewById
将当前视图层次结构设置为活动。