在寻呼机适配器

时间:2018-06-16 08:08:00

标签: android arrays android-studio android-viewpager android-pageradapter

我正在Android应用中创建Slider适配器,我必须使用strings.xml中的字符串。我认为使用 context.getResources()。getStringArray()会对我有所帮助,但它不起作用。我对listAdapter有同样的问题,但我认为我在两种情况下犯同样的错误。 你有什么想法吗?

Slider adapter.class

public class SliderAdapter extends PagerAdapter {

static Resources res = null;
Context context;
LayoutInflater layoutInflater;


public SliderAdapter(Context context)
{
    this.context = context;

}

public int [] slide_images = {
        R.drawable.ecu_start,
        R.drawable.ecu_version


};

String[] slide_headings = context.getResources().getStringArray(R.array.p06_heading);


String[] slide_descs = context.getResources().getStringArray(R.array.p06_desc);


@Override
public int getCount() {
    return slide_headings.length;
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == (RelativeLayout) object;

}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.slide_layout, container, false);




    ImageView slideImageView = (ImageView) view.findViewById(R.id.slide_image);
    TextView slideHeading = (TextView) view.findViewById(R.id.slide_heading);
    TextView slideDescription = (TextView) view.findViewById(R.id.slide_desc);

    slideImageView.setImageResource(slide_images[position]);
    slideHeading.setText(slide_headings[position]);
    slideDescription.setText(slide_descs[position]);

    container.addView(view);

    return view;
}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView((RelativeLayout)object);
}
}

logcat的:

  

06-16 01:45:01.834 14492-14492 / com.example.jacek.hondadiagnostic E / AndroidRuntime:FATAL EXCEPTION:main                                                                                      过程:com.example.jacek.hondadiagnostic,PID:14492                                                                                      java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.jacek.hondadiagnostic / com.example.jacek.hondadiagnostic.Tipsy.P06_vtec_conversion}:java.lang.NullPointerException:尝试调用虚方法'android.content.res .Resources对空对象引用的android.content.Context.getResources()'

1 个答案:

答案 0 :(得分:0)

试试这个:

public class SliderAdapter extends PagerAdapter {

static Resources res = null;
Context context;
LayoutInflater layoutInflater;


public SliderAdapter(Context context)
{
    this.context = context;

    slide_headings = context.getResources().getStringArray(R.array.p06_heading);
    slide_descs = context.getResources().getStringArray(R.array.p06_desc);
}

public int [] slide_images = {
        R.drawable.ecu_start,
        R.drawable.ecu_version


};

String[] slide_headings  ;


String[] slide_descs ;


@Override
public int getCount() {
  if(slide_headings==null)return 0;
    return slide_headings.length;
}

@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
    return view == (RelativeLayout) object;

}

@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
    layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.slide_layout, container, false);




    ImageView slideImageView = (ImageView) view.findViewById(R.id.slide_image);
    TextView slideHeading = (TextView) view.findViewById(R.id.slide_heading);
    TextView slideDescription = (TextView) view.findViewById(R.id.slide_desc);

    slideImageView.setImageResource(slide_images[position]);
    slideHeading.setText(slide_headings[position]);
    slideDescription.setText(slide_descs[position]);

    container.addView(view);

    return view;
}

@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    container.removeView((RelativeLayout)object);
}
  }