【发布时间】:2016-08-16 04:41:38
【问题描述】:
我想在我的片段中添加一个新的TextView。这是我在 asynctask backgroundjob 类中的代码:
protected void onPostExecute(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("server_response");
JSONObject JO = jsonArray.getJSONObject(0);
String code = JO.getString("code");
String message = JO.getString("message");
if (code.equals("true")) {
/**********************************/
LayoutInflater inflater = activity.getLayoutInflater();
View mContainer = inflater.inflate(R.layout.fragment_home, null);
LinearLayout linearLayout = (LinearLayout)mContainer.findViewById(R.id.mylinearLayout);
TextView text = new TextView(linearLayout.getContext());
text.setText("TEST");
text.setTextSize(30);
Log.d("View", "Start");
try {
linearLayout.addView(text);
} catch (Exception e) {
e.printStackTrace();
}
这是我的片段类:
public class Home extends Fragment {
public Home() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TopicBackgroundTask backgroundTask = new TopicBackgroundTask(getActivity());
backgroundTask.execute("yes");
// Inflate the layout for this
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
注意:Home 片段和TopicBackgroundTask 是两个不同的类
这对我不起作用。谁能告诉我为什么?
【问题讨论】:
标签: java android android-layout android-fragments android-asynctask