1

私のフラグメント内に新しいTextViewを追加します。これはasynctaskのbackgroundjobクラスで私のコードです:AsyncTaskでフラグメント化する新しいTextViewを設定します

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

そして、これは私のフラグメントのクラスです:HomeフラグメントとTopicBackgroundTask二つの異なるクラス

これです:は

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

NOTE私のために働かない。なぜ誰かが私に言うことができますか?あなたはonPostExecute()

に再びレイアウトを膨らませる必要はありません

答えて

0

あなたは はonCreateView()でそれを設定し、 はそうあなたがグローバル変数ビューを作成する必要があります、onCreateView()に与えられたレイアウトを再利用する必要がありますそれを直接返すのではなく、以下のような

から
myView = inflater.inflate(R.layout.fragment_home, container, false);

onPostExecute()で、

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(); 
       } 
+0

'Home'フラグメントと' TopicBackgroundTask'は2つの異なるクラスです** **あなたは 'onCreateView()から' View'を呼び出すことはできません 'getActivity()。setContentView(mContainer);'を追加しようとしました作品が断片の外側に表示されます –

0

私ははあなたがViewGroup

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 { 
       ((ViewGroup)activity.findViewById(R.id.mylinearLayout)).addView(text); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
Layout Viewをキャストしなければなりません解決策を見つけました
関連する問題