2017-06-06 107 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),不考虑此答案。