2012-05-24 44 views
0

嘗試在expandableListView中獲取子項的文本值。onChildClick獲取nullpointerexception

在onchildclick事件中獲取nullpointerexception。

E/AndroidRuntime(358): at tournament.tracker.pkg.ExpList$5.onChildClick(ExpList.java:124) 

124行是adapter.getChild行。

我想傳遞被點擊到另一個活動的孩子的字符串值。

expList.setOnChildClickListener(new OnChildClickListener() 
    { 

    @Override 
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
     ExpandableListAdapter adapter = getExpandableListAdapter(); 
     gametype = adapter.getChild(groupPosition, childPosition).toString(); 
     //--------------------- 
     Intent pullt = new Intent(ExpList.this, JsonActivity.class); 
     Bundle bundle = new Bundle(); 
     bundle.putString("gametype", gametype); 
     pullt.putExtras(bundle); 
     pullt.putExtra("gametype", gametype); 

     startActivity(pullt); 
     //--------------------- 
     return false; 
    } 

    }); 

任何人都知道爲什麼這不起作用?請幫助如果可能的話

編輯

這裏的適配器:

public class ExpAdapter extends BaseExpandableListAdapter { 

     private Context myContext; 
     public ExpAdapter(Context context) { 
     myContext = context; 
     } 

     @Override 
     public Object getChild(int groupPosition, int childPosition) { 
     return null; 
     } 

     // Other code - not relevant 
    } 
+0

究竟是怎樣的例外是什麼樣子? (又名:stacktrace plz!) – yoshi

+0

你在哪裏得到NullPointerException? –

+0

快速猜測只有'適配器'將是一個可能的候選人。所以'getExpandableListAdapter()'返回'null'。你有沒有設置適配器? – yoshi

回答

3
public Object getChild(int groupPosition, int childPosition) { 
    return null; 
} 

此功能將總是返回null。嘗試以任何方式訪問null(如使用toString())將創建一個空指針異常,您必須實現此函數才能返回實際數據。

修復程序可能是:

public Object getChild(int groupPosition, int childPosition) { 
    return ExpList.arrChildelements[groupPosition][childPosition];; 
} 
+0

謝謝。你能給我一個如何做的簡單例子嗎?如果你有時間。感謝您的時間以及 – user1411823

+0

您沒有提供太多的上下文,所以這是一個盲目的猜測,但嘗試'返回ExpList.arrChildelements [groupPosition] [childPosition];' – Sam

+0

謝謝薩姆你提供了寶貴的知識。請參閱編輯描述的底部,瞭解如何使用onChildClick功能。如果你不介意的話,你可以將我的代表撞上去。對於任何想要做同樣事情的人來說,這將是一個有價值的問題/答案。現在很多人都無法解釋如何做到這一點。 – user1411823

相關問題