我可以将数据从孩子那里拉出来,然后给它敬酒。但是,如果我尝试使用Communicator接口,它将立即崩溃。 Communicator对于我所有其他的Fragments和FragmentLists都可以正常工作。无论我从ExpandableListView片段在哪里调用基本的toastMessage方法,它都会不断崩溃。

/**
* Created by petakirikiri on 3/03/16.
*/
public class ThingsInputFragmentExpandableList extends Fragment {
View v;
HashMap<String, List<String>> movies_category;
List<String> movies_list;
ExpandableListView Exp_list;
MoviesAdapter adapter;
Communicator comm;

public ThingsInputFragmentExpandableList() {

}

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


    v =  inflater.inflate(R.layout.movies_expandablelist_fragment, container, false);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    super.onCreate(savedInstanceState);

    Exp_list = (ExpandableListView) v.findViewById(R.id.exp_list);
    movies_category = ThingsDataProvider.getInfo();
    movies_list = new ArrayList<String>(movies_category.keySet());
    adapter = new MoviesAdapter(getActivity(), movies_category, movies_list);
    Exp_list.setAdapter(adapter);
    Exp_list.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getActivity(), movies_list.get(groupPosition) + " is expanded", Toast.LENGTH_LONG).show();
        }
    });

    Exp_list.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getActivity(), movies_list.get(groupPosition) + " is collapsed", Toast.LENGTH_LONG).show();
        }
    });

    Exp_list.setOnChildClickListener(new       ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            if (movies_list.get(groupPosition) == "Determiners"){
                comm.toastMessageTest();
            }
            else if (movies_list.get(groupPosition) == "Things"){


            }
            else if (movies_list.get(groupPosition) == "Adjectives"){


            }

            return false;
        }
    });
}
}

public interface Communicator {
public void toastMessageTest();
public void setIntransitiveFragment();
public void setTransitiveFragment();
public void setPronounFragment();
public void setThingFragment();
public void setLocationFragment();
public void setVerb(String data);
public void setTense(String data);
public void setSubject(String data);
public void setDeterminerSubject(String data);
public void setThingSubject(String data);
public void setAdjectiveSubject(String data);
}

public class MainActivity extends Activity implements Communicator{

@Override
public void toastMessageTest() {
    Toast.makeText(this, "This works", Toast.LENGTH_LONG).show();
}
}

最佳答案

我认为您在Fragment中每次都有空指针异常,因为您没有初始化和设置

Communicator comm;


您需要重写片段中的onAttach
例:

 @Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof Communicator )
        conn = (Communicator )context;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (activity instanceof Communicator )
        conn= (Communicator)activity;
}

07-24 21:23