2017-06-06 106 views
-1

我正在開發一個應用程序,我需要在兩個片段之間進行通信。我知道兩個片段之間的通信是通過使用接口完成的。我在片段1中創建了一個接口並在活動中實現它,但是當我運行該應用程序時,它會崩潰。下面片段通信不起作用make java.lang.nullpointerexception

是我的代碼

片段一個代碼

public class Fragment_one extends Fragment { 

    EditText editText; 
    Button button; 

    SendData sd; 


    @Nullable 
    @Override 
    public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view; 

     view = inflater.inflate(R.layout.fragment_one,container,false); 



     editText = (EditText)view.findViewById(R.id.editText); 

     button = (Button)view.findViewById(R.id.button); 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       String data ; 
       data = editText.getText().toString(); 
       sd.SendData1(data); 
      } 
     }); 

     return view; 

    } 



     interface SendData 
    { 
     String SendData1(String name); 

    } 


    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 


     try{ 

      sd = (SendData)context; 


     }catch (ClassCastException e){ 

      throw new ClassCastException ("You have to impliment it"); 

     } 

    } 
} 

當我調試應用程序,我知道sdnull。但數據正在被設置爲文本。

任何一個可以幫助我走出這個問題

+0

什麼是'SendData'? –

+0

在你的'onAttach()'裏面設置一個斷點,看看爲什麼它是空的。 –

+0

您可以使用接口與碎片進行通信。 –

回答

0

訪問你需要實現定義接口功能之前。

你可以給像

interface SendData { 
public String SendData1(String name); 
} 

可以使用例如像這樣定義一個函數:

SendData sd; 
sd = new SendData() { 
public String SendData(String name) { 
    return name; 
    } 
}; 
+0

現在感謝數據傳遞發送活動 –

0

在第一個片段..

Fragment fragment = new EditExamFragment(); 
       FragmentManager fm = getActivity().getSupportFragmentManager(); 
       FragmentTransaction ft = fm.beginTransaction(); 
       ft.replace(R.id.content_frame, fragment); 
       ft.commit(); 

       Bundle bundle = new Bundle(); 
       bundle.putString("branch_id", mDataset.get(position).getiBranchId()); 

       bundle.putString("exam_id",mDataset.get(position).getiExamId()); 
       fragment.setArguments(bundle); 

在第二塊碎片的獲取值試試下面的代碼;

String branch_id, exam_id ; 


final Bundle bundle = this.getArguments(); 
    if (bundle != null) { 
     branch_id = bundle.getString("branch_id");   
     exam_id = bundle.getString("exam_id"); 
    } 

如果您想了解更多關於捆綁類然後單擊鏈接:https://developer.android.com/reference/android/os/Bundle.html

+0

什麼是'content_frame'? –

0

如果你想從FirasrFragment數據發送到SecondFragment然後用捆綁類來發送數據

//Put the value 
SecondFragment sFrag = new SecondFragment(); 
Bundle args = new Bundle(); 
args.putString("YourKey", "YourValue"); //replace "YourValue" to editText.getext().toString().trim(); 
sFrag.setArguments(args); 

//Inflate the fragment 
getFragmentManager().beginTransaction().add(R.id.container, sFrag).commit(); 

在SecondCragment的onCreateView中:

//Retrieve the value 
String value = getArguments().getString("YourKey"); 

更多地瞭解Bundle Class

+0

什麼是R.id.container? –

+0

R.id.content_frame是來自layout_fragment_second.xml的片段ID –

0

如果你使用的平臺碎片工作在API級別23中加入onAttach(Context)方法(你已經擴展android.app.Fragment)這個方法不會被調用的設備比舊的棉花糖。

改爲替代onAttach(Activity)


如果您正在擴展支持片段(android.support.v4.app.Fragment),不考慮此答案。