当前位置:  开发笔记 > 编程语言 > 正文

Android Studio不会让我设置TextView

如何解决《AndroidStudio不会让我设置TextView》经验,为你挑选了1个好方法。

我不确定为什么这段代码不起作用.我正在尝试选择你自己的冒险游戏.描述您的情况的文本是文本视图,但我无法设置它们.Android Studio表示无法解析findViewByID().

package com.blogspot.darokrithia.dungeonfungeon;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class RoomActivity extends AppCompatActivity {
    static int row = 1;            //current row of room
    static int column = 1;         //current column of room
    static int totalRows = 5;      //total number of rows (including blank rooms)
    static int totalColumns = 5;   //total number of columns (including blank rooms)
    static Layout dungeon = new Layout(totalRows,totalColumns);    //Layout of this
    static Room currentRoom = dungeon.getRoom(row,column);         //The rooms the player is in

    TextView roomText = (TextView) findViewByID(R.id.RoomDescriptionText);    //What the room says
    TextView aOption = (TextView) findViewByID(R.id.AOptionText);     //What option a does right now
    TextView bOption = (TextView) findViewByID(R.id.BOptionText);     //What option b does right now
    TextView cOption = (TextView) findViewByID(R.id.COptionText);     //What option c does right now
    TextView dOption = (TextView) findViewByID(R.id.DOptionText);     //What option d does right now

    @Override
    protected void onCreate(Bundle savedInstanceState) {  // No fucking idea what this does, but android studio seems to need it.
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);

        currentRoom = dungeon.getRoom(row,column);
    }

    public void aButtonCLick(View v){
        if (v.getId() == R.id.AButton){
            dungeon.optionA(currentRoom);
        }
    }
    public void bButtonCLick(View v){
        if (v.getId() == R.id.BButton){
            dungeon.optionB(currentRoom);
        }
    }
    public void cButtonCLick(View v){
        if (v.getId() == R.id.CButton){
            dungeon.optionC(currentRoom);
        }
    }
    public void dButtonCLick(View v){
        if (v.getId() == R.id.DButton){
            dungeon.optionD(currentRoom);
        }
    }

    public Room getCurrentRoom(){
        return currentRoom;
    }

    public static boolean canMove(int direction){      //makes sure there is no zero room (A wall / blank room) in the direction you want to go
        Room testRoom;
        switch (direction){
            case 1:
                testRoom = dungeon.getRoom((row-1),column);
                if(testRoom.getRoomID() == 0){
                    return false;
                }
                else{
                    return true;
                }
            case 2:
                testRoom = dungeon.getRoom(row,(column-1));
                if(testRoom.getRoomID() == 0){
                    return false;
                }
                else{
                    return true;
                }
            case 3:
                testRoom = dungeon.getRoom(row,(column+1));
                if(testRoom.getRoomID() == 0){
                    return false;
                }
                else{
                    return true;
                }
            case 4:
                testRoom = dungeon.getRoom((row+1),column);
                if(testRoom.getRoomID() == 0){
                    return false;
                }
                else{
                    return true;
                }
            default:
                return false;
        }
    }

    public static void moveNorth(){    //moves up one row
        if(canMove(1)){
            row -= 1;
            currentRoom = dungeon.getRoom(row, column);
        }
    }
    public static void moveEast(){     //moves across one column
        if(canMove(2)){
            column -= 1;
            currentRoom = dungeon.getRoom(row,column);
        }
    }
    public static void moveWest(){     //moves back one column
        if(canMove(3)){
            column += 1;
            currentRoom = dungeon.getRoom(row,column);
        }
    }
    public static void moveSouth(){    //moves down one row
        if(canMove(4)){
            row += 1;
            currentRoom = dungeon.getRoom(row,column);
        }
    }


}

我知道这不是很多代码,所以如果你需要更多的上下文,我愿意将我的整个代码粘贴到这里.我还确保导入TextView.



1> Tanis.7x..:

findViewById() 最后有一个小写的"d".


一个好的做法是尽可能使用IDE中的代码完成.错字,更高的生产力,更少的挫折感,更多的快乐;)
推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有