setText将粉碎应用程序

时间:2012-03-08 08:25:10

标签: android nullpointerexception settext

嗨,我正在开始我的第一个Android应用程序,并试图让TextView在一段时间后改变了这些词。

public class Game1Activity extends Activity {

    GameViews ngv;
    private static String gameId = "game1Activity-game";
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game1_layout);
        ngv = (GameViews)findViewById(R.id.Game1);
        ngv.setTextView((TextView)findViewById(R.id.Text));
    };

}
public class GameViews extends View
{
    private TextView gameText;
    private long time1 = System.currentTimeMillis();

    public GameViews(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        initGameViews();
    }

    public GameViews(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initGameViews();
    }

    public void setTextView(TextView tv)
    {
        gameText = tv;
    }

    public void initGameViews()
    {
        setFocusable(true);
        Resources r = this.getContext().getResources();
        changeGameText();
    }

    public void changeGameText()
    {
        while (true)
        {
            if (System.currentTimeMillis() - time1 > 100)
            {
                gameText.setText(R.string.hi2);
                return;
            }
        }
    }
}

布局XML文件是:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <game1.code.GameViews
    android:id="@+id/Game1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    tileSize="24" />

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:gravity="center" >

        <TextView
        android:id="@+id/Text"
        android:text="@string/hi1"
        android:visibility="visible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:textColor="#ff8888ff"
        android:textSize="24sp"/>
    </RelativeLayout>
</FrameLayout>

和strings.xml是:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hi1">hi1</string>
    <string name="hi2">hi2</string>
</resources>

当我尝试运行时,logCat告诉我:

03-08 03:19:28.067: E/AndroidRuntime(602): Caused by: java.lang.NullPointerException
03-08 03:19:28.067: E/AndroidRuntime(602): game1.code.GameViews.changeGameText(GameViews.java:67)
03-08 03:19:28.067: E/AndroidRuntime(602):
game1.code.GameViews.initGameViews(GameViews.java:52)

似乎setText导致了这个问题,因为当我注释掉这一行时,没有问题发生。

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

您将字符串值设置为R.java中的某个值,而不是该R.java变量引用的值

更改

gameText.setText(R.string.hi2);

gameText.setText(getContext().getResources().getString(R.string.hi2));

答案 1 :(得分:0)

您应该使用null方法进行changeGameText()检查。

if (System.currentTimeMillis() - time1 > 100 && gameText != null)
{
    gameText.setText(R.string.hi2);
    return;
}
else
{
    return;
}

问题是这个changeGameText()是由initGameViews()调用的,而这个方法被称为构造函数GameViews()。此时,类变量gameTextnull