Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在.xml中初始化的TextView数组的getText()_Android_Textview_Nullpointerexception_Gettext - Fatal编程技术网

Android 在.xml中初始化的TextView数组的getText()

Android 在.xml中初始化的TextView数组的getText(),android,textview,nullpointerexception,gettext,Android,Textview,Nullpointerexception,Gettext,我已经将TextView数组定义为final。 我为所有数组元素设置了OnClickListener(), 而且我能够使用setBackground(即使阵列未设置为最终阵列)。 但我不能使用getText()方法,该方法在操作期间返回NullPointerException 你们知道为什么会有这个问题吗 final ArrayList<TextView> seats = new ArrayList<TextView>(); @Override public void

我已经将TextView数组定义为final。 我为所有数组元素设置了OnClickListener(), 而且我能够使用setBackground(即使阵列未设置为最终阵列)。 但我不能使用getText()方法,该方法在操作期间返回NullPointerException

你们知道为什么会有这个问题吗

final ArrayList<TextView> seats = new ArrayList<TextView>();

@Override
public void onCreate(Bundle savedInstanceState) {
    .
    .
    seats.add((TextView) findViewById(R.id.A1));
    seats.add((TextView) findViewById(R.id.A2));
    seats.add((TextView) findViewById(R.id.A3));
    .
    .
    for (int i = 0; i<seats.size(); i++)
        seats.get(i).setOnClickListener(seatClick);
    .
    .
}
OnClickListener seatClick = new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        v.setBackgroundColor(-16711681);
        Toast.makeText(getApplicationContext(), ((TextView)v).getText().toString(), Toast.LENGTH_SHORT);
    }
};
final ArrayList seats=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
.
.
添加((文本视图)findViewById(R.id.A1));
添加((文本视图)findViewById(R.id.A2));
添加((文本视图)findViewById(R.id.A3));
.
.

对于(inti=0;i我测试了您的代码,它工作正常。只需更改它

 Toast.makeText(getApplicationContext(), ((TextView)v).getText().toString(), Toast.LENGTH_SHORT).show();
这是完整的代码

seats.add((TextView) findViewById(R.id.textView2));
            seats.add((TextView) findViewById(R.id.textView3));
            seats.add((TextView) findViewById(R.id.textView4));



            OnClickListener seatClick = new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                  v.setBackgroundColor(-16711681);
                      Toast.makeText(getApplicationContext(), ((TextView)v).getText().toString(), Toast.LENGTH_SHORT).show();
                }
            };

            for (int i = 0; i<seats.size(); i++)
                seats.get(i).setOnClickListener(seatClick);
seats.add((TextView)findViewById(R.id.textView2));
添加((TextView)findViewById(R.id.textView3));
添加((TextView)findViewById(R.id.textView4));
OnClickListener seatClick=new OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
v、 挫折背景色(-16711681);
Toast.makeText(getApplicationContext(),((TextView)v).getText().toString(),Toast.LENGTH_SHORT).show();
}
};

for(inti=0;i语句
v.setBackgroundColor(-16711681);
如您所说成功运行,因此您的
v
对象是完美的,而不是
null
。我猜您的getText方法在这里
((TextView)v)。getText()
返回
null
,因此调用
toString()
on
null
会导致NPE。你应该检查它。

@henry你应该调整它,为什么它会返回null。对不起,我想问一下如何调整它?@henry我不是android专家,所以不能告诉你完美的解决方案,你可以尝试的是,看看你在onClick中得到的视图是否正确?文本视图是否正确有没有与之相关的文本?我已经为textfield定义了文本,应用程序可以在屏幕上显示它们..查看上面问题的更新..只是不知道getText()返回null的原因…我很长一段时间都不知道,所以我把它发布在这里
seats.add((TextView) findViewById(R.id.textView2));
            seats.add((TextView) findViewById(R.id.textView3));
            seats.add((TextView) findViewById(R.id.textView4));



            OnClickListener seatClick = new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                  v.setBackgroundColor(-16711681);
                      Toast.makeText(getApplicationContext(), ((TextView)v).getText().toString(), Toast.LENGTH_SHORT).show();
                }
            };

            for (int i = 0; i<seats.size(); i++)
                seats.get(i).setOnClickListener(seatClick);