我想在我的Fragment中添加一個新的TextView
。這是我在的AsyncTask backgroundjob類代碼:設置新的TextView在AsyncTask中分段
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
是兩個不同的類
這不適合我。誰能告訴我爲什麼?
'Home'片段和'TopicBackgroundTask'是兩個不同的**類** +你不能從'onCreateView()'調用'View'我嘗試添加'getActivity()。setContentView(mContainer);'和它工作,但它顯示在片段之外 –