片段中的getActivity()x2使其崩溃

时间:2016-02-02 14:52:31

标签: android android-fragments

一个带有API调用的简单Android应用程序,这要归功于凌空和一个MenuDrawer。

如果我点击我的" Marks"部分我得到了我想要的东西。 但如果我点击两次我的应用程序崩溃。

public class MarkFragment extends Fragment implements Observer {

    private Marks allMarks;
    private View initView;
    private ListView marksList;
    private View mProgressView;

    public MarkFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        initView = inflater.inflate(R.layout.fragment_mark, container, false);
//        mProgressView = initView.findViewById(R.id.progressBar);
        TaskManager.get().register(ETarget.MARKS_FRAGMENT.getId(), this);

        VolleyRequest.get().doRequest(ETarget.MARKS_FRAGMENT.getId(), RequestFactory.marksRequestGET(UserContext.myToken));
        Log.d("Gilou", "VolleyRequest");
//        showProgress(true);

        return initView;
    }

//    @Override
//    public void onResume() {
//        Log.d("Gilou", "onStart");
//        VolleyRequest.get().doRequest(ETarget.MARKS_FRAGMENT.getId(), RequestFactory.marksRequestGET(UserContext.myToken));
//    }

    @Override
    public void update(Observable observable, Object data) {

        if (data instanceof VolleyError) {
            Log.d("ERROR", "[VolleyRequest] Error: " + data);
        } else if (data instanceof Marks) {
            allMarks = ((Marks) data);
            ArrayAdapter<Mark> adapter = new MarkArrayAdapter();;
            Log.d("Gilou", adapter.toString());

            marksList = (ListView) initView.findViewById(R.id.marks_list);
            marksList.setAdapter(adapter);
//            showProgress(false);
        }
    }

    private class MarkArrayAdapter extends ArrayAdapter<Mark> {

        public MarkArrayAdapter() {
            super(getActivity(), R.layout.mark_item, allMarks.getNotes());
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                v = getLayoutInflater(null).inflate(R.layout.mark_item, parent, false);

                ArrayList<Mark> all = allMarks.getNotes();

                if (all != null) {

                    Mark i = all.get(position);

                    FrameLayout container = (FrameLayout) v.findViewById(R.id.note_container);
                    TextView note = (TextView) v.findViewById(R.id.project_note);
                    TextView corrector = (TextView) v.findViewById(R.id.project_corrector);
                    TextView projectName = (TextView) v.findViewById(R.id.project_name);
                    TextView moduleName = (TextView) v.findViewById(R.id.module_name);

                    if (container != null) {
                        container.setBackgroundColor(getResources().getColor(R.color.myRed));
                    }
                    if (note != null) {
                        note.setText(i.getFinal_note().toString());
                    }
                    if (projectName != null) {
                        projectName.setText(i.getTitle());
                    }
                    if (moduleName != null) {
                        moduleName.setText(i.getTitlemodule());
                    }
                    if (corrector != null) {
                        corrector.setText(i.getCorrecteur());
                    }
                }
            }
            return v;
        }
    }

我得到的错误是:

FATAL EXCEPTION: main                                                 Process: xyz.gfelot.epiandroid, PID: 1891
                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference                                                                              at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
                                                                             at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
                                                                             at xyz.gfelot.epiandroid.fragment.MarkFragment$MarkArrayAdapter.<init>(MarkFragment.java:88)
                                                                             at xyz.gfelot.epiandroid.fragment.MarkFragment.update(MarkFragment.java:76)
     [...]

我不理解错误,所以我不知道如何修复它。

第二次出现getActivity()中的问题。

2 个答案:

答案 0 :(得分:2)

您在活动结束时收到回复,并且您必须检查片段是否附加到活动

@Override
public void update(Observable observable, Object data) {
     if(!isAdded()) return;
     rest your code
}
@Override
    public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
      ==Move here VolleyRequest==
    }

答案 1 :(得分:1)

我真的不知道getActivity()

发生了什么

但是错误消息清楚地告诉您,您尝试访问空指针。

我想你可以试试

public MarkArrayAdapter(Context context) {
        super(context, R.layout.mark_item, allMarks.getNotes());
}

ArrayAdapter<Mark> adapter = new MarkArrayAdapter(getActivity());

如果它不起作用,也许你的allMarks为空。 您可以尝试使用调试模式进行检测。