我有第一个片段和第二个片段,我希望从第二个片段在frist fragent中使用接口回调制作一些东西,但我发现此错误
java.lang.NullPointerException: Attempt to invoke interface method
接口
public interface MyProfileCallback
{
void callbackCall(Context con);
}
第一个片段:
public class firstfragment extends Fragment implements MyProfileCallback
{
public firstfragment()
{
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.fisrt, container, false);
/
...
/
}
@Override
public void callbackCall(Context con)
{
Toast.makeText(getActivity(), "calllllllllll baaaaaaaaaaaaack",
Toast.LENGTH_SHORT).show();
}
}
第二个片段:
public class secondfragment extends Fragment
{
MyProfileCallback mcallback;
public secondfragment()
{
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.second, container, false);
mcallback.callbackCall(getContext());
return rootview;
}
答案 0 :(得分:1)
在第二个片段中添加此内容,
mcallback=refernce of your First Frgament;
并调用此方法:
mcallback.callbackCall(getContext());