嗨我想在Android中创建动态按钮但是当我尝试设置上下文时,我有这个问题:
这是代码:
JSONArray jsonArr = new JSONArray(result);
for (int i=0; i < jsonArr.length(); i++) {
JSONObject building = jsonArr.getJSONObject(i);
Log.i("results", building.optString("name"));
Button myButton = new Button(this);
myButton.setText("Push Me");
LinearLayout ll = (LinearLayout)findViewById(R.id.buildingLL);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(myButton, lp);
}
任何人都可以帮助我吗?
答案 0 :(得分:1)
提供调用WebServiceHandler的上下文&amp;然后
Button myButton = new Button(mContext); // mContext is the context received on WebServiceHandler class
如果WebServiceHandler是任何Activity的私有类,那么
Button myButton = new Button(YOUR_ACTIVITY.this);