从一个片段到另一个片段的访问方法

时间:2014-07-24 11:07:04

标签: android android-fragments

这是我的细节片段。在这里,我创建了一个公共方法,它将在下面描述的listview Fragment中显示所点击的项目。

public class DetailsFragment extends Fragment {
    TextView text,vers;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.detailsfragment, container, false);
        text= (TextView) view.findViewById(R.id.AndroidOs);
        vers= (TextView)view.findViewById(R.id.Version);
        return view;
    }
    public void change(String txt, String txt1){
        text.setText(txt);
        vers.setText(txt1);
    }
}

我正在尝试从此ListFragment Activity中将值设置为Change(xx,xx)方法:

public class ListMenuFragment extends ListFragment {

    String[] AndroidOS = new String[] { "Cupcake", "Donut", "Eclair", "Froyo",
            "Gingerbread", "Honeycomb", "Ice Cream SandWich", "Jelly Bean",
            "KitKat" };
    String[] Version = new String[] { "1.5", "1.6", "2.0-2.1", "2.2", "2.3",
            "3.0-3.2", "4.0", "4.1-4.3", "4.4" };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.listfragment, container, false);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, AndroidOS);
        setListAdapter(adapter);

        return view;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        DetailsFragment txt = (DetailsFragment) getFragmentManager()
                .findFragmentById(android.R.id.list);
        txt.change(AndroidOS[position], "Version : " + Version[position]);
        getListView().setSelector(android.R.color.holo_blue_dark);
    }

}

列表显示良好。但是当点击列表时,它强制关闭并给出空指针异常:

here : txt.change(AndroidOS[position], "Version : " + Version[position]);

主要活动XML是:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        tools:context="com.example.androidlistfragment.MainActivity">
        <fragment
            android:layout_height="match_parent"
            android:layout_width="240px"
            class="com.example.androidlistfragment.ListMenuFragment"
            android:id="@+id/fragment"/>
        <fragment
            android:layout_width="240px"
            android:layout_height="match_parent"
            class="com.example.androidlistfragment.DetailsFragment"
            android:id="@+id/fragment2"/>
</LinearLayout>

Log cat:

07-24 17:28:22.109: E/AndroidRuntime(22421): FATAL EXCEPTION: main
07-24 17:28:22.109: E/AndroidRuntime(22421): java.lang.NullPointerException
07-24 17:28:22.109: E/AndroidRuntime(22421):    at com.example.androidlistfragment.ListMenuFragment.onListItemClick(ListMenuFragment.java:38)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at android.widget.AbsListView.performItemClick(AbsListView.java:1394)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3024)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at android.widget.AbsListView$1.run(AbsListView.java:3830)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at android.os.Handler.handleCallback(Handler.java:605)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at android.os.Looper.loop(Looper.java:137)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at android.app.ActivityThread.main(ActivityThread.java:4517)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at java.lang.reflect.Method.invokeNative(Native Method)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at java.lang.reflect.Method.invoke(Method.java:511)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-24 17:28:22.109: E/AndroidRuntime(22421):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

如果没有,请查看Android开发者网页,请查看HERE

在快捷方式创建接口片段中实现它在容器(activity)中设置引用和调用方法。

答案 1 :(得分:0)

如果您使用的是支持库,可能会尝试将getFragmentManager()更改为getSupportFragmentManager()

答案 2 :(得分:0)

呃......你的片段是空的,因为你提供了错误的ID。这是您提供片段的ID:fragment2,这是您要查找的片段的ID:android.R.id.list