嗨,我正在启动我的第一个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>

还有琴弦。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导致了这个问题,因为当我注释掉这一行时,没有问题发生。

我错过什么了吗?

有帮助吗?

解决方案

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

更改

gameText.setText(R.string.hi2);
.

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

其他提示

你应该做一个 null 登记 changeGameText() 方法。

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

问题是这个 changeGameText() 被调用 initGameViews() 而这个方法被构造函数调用 GameViews().此时类变量 gameTextnull.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top