如何在List <string> </string>上使用setText()函数

时间:2012-06-07 18:41:48

标签: java android eclipse android-layout nullpointerexception

我正在创建应用程序,其中玩家将他们的名字放入程序中,程序将他们的名字发送到String数组中的下一个活动,然后下一个程序将数组放入一个列表然后将其洗牌。出于某种原因,每次我尝试使用.setText函数时,应用程序都会收到nullpointerexception错误。我已经用toast来测试列表,以验证它不是null,但事实并非如此。有什么建议吗?

endInstructions = (Button)findViewById(R.id.buttonEndInstructions);

    player = (TextView)findViewById(R.id.textViewPlayerName);

    endInstructions.setOnClickListener(this);

    Bundle ext = getIntent().getExtras();
    int numberOfPlayers= ext.getInt("numberofplayers");
    String[] sarray = ext.getStringArray("namearray");

    names = Arrays.asList(sarray);
    Collections.shuffle(names);

    Toast.makeText(this, names.get(0), Toast.LENGTH_SHORT).show();

    //player.setText(names.get(0));

提前致谢!

2 个答案:

答案 0 :(得分:0)

没有一百万种解决方案......

播放器为空,您可以通过测试来检查:

 Toast.makeText(this, player, Toast.LENGTH_SHORT).show();

这是什么意思?可能您当前的布局不包含名为“textViewPlayerName”的文本视图

请仔细检查您的xml布局或确保您之前使用过setContentView; - )

编辑:因为endInstructions对于你的代码不是null,所以你之前正确地使用了setContentView ...

答案 1 :(得分:0)

我认为GUI对象播放器为空。如果放置try / catch块,可以验证这一点,并在导致异常后读取错误消息。

我怀疑GUI对象ID R.id。 textViewPlayerName 不存在或类型 TextView 。因此玩家为空。例如,对象类型可能是 TextField ,或者Android框架中的任何其他有效类型。

祝你好运,让我们知道你的进步。