Raghav
Raghav

Reputation: 137

How to show Snackbar message in activity?

I have Implemented a class known as CSnackBar contains Snackbar implementation and I have an Activity called "A" which contain listview and having adapter of it know as "A Adapter" I have implemented click event in Adapter class ...Scenario is like that On click listview Item I want to check whether Internet is available or not if yes then want to navigate to another activity and if No then want to show a snackbar message.....but when I am trying to show snack bar message from adapter its gives me error:-

SnackBar class:-

 private static CSnackBar s_m_oCSnackBar;// declare snack bar variable

    public static CSnackBar getInstance() {// creating Instance of nack bar
        if (s_m_oCSnackBar == null) {
            s_m_oCSnackBar = new CSnackBar();
           }
        return s_m_oCSnackBar;
      }

    public void showSnackBarError(View v, String message, Context context) {
        // parametrised constructor

        Snackbar snack = Snackbar.make(v, "" + message, Snackbar.LENGTH_SHORT);
        View view = snack.getView();
        TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
        tv.setTextColor(Color.WHITE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        else
            tv.setGravity(Gravity.CENTER_HORIZONTAL);
        ColoredSnackbar.alert(snack).show();
    }

    public void showSnackBarSuccess(View v, String message, Context context) {

        Snackbar snack = Snackbar.make(v, "" + message, Snackbar.LENGTH_SHORT);
        View view = snack.getView();
        TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
        tv.setTextColor(Color.WHITE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        else
            tv.setGravity(Gravity.CENTER_HORIZONTAL);
        ColoredSnackbar.confirm(snack).show();
    }


Adapter class code for Onclick:-

    private final Context m_Context;// declaring context variable
    private final ArrayList<CDealAppDatastorage> s_oDataset;// declaring array list ariable

    public CDealAppListingAdapter(Context m_Context, ArrayList<CDealAppDatastorage> mDataList) {
        this.m_Context = m_Context;
        s_oDataset = mDataList;
    }

    @Override
    public int getCount() {// get total arraylist size
        return s_oDataset.size();
    }

    @Override
    public Object getItem(int position) {// get item position in array list
        return s_oDataset.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"SetTextI18n", "InflateParams"})
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.deallisting_card_view, null);
            viewHolder.m_Header = (TextView) convertView.findViewById(R.id.headingText);
            viewHolder.m_Subheader = (TextView) convertView.findViewById(R.id.subHeaderText);
            viewHolder.m_DummyText = (TextView) convertView.findViewById(R.id.subHeadingText);
            viewHolder.m_logoImage = (ImageView) convertView.findViewById(R.id.appImage);
            viewHolder.m_getBtn = (Button) convertView.findViewById(R.id.getDealBtn);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.m_getBtn.setOnClickListener(new View.OnClickListener() {// onclick getDeal Btn
            @Override
            public void onClick(View v) {//send to deal detail page onclick getDeal Btn
                if (NetworkUtil.isConnected(m_Context)){
                    Intent i = new Intent(v.getContext(), CDealAppListingDetails.class);
                    i.putExtra("DealCode", s_oDataset.get(position).getM_szsubHeaderText());// get deal code from deal data storage
                    i.putExtra("headerText", s_oDataset.get(position).getM_szHeaderText());// get deal name from deal dta storage
                    v.getContext().startActivity(i);
                }else {
                    /*here I am getting error*/CSnackBar.getInstance().showSnackBarError(v.findViewById(R.id.mainLayout),"No Internet connection available",m_Context);
                }

            }
        });
        CDealAppDatastorage m = s_oDataset.get(position);
        viewHolder.m_Header.setText(m.getM_szHeaderText());
        viewHolder.m_Subheader.setText(m.getM_szsubHeaderText());
        viewHolder.m_getBtn.setText("GET " + m.getM_szDealValue() + " POINTS");// set deal button text
        viewHolder.m_logoImage.setImageResource(m.getM_n_Image());// set deal logo

        String source = "Dummy text is text that is used in the publishing industry or by web designers to occupy space.";
        viewHolder.m_DummyText.setText(Html.fromHtml(source));

        return convertView;
    }

    private class ViewHolder {
        public TextView m_Header, m_Subheader, m_DummyText;
        public ImageView m_logoImage;
        public Button m_getBtn;
    }
}

Here is log error:-

Process: com.example.devui1.rewardapp, PID: 11205
java.lang.NullPointerException
at android.support.design.widget.Snackbar.<init>(Snackbar.java:188)
at android.support.design.widget.Snackbar.make(Snackbar.java:220)
at com.example.devui1.rewardapp.snackbar.CSnackBar.showSnackBarError(CSnackBar.java:26)
at com.example.devui1.rewardapp.adapter.CDealAppListingAdapter$1.onClick(CDealAppListingAdapter.java:78)

Upvotes: 2

Views: 6745

Answers (1)

0xDEADC0DE
0xDEADC0DE

Reputation: 2473

This line is your problem

/*here I am getting error*/CSnackBar.getInstance().showSnackBarError(v.findViewById(R.id.mainLayout),"No Internet connection available",m_Context);

The variable v represents the Button you clicked. A Button does not derive from ViewGroup hence it cannot have any childs. Therefore findViewById will return null, which you pass into the make function of SnackBar. If you want to pass the main layout, you have to change that line to this

CSnackBar.getInstance().showSnackBarError(findViewById(R.id.mainLayout),"No Internet connection available",m_Context);

This will try to find a View with id mainLayout in the layout you set by setContentView

Upvotes: 2

Related Questions