2012-05-24 46 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

相关问题