在寻呼机适配器中使用字符串数组
Using String-array in Pager adapter
我正在我的 Android 应用程序中创建 Slider 适配器,我必须使用来自 strings.xml 的字符串。我认为使用 context.getResources().getStringArray() 会对我有所帮助,但它不起作用。我对 listAdapter 有同样的问题,但我认为我在两种情况下犯了同样的错误。
你有什么想法吗?
滑块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
Process: com.example.jacek.hondadiagnostic, PID: 14492
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jacek.hondadiagnostic/com.example.jacek.hondadiagnostic.Tipsy.P06_vtec_conversion}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
试试这个:
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);
}
}
我正在我的 Android 应用程序中创建 Slider 适配器,我必须使用来自 strings.xml 的字符串。我认为使用 context.getResources().getStringArray() 会对我有所帮助,但它不起作用。我对 listAdapter 有同样的问题,但我认为我在两种情况下犯了同样的错误。 你有什么想法吗?
滑块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 Process: com.example.jacek.hondadiagnostic, PID: 14492 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jacek.hondadiagnostic/com.example.jacek.hondadiagnostic.Tipsy.P06_vtec_conversion}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
试试这个:
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);
}
}