将listview选中的项目从片段传递到活动

时间:2016-02-26 21:01:15

标签: android android-fragments

我正在尝试将所选名称从ListView传递到Fragment中的MainActivity但我在onItemClick方法中获得空指针异常。 以下是片段中的代码:

getListText getListTextObject;


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

        listView=(ListView) rootView.findViewById(R.id.profile_list);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                nameListItem=(TextView) view;
                String text=nameListItem.getText().toString();

                getListTextObject.getData(text);
            }
        });

        return rootView;
    }

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

        try{
            getListTextObject=(getListText) context;
        }catch (ClassCastException e){
            throw new ClassCastException(context.toString() + " must implement getListText");
        }
    }

    public interface getListText{
        public void getData(String s);
    }

用于空指针异常的StackTrace:

02-25 10:17:14.445 7388-7388/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 java.lang.NullPointerException
                                                     at com.example.user.example.FragmentMain$1.onItemClick(FragmentMain.java:67)
                                                     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
                                                     at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
                                                     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
                                                     at android.widget.AbsListView$1.run(AbsListView.java:3529)
                                                     at android.os.Handler.handleCallback(Handler.java:615)
                                                     at android.os.Handler.dispatchMessage(Handler.java:92)
                                                     at android.os.Looper.loop(Looper.java:137)
                                                     at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:511)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

你的实施很糟糕。您的活动必须实施&#34; getListText &#34;接口。然后你可以打电话给你的片段到你的活动,就像这样:

((getListText)context).getData(text);

并在YourActivity中收到此数据。

This answer更完整。我认为这可以帮助你。