gpt4 book ai didi

android - 无法在 ViewPager 中设置文本

转载 作者:行者123 更新时间:2023-11-29 18:11:24 25 4
gpt4 key购买 nike

我的 ViewPager 工作正常,但我现在试图在我的 ViewPager 中的图像顶部添加一个 TexView,因此文本被“附加”到寻呼机。
我可以很好地定义 TexView,但是当我尝试 setText() 时,它会在此行返回 NullPointerException:
score.setText("TEST");

这是我的适配器的代码:

  private class MyPagerAdapter extends PagerAdapter {

public int getCount() {
return 5;
}

public Object instantiateItem(View collection, int position) {

LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

int resId = 0;
switch (position) {
case 0:
resId = R.layout.farleft;

score = (TextView) findViewById(R.id.textView1);
**EXCEPTION HERE** score.setText("TEST");

break;
case 1:
resId = R.layout.left;
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
break;
}

View view = inflater.inflate(resId, null);
view.setTag(position);

view.setOnClickListener(new OnClickListener() {
public void onClick(View view) {

int position = (Integer) view.getTag();
Log.v("Log_tag", "Here image is clicked" + position);

if(position == 0){
Intent intent = new Intent(LogoQuizActivity.this,
LevelOne.class);
startActivity(intent);

}

if(position == 1){
Intent intent = new Intent(getBaseContext(),
LevelTwo.class);
startActivity(intent);
}

if(position == 2){
Intent intent = new Intent(getBaseContext(),
LevelThree.class);
startActivity(intent);
}

if(position == 3){
Intent intent = new Intent(getBaseContext(),
LevelFour.class);
startActivity(intent);
}

if(position == 4){
Intent intent = new Intent(getBaseContext(),
LevelFive.class);
startActivity(intent);
}
}
});


((ViewPager) collection).addView(view, 0);

return view;


}

最佳答案

使用这个

View view = (View) inflater.inflate(R.layout.your_layout, null);

并找到 TextView 如

score = (TextView) view.findViewById(R.id.textView1);
score.setText("TEST");

关于android - 无法在 ViewPager 中设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10451091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com