2017-05-25 10 views
1

を作成しながら:新しいボタンのコンテキストの問題を動的こんにちは私はアンドロイドで動的にボタンを作成したいが、私はこれに設定されたコンテキストをしようとすると、私はその問題持っ

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); 

}

enter image description here

ここではコードです

誰でも私を助けることができますか?

+1

は、それが活動ではありませんActivityクラスまたは他のクラスとは?

Button myButton = new Button(mContext); // mContext is the context received on WebServiceHandler class 

その後、WebServiceHandlerが&と呼ばれているところからコンテキストを提供 –

+1

おそらく 'this'はActivityのコンテキストではありません。 'MainActivity.this'を使用しようとしましょう またはそれ以上のコードを表示 –

+0

これはプライベートクラスですWebServiceHandlerはAsyncTaskを拡張しました { –

答えて

1

WebServiceHandlerは、あらゆる活動のプライベートクラスであれば、その後、

Button myButton = new Button(YOUR_ACTIVITY.this); 
関連する問題

 関連する問題