I';我得到了';android.content.res.Resources android.content.Context.getResources()';
我已经尝试了很多方法来解决这个问题,但不幸的是没有找到解决办法。这是我呼叫寻呼机适配器的主要活动:I';我得到了';android.content.res.Resources android.content.Context.getResources()';,android,Android,我已经尝试了很多方法来解决这个问题,但不幸的是没有找到解决办法。这是我呼叫寻呼机适配器的主要活动: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); sliderAdapter = new _SliderAdapter(Activity.this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
sliderAdapter = new _SliderAdapter(Activity.this);
intro_view_pager = findViewById(R.id.intro_view_pager);
intro_view_pager.setAdapter(sliderAdapter);
}
这是我的寻呼机适配器类。我正在尝试获取资源字符串,但上下文始终为空:
static Context mcontext;
LayoutInflater layoutInflater;
public Wazu_SliderAdapter(Context context){
this.mcontext =context;
}
@Override
public int getCount() {
return slide_headings.length;
}
public String[] slide_headings={
mcontext.getResources().getString(R.string.headng1),
};
public String[] slide_description={
mcontext.getString(R.string.string1),
};
public int[] slide_Images={
R.drawable.image1,
R.drawable.image2,
};
@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) mcontext.getSystemService(mcontext.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.slider_layout,container,false);
ImageView imageView=view.findViewById(R.id.intro_image);
TextView heading_text=view.findViewById(R.id.intro_heading_textView);
TextView description=view.findViewById(R.id.intro_textView_description);
imageView.setImageResource(slide_Images[position]);
heading_text.setText(slide_headings[position]);
description.setText(slide_description[position]);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((RelativeLayout)object);
}
}
而不是方法
实例化项中的这行
layoutInflater=(LayoutInflater) mcontext.getSystemService(mcontext.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.slider_layout,container,false);
像这样使用,
View view = LayoutInflater.from(container.getContext()).inflate(R.layout.slider_layout,container,false);
问题是context
传递的是null
&对于getCount
方法,请使用如下方法:
@Override
public int getCount() {
return mcontext.getResources().getString(R.string.headng1).length;
}
您在这里的声明时间使用上下文
public String[] slide_description={
mcontext.getString(R.string.string1),
};
这就是为什么创建静态上下文静态上下文mcontext
可能会避免重复null的原因,谢谢kahled Lela,但这并没有解决我的解决方案为什么亲爱的Jeel Vankhede谢谢你的帮助请再帮我一点…我想知道如何使用此方法获取资源字符串,因为我仍在获取erorr公共字符串[]幻灯片描述={mcontext.getString(R.String.string1),};你能给我解释一下你在用幻灯片描述做什么吗?幻灯片描述也是字符串,我在viewpager的所有页面中都用这个字符串来解释一些事情