我已经实现了一个名为CSnackBar的类包含Snackbar实现,我有一个名为“A”的Activity,它包含listview,并且它的适配器被称为“A Adapter”我在Adapter类中实现了click事件...场景是像那样点击列表视图项目我想检查互联网是否可用,如果是,那么想导航到另一个活动,如果否则想要显示小吃栏消息.....但当我试图显示小吃栏消息从适配器它给我错误: -
SnackBar类: -
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;
}
}
以下是日志错误: -
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)
答案 0 :(得分:2)