将2个arraylist与SharedPref UserId进行比较

时间:2018-06-03 03:44:41

标签: android arraylist sharedpreferences

我想比较来自LatestFragment和ApplyFragment的2个ArrayList。 ApplyFragment包含来自用户的ArrayList。但是,当我比较它时,会出现以下错误。当我比较这个2 ArrayList时,如何从SharedPref获取UserId()?

这是JobAdapter.java中的比较

for (ItemJob itemJob : LatestFragment.mListItem){
            boolean found = false;
            for (ItemJob itemJob2 : ApplyFragment.mListItem) {
                        if (itemJob2.id == itemJob.id)
                        btnApplyJob.setText(R.string.applied);
                    found = true;
                            }
            if(!found){
                btnApplyJob.setText(R.string.apply_now);
            }
        }

ApplyFragment.java

package com.data.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.app.hero.MyApplication;
import com.app.hero.R;
import com.data.adapter.JobAdapter;
import com.data.adapter.JobProviderAdapter;
import com.data.item.ItemJob;
import com.data.util.Constant;
import com.data.util.ItemOffsetDecoration;
import com.data.util.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by laxmi.
 */
public class ApplyFragment extends Fragment {

    public static ArrayList<ItemJob> mListItem;
    public RecyclerView recyclerView;
    JobAdapter adapter;
    private ProgressBar progressBar;
    private LinearLayout lyt_not_found;
    MyApplication MyApp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.row_recyclerview, container, false);
        MyApp = MyApplication.getInstance();
        mListItem = new ArrayList<>();
        lyt_not_found = rootView.findViewById(R.id.lyt_not_found);
        progressBar = rootView.findViewById(R.id.progressBar);
        recyclerView = rootView.findViewById(R.id.vertical_courses_list);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getActivity(), R.dimen.item_offset);
        recyclerView.addItemDecoration(itemDecoration);
        if (JsonUtils.isNetworkAvailable(getActivity())) {
            new getLatest().execute(Constant.USERS_JOB_APPLIED_LIST + MyApp.getUserId());
        }
        return rootView;
    }

    private class getLatest extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showProgress(true);
        }

        @Override
        protected String doInBackground(String... params) {
            return JsonUtils.getJSONString(params[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            showProgress(false);
            if (null == result || result.length() == 0) {
                lyt_not_found.setVisibility(View.VISIBLE);
            } else {
                try {
                    JSONObject mainJson = new JSONObject(result);
                    JSONArray jsonArray = mainJson.getJSONArray(Constant.ARRAY_NAME);
                    JSONObject objJson;
                    for (int i = 0; i < jsonArray.length(); i++) {
                        objJson = jsonArray.getJSONObject(i);
                        ItemJob objItem = new ItemJob();
                        objItem.setId(objJson.getString(Constant.JOB_ID));
                        objItem.setJobName(objJson.getString(Constant.JOB_NAME));
                        objItem.setJobCompanyName(objJson.getString(Constant.JOB_COMPANY_NAME));
                        objItem.setJobDate(objJson.getString(Constant.JOB_DATE));
                        objItem.setJobDateFrom(objJson.getString(Constant.JOB_DATEFROM));
                        objItem.setJobDateTo(objJson.getString(Constant.JOB_DATETO));
                        objItem.setJobDesignation(objJson.getString(Constant.JOB_DESIGNATION));
                        objItem.setJobAddress(objJson.getString(Constant.JOB_ADDRESS));
                        objItem.setJobImage(objJson.getString(Constant.JOB_IMAGE));
                        mListItem.add(objItem);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                displayData();
            }
        }
    }


    private void displayData() {
        adapter = new JobAdapter(getActivity(), mListItem);
        recyclerView.setAdapter(adapter);

        if (adapter.getItemCount() == 0) {
            lyt_not_found.setVisibility(View.VISIBLE);
        } else {
            lyt_not_found.setVisibility(View.GONE);
        }

    }



    private void showProgress(boolean show) {
        if (show) {
            progressBar.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            lyt_not_found.setVisibility(View.GONE);
        } else {
            progressBar.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
    }
}

这是错误

Process: com.app.hero, PID: 25830
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.app.hero.MyApplication.getUserId()' on a null object reference
        at com.data.fragment.ApplyFragment.onCreateView(ApplyFragment.java:55)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5451)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

'java.lang.String com.app.hero.MyApplication.getUserId()' on a null object reference

您收到此错误,因为您的UserId()设置不正确并且您尝试使用getUserId()。 我想一切都很完美只需检查你的sharedPreference有你所有的UserData。