NullPointerException - 替换FrameLayout中的片段

时间:2013-12-14 13:48:57

标签: android android-fragments

我正在尝试用另一个片段替换现有片段。我正在使用FrameLayout,以便我可以交换片段。

在显示之前,我必须将一些数据设置为新片段。但是,我得到一个NullPointerException。

我对Fragments很新,所以请帮助我。我已经查看了关于SO的其他问题,但是,我无法解决我的问题

Logcat :::

12-14 19:15:08.577: E/AndroidRuntime(10726): FATAL EXCEPTION: main
12-14 19:15:08.577: E/AndroidRuntime(10726): java.lang.NullPointerException
12-14 19:15:08.577: E/AndroidRuntime(10726):    at com.xx.xxx.wallet.WalletNotRedeemedFragment.setParameters(WalletNotRedeemedFragment.java:29)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at com.xx.xxx.wallet.WalletListActivity.onWalletListItemClick(WalletListActivity.java:70)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at com.xx.xxx.wallet.WalletListFragment$1.onItemClick(WalletListFragment.java:108)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at android.widget.AbsListView$1.run(AbsListView.java:3463)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at android.os.Handler.handleCallback(Handler.java:730)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at android.os.Looper.loop(Looper.java:137)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at android.app.ActivityThread.main(ActivityThread.java:5103)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at java.lang.reflect.Method.invokeNative(Native Method)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at java.lang.reflect.Method.invoke(Method.java:525)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-14 19:15:08.577: E/AndroidRuntime(10726):    at dalvik.system.NativeStart.main(Native Method)

请查看以下代码......

    Bundle bundle = new Bundle();
    bundle.putString("imagepath", imagePath);
    bundle.putString("title", title);
    bundle.putString("desc", desc);

    WalletNotRedeemedFragment frag = new WalletNotRedeemedFragment();
    frag.setParameters(bundle);

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.wallet_detail_container, frag).commit();

WalletNotRedeemed.java

public class WalletNotRedeemedFragment extends SherlockFragment {

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

        View view = inflater.inflate(R.layout.fragment_wallet_not_redeemed,
                container, false);
        return view;
    }

    public void setParameters(Bundle bundle) {
    // Null Pointer Exception here...
        TextView title = (TextView) getView().findViewById(
                R.id.textview_coupon_title_nr);
        title.setText(bundle.getString("title"));

        TextView desc = (TextView) getView().findViewById(
                R.id.textview_coupon_description_nr);
        desc.setText(bundle.getString("desc"));

        ImageView image = (ImageView) getView().findViewById(
                R.id.imageview_coupon_image_nr);
        image.setImageURI(Uri.parse(bundle.getString("imagepath")));

    }

}

根据我的理解,通过查看其他问题,该片段尚未加载,因此当我尝试访问TextView时,我得到NullPointerException,但我不知道如何让它工作

非常感谢您的帮助..谢谢!


回答:::

我想,我让它发挥作用......但是,它可能不是最好的方式......

这就是我所做的......

public class WalletNotRedeemedFragment extends SherlockFragment {

    TextView title, desc;
    ImageView image;

    String strTitle, strDesc, strImage;

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

        View view = inflater.inflate(R.layout.fragment_wallet_not_redeemed,
                container, false);

        title = (TextView) view.findViewById(R.id.textview_coupon_title_nr);
        title.setText(strTitle);

        desc = (TextView) view
                .findViewById(R.id.textview_coupon_description_nr);
        desc.setText(strDesc);

        image = (ImageView) view.findViewById(R.id.imageview_coupon_image_nr);
        image.setImageURI(Uri.parse(strImage));

        return view;
    }

    public void setParameters(Bundle bundle) {

        strTitle = bundle.getString("title");
        strDesc = bundle.getString("desc");
        strImage = bundle.getString("imagepath");

    }

}

2 个答案:

答案 0 :(得分:0)

声明OnCreateView方法中的所有视图字段

  TextView title;
  TextView desc;
  ImageView image;

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

    View view = inflater.inflate(R.layout.fragment_wallet_not_redeemed,
            container, false);

    title = (TextView) view.findViewById(
            R.id.textview_coupon_title_nr);

    desc = (TextView) view.findViewById(
            R.id.textview_coupon_description_nr);

    image = (ImageView) view.findViewById(
            R.id.imageview_coupon_image_nr);

    return view;
}

答案 1 :(得分:0)

这是因为视图返回的 onCreateView()方法。

setParameters 方法中,您引用了 getView() 来查找UI元素的ID。

所以改变

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

    View view = inflater.inflate(R.layout.fragment_wallet_not_redeemed,
            container, false);
    TextView title = (TextView) view .findViewById(
            R.id.textview_coupon_title_nr);


    TextView desc = (TextView) view .findViewById(
            R.id.textview_coupon_description_nr);


    ImageView image = (ImageView) view ).findViewById(
            R.id.imageview_coupon_image_nr);

    return view;
}

在setParameters中的方法:

public void setParameters(Bundle bundle) {
// Null Pointer Exception here...

    title.setText(bundle.getString("title"));


    desc.setText(bundle.getString("desc"));


    image.setImageURI(Uri.parse(bundle.getString("imagepath")));

}