将应用程序切换到设置然后返回应用程序崩溃

时间:2016-03-31 10:00:53

标签: android android-activity fragment

从应用程序切换到设置以更改字体然后再返回应用程序,应用程序崩溃。

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

    mListView = (SwipeMenuListView) getActivity().findViewById(R.id.transactionManageListView);

    if(mListView==null){
        Log.e("mListView","mListView");
    }

    cal = Calendar.getInstance();
    day = cal.get(Calendar.DAY_OF_MONTH);
    month = cal.get(Calendar.MONTH);
    year = cal.get(Calendar.YEAR);

    transactionDateEnd = (Utils.calculateDate(day) + "/" + Utils.calculateDate(month + 1) + "/" + Utils.calculateDate(year));

    //set range for transaction -3 day's from today
    transactionDateStart = calculateDate(-3);

    // step 1. create a MenuCreator
    SwipeMenuCreator creator = new SwipeMenuCreator() {

        @Override
        public void create(SwipeMenu menu) {
            // create "delete" item
            SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity().getApplicationContext());
            // set item background
            deleteItem.setBackground(new ColorDrawable(Color.rgb(0xba, 0xd7, 0xe2)));
            // set item width
            deleteItem.setWidth(dp2px(70));
            // set a icon
            deleteItem.setIcon(R.drawable.ic_delete);
            // add to menu
            menu.addMenuItem(deleteItem);
        }
    };

    // set creator
    mListView.setMenuCreator(creator);

崩溃报告:

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.baoyz.swipemenulistview.SwipeMenuListView.setMenuCreator(com.baoyz.swipemenulistview.SwipeMenuCreator)' on a null object reference
        at activities.mswift.info.mswift.mswift.transactions.TransactionManageFragment.onActivityCreated(TransactionManageFragment.java:99)

这里mListView获取空指针异常。 Fragment onResume是否有任何问题?

1 个答案:

答案 0 :(得分:0)

onCreateView的{​​{1}}内移动:

fragment

它应该是:

mListView = (SwipeMenuListView) getActivity().findViewById(R.id.transactionManageListView);