2014-06-13 67 views
-1

看着許多其他職位,並嘗試了幾種變化,但至今沒有運氣...的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) 

...和很多其他行,沒有引用我的代碼。

回答

1

您不應該從Adapter設置onItemClickListenerAdapter只關心創建單個列表項視圖。在視圖膨脹後,從Activity(或Fragment)中設置onItemClickListener

看起來您目前沒有設置活動視圖,這就是tripList ListView爲NULL的原因。使用setContentView(R.layout.your_layout)將視圖設置爲xml佈局。

您可能需要發佈大部分代碼。這是一個活動還是片段?你在擴展一個基本的Activity/Fragment,或者其他的ListActivity/ListFragment。我不知道,因爲你正在訪問Android的R檔,而不是你自己(android.R.id.list VS R.id.list)

更新

好吧,我已經回答了我的問題。您正在使用ListActivity(或ListFragment),這就是您使用Android的R文件的原因。我會將其更改爲getListView()而不是findViewById。我仍然會將setOnItemClickListener移到新的NPE例外,因爲這是正確的方法。

+0

以前的答案明確說過把onClick放在適配器的getView裏面,用於一個自定義的適配器。我已經嘗試了onClick活動,並得到了其他NPE錯誤。有一個setContentView調用 - 它只是下一行,但我沒有打擾把它放在這裏... – Nelmo

+0

我寫了大量的自定義適配器,並且從不需要在適配器中設置點擊偵聽器。你正在擴展ArrayAdapter,BaseAdapter,...嗎? – Spidy

+0

CursorAdapter - 無可否認,當我在活動中使用了onClick時,我至少獲得了填充列表,但沒有響應點擊,就好像onClick不存在一樣。那時候我開始在這裏看到並且看到了使用getView的建議... – Nelmo

0

在分配您的tripView變量ListView,需要使用:

tripList = (ListView) findViewById(android.R.id.list); 

嘗試改變這一行:

tripList = (ListView) findViewById(R.id.list); 

而是搜索android.R爲正確項(其中它可能沒有按」的t退出),在構建項目時生成的R文件中查找ListView項目。

確保在運行之前重建項目。

+0

如果我這樣做,我會得到'列表無法解析或不是字段'的編譯錯誤。 – Nelmo

+0

這是一個'ListActivity'或者只是一個帶有ListView視圖的普通'Activity'類? – rageandqq

相關問題