[android]获得 view 方法给空指针异常 fragment 吗?

发布时间: 2016/10/10 21:17:58
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这是我 Adapter 类:-

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);

    }

}

这是我 Fragment 类:-

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;
    }

当用户点击回收 view 项目弹出 window showing.but 我的问题是什么时候回收用户单击 view 项目和不要按确定 button 点击后面 button 的电话所以流行 window 前显示 activity.i 不想要我想要那个当用户单击背面 button 流行起来 window 解雇 automatically.please 告诉我任何想法,我试过 amny 方法。

解决方法 1:

试着用替换

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

popupWindow.setOutsideTouchable(true);
    popupWindow.setFocusable(true);
赞助商