2015-11-14 93 views
1

我有我的問題,其安裝上做他的紅線,寫我:Android的碎片onAttach錯誤

「覆蓋的方法已過時的‘android.support.v4.app.Fragment’」

請幫助我瞭解我做錯了什麼? 你爲所有的幫手!

package com.example.omermalka.memecreator; 

    import android.app.Activity; 
    import android.support.v4.app.Fragment; 
    import android.os.Bundle; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.Button; 
    import android.widget.EditText; 
    /** 
    * Created by omermalka on 14/11/2015. 
    */ 



    public class TopSectionFragment extends Fragment { 

     private static EditText TopText; 
     private static EditText BottomText; 

     TopSectionListener acitivtyCommander; 

     public interface TopSectionListener{ 
      public void createMime(String top , String Bottom); 
     } 


     @Override 
     public void onAttach(Activity activity) { 
      super.onAttach(activity); 
      try{ 
       acitivtyCommander = (TopSectionListener) activity; 
      }catch (ClassCastException e){ 
       throw new ClassCastException(activity.toString()); 
      } 
     } 

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View view = inflater.inflate(R.layout.top_section_fragment, container, false); 

      TopText = (EditText) view.findViewById(R.id.TopTextInput); 
      BottomText = (EditText) view.findViewById(R.id.BottomTextInput); 
      final Button button = (Button) view.findViewById(R.id.BottomTextInput); 

      button.setOnClickListener(
        new View.OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          buttonClicked(v); 
         } 
        } 
      ); 



      return view; 
    } 


     public void buttonClicked(View v) { 
      acitivtyCommander.createMime(TopText.getText().toString(),BottomText.getText().toString()); 
     } 

    } 
+0

看到這一點:http://stackoverflow.com/questions/ 32083053 /機器人片段的onattach-deprec ated –

回答

2

您需要更改

public void onAttach(Activity activity) 

public void onAttach(Context context) 

最終代碼:

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    try { 
     acitivtyCommander = (TopSectionListener) context; 
    } catch (ClassCastException e){ 
     throw new ClassCastException(context.toString()); 
    } 
} 
+0

我改變了它,現在我有關於附加活動的錯誤 –

+0

檢查我的更新 –

+0

ty爲你所有的幫助! –