获取视图方法在片段中给出空指针异常?

时间:2016-09-14 14:40:48

标签: android android-fragments

这是我的适配器类: -

public class PgListAdapter extends RecyclerView.Adapter<PgListAdapter.FaqViewHolder> {

    private Context mContext;
    private List<Faq> faqList1;
    private ValueAnimator mAnimator;
    private int updatePosition = -1;
    private int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    private int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    private Faq ci;
    private PopupWindow popupWindow;
    public static View popupView;
    private Boolean Flag;


    public PgListAdapter(PgList pgList, Context context, List<Faq> faqList) {
        mContext = context;
        this.faqList1 = faqList;


    }





    @Override
    public FaqViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_pglist, parent, false);
        FaqViewHolder vh = new FaqViewHolder(v);

        return vh;


    }

    @Override
    public void onBindViewHolder(final FaqViewHolder holder, int position) {
        ci = faqList1.get(position);
        holder.questionTv.setText(ci.getQuestion());
        holder.answerTextView.setText(ci.getAnswer());
        holder.Phone.setText(ci.getPhone());
        holder.pgAddress.setText(ci.getAddress());


        holder.ll_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

           showFilter();


            }
        });

        holder.Phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + holder.Phone.getText()));
                mContext.startActivity(intent);

            }
        });


        if (ci.isPGFull()) {
            holder.pgstatus.setVisibility(View.VISIBLE);
        } else {
            holder.pgstatus.setVisibility(View.INVISIBLE);
        }

    }


    @Override
    public int getItemCount() {
        return faqList1 == null ? 0 : faqList1.size();
    }




    class FaqViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout faqItemView;
        private RelativeLayout extendedView;
        private TextView questionTv, Phone;
        private TextView answerTextView, pgstatus, pgAddress;
        private LinearLayout ll_top;
        private LinearLayout ll_layout1;
        private ImageView arrowImgView;
        private int holderPosition;

        public FaqViewHolder(View itemView) {
            super(itemView);
            Phone = (TextView) itemView.findViewById(R.id.pgPhoneNo1);
            questionTv = (TextView) itemView.findViewById(R.id.pgOwner);
            answerTextView = (TextView) itemView.findViewById(R.id.pgPhoneNo);
            pgstatus = (TextView) itemView.findViewById(R.id.pgstatus);
            pgAddress = (TextView) itemView.findViewById(R.id.pgAddress);
            ll_top = (LinearLayout) itemView.findViewById(R.id.ll_top);
            ll_layout1 = (LinearLayout) itemView.findViewById(R.id.ll_layout1);




        }
    }

这是我展示PopUpWindow的方法: -

public void showFilter() {


        popupView = LayoutInflater.from(mContext).inflate(R.layout.pglist_popup, null);

        popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);


        final TextView tv_phone = (TextView) popupView.findViewById(R.id.tv_phone);
        final LinearLayout ll_layout = (LinearLayout) popupView.findViewById(R.id.ll_layout);
        final LinearLayout ll_top = (LinearLayout) popupView.findViewById(R.id.ll_top);
        TextView tv_address = (TextView) popupView.findViewById(R.id.tv_address);
        final TextView btn_ok = (TextView) popupView.findViewById(R.id.btn_ok);

        // popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(false);


        tv_phone.setText(ci.getPhone());
        tv_address.setText(ci.getAddress());


        tv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + tv_phone.getText()));
                mContext.startActivity(intent);


            }
        });


        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                popupWindow = null;


            }
        });


        popupWindow.showAsDropDown(popupView, 0, 0);

    }

}

这是我的片段类: -

public class PgList extends Fragment implements WebServiceListener {


    private String responseCodeCameFromServer = "null";
    private String responseCameFromServer, responseMessageCameFromServer;
    private RecyclerView rvFaqs;
    private List<Faq> faqList = new ArrayList<>();
    private List<NameValuePair> basicNameValuePair;
    private GeneralUtilities generalUtilities;
    private SharedPreferencesUtilities sharedPreferencesUtilities;
    PgListAdapter pgAdapter;
    private View rootView;
    private TextView empty_view;
    ProgressDialog br;
    private PgList pgList;


    List<String> typelist = new ArrayList<String>();
    List<String> citylist = new ArrayList<String>();
    List<CityAreaDetails> temp = new ArrayList<>();

    Spinner type;
    WebServiceHandler webServiceHandler;


    public PgList() {

    }

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


        if (rootView == null) {
            rootView = inflater.inflate(R.layout.pglist, container, false);


            pgList = new PgList();
            generalUtilities = new GeneralUtilities(getActivity());
            sharedPreferencesUtilities = new SharedPreferencesUtilities(getActivity());
            br = new ProgressDialog(getContext());
            br.setTitle("Loading...");
            br.setCancelable(false);


            empty_view = (TextView) rootView.findViewById(R.id.tv_empty_view);

            empty_view.setText("Sorry for the Inconvience! We will update the list very soon for this Area. ");
            getActivity().setTitle("PG/Room List");
            typelist.add("Boys");
            typelist.add("Girls");
            citylist.clear();
            temp.clear();


            rvFaqs = (RecyclerView) rootView.findViewById(R.id.overviewRv);
            rvFaqs.setHasFixedSize(true);
            rvFaqs.addItemDecoration(new SpacesItemDecoration(20, 20, 12));
            rvFaqs.setLayoutManager(new LinearLayoutManager(getActivity()));

             rvFaqs.setAdapter(new PgListAdapter(getActivity(), faqList));
            webServiceHandler = new WebServiceHandler(getActivity());
            webServiceHandler.webServiceListener = PgList.this;
            HashMap<String, String> formData = new HashMap<>();
            formData.put("Type", "Boys");
            formData.put("CityAreaDetailsID", "1");
            if (br != null) {
                br.show();
            }
            //Checking internet connectivity and then requesting to the server
            Log.e("", "Data :" + formData);
            if (generalUtilities.isConnected()) {
                webServiceHandler.requestToServer((getResources().getString(R.string.api_end_point)) + "CityDetails",
                        WebService.ORDER, formData, true);
            } else {
                if (br != null) {
                    br.dismiss();
                }
                generalUtilities.showAlertDialog("Error", getResources().getString(R.string.internet_error), "OK");
            }

        }


        return rootView;
    }

当用户点击回收器视图项时弹出窗口显示。但我的问题是当用户点击回收器视图项并且没有按下确定按钮并单击手机的后退按钮时弹出窗口正在上一个活动中显示。我不想要它。我希望当用户点击后退按钮时,弹出窗口会自动解除。请告诉我任何想法我试过amny方法。

2 个答案:

答案 0 :(得分:0)

什么是PgList?这是一个观点?因为您需要使用id识别它。

编辑:如果您没有从活动中覆盖onBackPressed()方法,则按下后退按钮时,通常会自动关闭对话框。 无论如何,您可以为对话框实现自己的侦听器,但对于对话框而不是整个视图:

dialog.setOnKeyListener(new Dialog.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {        
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dialog.dismiss();
        }
        return true;
    }
});

答案 1 :(得分:0)

尝试替换

popupWindow.setOutsideTouchable(false);
popupWindow.setFocusable(false);

popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);