android studio没有解析findByViewId变量

时间:2016-12-14 04:43:45

标签: android view

我正在创建一个围绕网格移动mosnsters并让它们在易受攻击状态和安全状态之间交替的游戏。

我的问题是我想要更新我的计时器视图和我的分数视图,但我的程序出于某种原因无法识别findByViewIdscore_text <的timer_text个变量/ p>

clickMonster方法更新了分数视图,countTimer更新了时间视图

CODE:

package com.oreilly.demo.android.pa.uidemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.oreilly.demo.android.pa.uidemo.model.Monster;
import com.oreilly.demo.android.pa.uidemo.model.Monsters;
import com.oreilly.demo.android.pa.uidemo.view.MonsterView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {


    /** time for game */
    static int time;

    /** earned score */
    static int score;
    static TextView score_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        MonsterView mview = (MonsterView) findViewById(R.id.dots);
        Monsters monsters = new Monsters(4, 4);
        for (int i = 0; i < monsters.n; i++) {
            for (int j = 0; j < monsters.m; j++) {
                if (Math.random() < 0.7) monsters.addMonster(i, j, 150 +     250*j, 150 + 250*i, 100);
            }
    }
    mview.setParams(this, monsters);
    mview.setOnTouchListener(new MonsterTouchListener(monsters));

    TextView timer_text = (TextView) findViewById(R.id.textView);//text1?
    score_text = (TextView) findViewById(R.id.textView2);

    score = 0;
    score_text.setText("Score: " + score);

    time = 10;
    Timer time_timer = new Timer();
    time_timer.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(() -> countTimer(timer_text));
        }
    }, /*initial delay*/ 0, /*periodic delay*/ 1000);

    Timer migrate_timer = new Timer();
    migrate_timer.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(() -> migrate(mview, monsters));
        }
    }, 0, 1000);

    Timer change_state_timer = new Timer();
    change_state_timer.schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(() -> changeState(mview, monsters));
        }
    }, 0, 1000);


}

private void countTimer(TextView timer_text) {
    if (time <= 0) {
        timer_text.setText("timeout");
        return;
    }
    timer_text.setText("time: " + time);
    time--;

}

private void migrate(MonsterView mview, Monsters monsters) {
    monsters.migrate();
    mview.invalidate();

}

private void changeState(MonsterView mview, Monsters monsters) {
    monsters.changeState();

    mview.invalidate();

}

/**
 * Touch listener for field and monsters
 */
private static final class MonsterTouchListener implements View.OnTouchListener {

    Monsters monsters;
    public MonsterTouchListener(Monsters _monsters) {
        monsters = _monsters;
    }

    @Override
    public boolean onTouch(View view, MotionEvent me) {
        if (time <= 0) return false;

        int action = me.getAction();
        int x = (int) me.getX();
        int y = (int) me.getY();
        if (action == MotionEvent.ACTION_DOWN) {
            for (int i = 0; i < monsters.monsters.size(); i++) {
                Monster monster = monsters.monsters.get(i);
                if (x > monster.x - monster.r && x < monster.x + monster.r &&
                        y > monster.y - monster.r && y < monster.y + monster.r) {
                    if (monster.canBeEliminated()) clickMonster(view, i);
                    break;
                }
            }

        }

        return true;
    }

    private void clickMonster(View view, int i) {
        monsters.monsters.remove(i);
        score += 10;
        score_text.setText("Score: " + score);

        view.invalidate();
    }
}
}

XML文件:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oreilly.demo.android.pa.uidemo"
android:versionCode="1"
android:versionName="1.0.0" >

<application
    android:theme="@style/Theme.AppCompat.Light"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:screenOrientation="portrait">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

1 个答案:

答案 0 :(得分:0)

您的AndroidManifest.xml文件无法在此处提供帮助。 只需检查您的activity_main.xml中是否有两个TextView,并且它们都有正确的ID(android:id):

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="150dp"
    android:id="@+id/textView"
    tools:text="Hello"
    android:textSize="24sp" />

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="150dp"
    android:id="@+id/textView2"
    tools:text="Hello"
    android:textSize="24sp" />