看着許多其他職位,並嘗試了幾種變化,但至今沒有運氣...的OnClick與自定義適配器和ListView
掌握定義所以下面的自定義列表適配器NPE:
public ListView tripList;
....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
// build a listView adapter
Log.i(LOGTAG,"About to create listView...");
tripList = (ListView) findViewById(android.R.id.list);
Log.i(LOGTAG,"ListView created...");
Log.i(LOGTAG,"About to create adapter...");
tripAdapter = new tripListAdapter(this, tripCur);
setListAdapter(tripAdapter);
}
setContentView(R.layout.activity_trip_display);
...和適配器具有這樣的:
// custom list adaptor class
private class tripListAdapter extends CursorAdapter {
//private final Cursor dataC;
private final LayoutInflater vi;
public tripListAdapter(Context con, Cursor c) {
// super constructor thingy
super(con, c);
//dataC = c;
vi = LayoutInflater.from(con);
}
@Override
public View getView (int position, View tripView, ViewGroup parent){
Log.i(LOGTAG,"In getView...");
// Create a message handling object as an anonymous class.
//mTripClickHandler = new OnItemClickListener() {
// NPE ERROR ON THIS NEXT LINE (line 228):
tripList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Do something in response to the click
// Enable other buttons
togBtns(true);
的logcat:
06-13 09:47:49.195: I/WIBBLE(864): ListView created...
06-13 09:47:49.195: I/WIBBLE(864): About to create adapter...
06-13 09:47:49.285: I/WIBBLE(864): OnResume with this many rows returned by cursor: 3
06-13 09:47:49.285: I/WIBBLE(864): In the loop, so cursor is not null....
06-13 09:47:49.375: I/WIBBLE(864): In getView...
06-13 09:47:49.387: D/AndroidRuntime(864): Shutting down VM
06-13 09:47:49.387: W/dalvikvm(864): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
06-13 09:47:49.505: E/AndroidRuntime(864): FATAL EXCEPTION: main
06-13 09:47:49.505: E/AndroidRuntime(864): java.lang.NullPointerException
06-13 09:47:49.505: E/AndroidRuntime(864): at com.example.myfirstapp.DisplayTripsActivity$tripListAdapter.getView(DisplayTripsActivity.java:228)
06-13 09:47:49.505: E/AndroidRuntime(864): at android.widget.AbsListView.obtainView(AbsListView.java:2177)
06-13 09:47:49.505: E/AndroidRuntime(864): at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
06-13 09:47:49.505: E/AndroidRuntime(864): at android.widget.ListView.onMeasure(ListView.java:1159)
06-13 09:47:49.505: E/AndroidRuntime(864): at android.view.View.measure(View.java:15848)
06-13 09:47:49.505: E/AndroidRuntime(864): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
任何建議感激地收到。
編輯:如果我更換:
//tripList = (ListView) findViewById(android.R.id.list);
有:
tripList = getListView();
...我繼續前進(見維布勒消息),但得到另一個NPE,但似乎是在一些常規的系統:
06-13 10:26:49.825: I/WIBBLE(934): About to create listView...
06-13 10:26:49.865: I/WIBBLE(934): ListView created...
06-13 10:26:49.865: I/WIBBLE(934): About to create adapter...
06-13 10:26:49.945: I/WIBBLE(934): OnResume with this many rows returned by cursor: 3
06-13 10:26:49.945: I/WIBBLE(934): In the loop, so cursor is not null....
06-13 10:26:50.035: I/WIBBLE(934): In getView...
06-13 10:26:50.035: I/WIBBLE(934): Leaving getView...
06-13 10:26:50.035: D/AndroidRuntime(934): Shutting down VM
06-13 10:26:50.035: W/dalvikvm(934): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
06-13 10:26:50.185: E/AndroidRuntime(934): FATAL EXCEPTION: main
06-13 10:26:50.185: E/AndroidRuntime(934): java.lang.NullPointerException
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.AbsListView.obtainView(AbsListView.java:2179)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.ListView.onMeasure(ListView.java:1159)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.view.View.measure(View.java:15848)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
06-13 10:26:50.185: E/AndroidRuntime(934): at android.view.View.measure(View.java:15848)
...和很多其他行,沒有引用我的代碼。
以前的答案明確說過把onClick放在適配器的getView裏面,用於一個自定義的適配器。我已經嘗試了onClick活動,並得到了其他NPE錯誤。有一個setContentView調用 - 它只是下一行,但我沒有打擾把它放在這裏... – Nelmo
我寫了大量的自定義適配器,並且從不需要在適配器中設置點擊偵聽器。你正在擴展ArrayAdapter,BaseAdapter,...嗎? – Spidy
CursorAdapter - 無可否認,當我在活動中使用了onClick時,我至少獲得了填充列表,但沒有響應點擊,就好像onClick不存在一樣。那時候我開始在這裏看到並且看到了使用getView的建議... – Nelmo