2013-03-23 29 views
0

這是導致錯誤的代碼。對不起,我從原來的帖子中改變了很多,但我現在把所有的代碼放在一個地方。告訴我這是否是一個可怕的想法。NullPointer和ListView有問題

我得到的那臺myArrayAdapter爲我的ListView行我的錯誤

public class DisplayCaf extends Activity implements OnNavigationListener { 
    private static final String STATE_SELECTED_NAVIGATION_ITEM = null; 
    //Holds items. 
    ArrayList<String> menuArray; 
    //String adapter for ListView 
    ArrayAdapter<String> myArrayAdapter; 
    //set listView 
    ListView listView; 


    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.pager_caf); 

     listView = (ListView)findViewById(R.id.listView); 
     menuArray = new ArrayList<String>(); 

     //set up the adapter for listView 
     myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuArray); 
     //connect adapter to feed info to listView 
     listView.setAdapter(myArrayAdapter); 
    } 
} 
+0

該開關發生了什麼?在所有情況下,你都會做同樣的事情。如果你只是想確保它在1到6之間,那麼把這兩行代碼包裝在一個'if(位置<= 0 &&位置<= 6)' – MrLore 2013-03-23 20:40:46

+3

上發佈你的logcat跟蹤 – Pragnani 2013-03-23 20:55:51

+0

ListView是列表中的ListView 'pager_caf'? – 2013-03-23 21:20:19

回答

0

這是很難說的問題是你提供的資料有限什麼:你已經張貼了大量的代碼的事實,正如你所說的,並不意味着你提供了正確的(例如,你粘貼了那個可能無關緊要的大開關,而且你沒有粘貼布局......)。

我最好的猜測是,調用

public Object instantiateItem(View collection, int position); 

通話後立即發生:

setContentView(R.layout.pager_caf); 

,因此適配器永遠不會被初始化的機會。

如果您想更清楚地瞭解發生了什麼,請先幫助您自己:在這裏和那裏放置幾個Log.d(在instantiateItem的開始和setContentView的前後可能是一個好開始...... ),將問題減少到基本部分(將其平分直到只剩下錯誤)併發布剩下的代碼(以及日誌)。

+0

你需要什麼信息?我願意分享盡可能多的,你需要的。這是我第二次在這裏問一個問題,所以我知道我的信息可能有點不足。 – sjoshua270 2013-03-24 20:28:07

+0

Pragnani上面已經提到的logcat跟蹤可能是一個好的開始...... :)然而,我的觀點是,您需要在尋求幫助之前減小代碼的大小,並提供一個示例,說明您需要重現錯誤。不僅這可能會給你更多的幫助,但是在修改源代碼的時候你可以自己跟蹤bug。 – Rick77 2013-03-24 22:37:08