在动态创建的片段之间进行通信,给出空指针异常

时间:2013-12-17 12:49:41

标签: java android android-fragments

我创建了一个ListFragment并在每个项目的onItemClick上创建了我尝试将列表的值打开到其他Fragment中。

我已经成功创建了两个fragments,并通过谷歌文档中描述的活动通过interface与他们进行了沟通。

但是,当我尝试在每个项目点击列表上创建新Fragment并通过界面进行通信时,我在该视图上获得Null Pointer Exception

public class Details extends Fragment {

    ViewGroup view_details;

    static int count;

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

        view_details = (ViewGroup) inflater.inflate( R.layout.details, container , false);

        return view_details;

    }

    public void setTextView(String details_string){

        Log.d("TAG", ""+details_string.toString());

        TextView txt_view_details = (TextView) view_details.findViewById(R.id.txt_details);

           //Getting Null POinter Here

        txt_view_details.setText(details_string.toString());

    }
}

我的界面将新片段插入FrameLayout的方法是:

@Override
    public void connectIt(String time) {

        Details details = new Details();

        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

        fragmentTransaction.replace( R.id.frameLayout , details );

        fragmentTransaction.addToBackStack(null);

        if (details != null ) {

            details.setTextView(time);

        }

        fragmentTransaction.commit();

    }


12-17 18:18:06.740: E/AndroidRuntime(15917): FATAL EXCEPTION: main
12-17 18:18:06.740: E/AndroidRuntime(15917): java.lang.NullPointerException
12-17 18:18:06.740: E/AndroidRuntime(15917):    at com.raweng.fragmentdemo.Details.setTextView(Details.java:31)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at com.raweng.fragmentdemo.FragmentDemo.connectIt(FragmentDemo.java:46)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at com.raweng.fragmentdemo.ListData.updateIt(ListData.java:41)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at com.raweng.fragmentdemo.ListData.onListItemClick(ListData.java:34)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at android.widget.AbsListView.performItemClick(AbsListView.java:1128)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2812)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at android.widget.AbsListView$1.run(AbsListView.java:3571)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at android.os.Handler.handleCallback(Handler.java:725)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at android.os.Looper.loop(Looper.java:153)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at android.app.ActivityThread.main(ActivityThread.java:5297)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at java.lang.reflect.Method.invokeNative(Native Method)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at java.lang.reflect.Method.invoke(Method.java:511)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-17 18:18:06.740: E/AndroidRuntime(15917):    at dalvik.system.NativeStart.main(Native Method)

任何帮助都得到了真正的赞赏......

1 个答案:

答案 0 :(得分:4)

这是因为当你调用details.setTextView(time)时;片段的视图尚未创建。 你应该把时间作为参数传递给片段。在您的详细碎片类中创建此方法

 public static Detail newInstance(String time) {
        Detail fragment = new Detail();
        Bundle args = new Bundle();
        args.putString("time", time);
        fragment.setArguments(args);
        return fragment;
 }

然后你可以花时间在onCreateView中设置它:

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

        view_details = (ViewGroup) inflater.inflate( R.layout.details, container , false);
        TextView txt_view_details = (TextView) view_details.findViewById(R.id.txt_details);
        txt_view_details.setText(getArguments().getString("title"));
        return view_details;

    }

然后在你的活动中创建这样的片段:

@Override
    public void connectIt(String time) {

        Details details = Details.newInstance(time);

        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

        fragmentTransaction.replace( R.id.frameLayout , details );

        fragmentTransaction.addToBackStack(null);

        fragmentTransaction.commit();

    }