0

こんにちは、私は最初の 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 は次のことを教えてくれます:</p>

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 が問題を引き起こしているようです。

何か見逃しましたか?

4

2 に答える 2

1

文字列値を、その R.java 変数によって参照される値ではなく、R.java のある値に設定しています。

変化する

gameText.setText(R.string.hi2);

gameText.setText(getContext().getResources().getString(R.string.hi2));
于 2012-03-08T09:59:31.467 に答える
0

メソッドでnullチェックを行う必要があります。changeGameText()

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

問題は、 thischangeGameText()が呼び出されinitGameViews()、このメソッドがコンストラクターによって呼び出されることGameViews()です。この時点で、クラス変数gameTextnull.

于 2012-03-08T08:27:48.263 に答える