视图可见性

时间:2018-05-29 21:08:35

标签: java android class

我有一个扩展另一个类的类。调用此类时,应根据需要设置变量和其他类。但是,XML元素将返回null。

基类:

public abstract class Shop implements AdapterView.OnItemClickListener {
    public interface Task {
        void completed();
    }

    public Button identifyBtn;
    public abstract void setup();
    public final void build() {
        setup();
    }

    ....
}

正在实例化的类:

public class Weapons extends Shop {
    public String shopName = "Ye Aulde Smithery";

    public Weapons(){
        super();
        type = "weapons";
    }

    public void setup(){
        setTitle(shopName);
        identifyBtn.setVisibility(View.VISIBLE);
        npcImg.setBackgroundResource(R.drawable.npc_weapons);
        blurb.setText(generateBlurb(R.array.bill_smith_blurbs));

        getItems(new Shop.Task() {
            @Override
            public void completed() {
                displayList(items);
            }
        });
    }
}

然后,在调用Activity的onCreate方法中,我得到了:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop);
    activity = this;

    app.stats.addObserver(new StatBar());
    app.stats.setState();

    final String type = getIntent().getStringExtra("type");

    switch(type){
        case "trade":
            shop = new Trade();
            break;

        case "shop_armor":
            shop = new Armor();
            break;

        case "shop_weapons":
            shop = new Weapons();
            break;

        case "shop_tavern":
            shop = new Tavern();
            break;

        case "shop_healer":
            shop = new Healer();
            break;

        case "shop_court":
            shop = new Court();
            break;

        case "shop_post_office":
            shop = new PostOffice();
            break;

        case "shop_smithy":
            shop = new Smithy();
            break;

        case "shop_guild":
            shop = new Skill();
            break;

        case "gems":

            break;

        case "shop_diner":

            break;

        case "storage":
            shop = new Storage();
            break;

        case "shop_den":

            break;

        case "shop_inn":

            break;
    }
    shop.items = new ArrayList<>();
    shop.content = findViewById(R.id.shop_bottom_right);
    shop.npcImg = findViewById(R.id.shop_npc);
    shop.exit = findViewById(R.id.exitBtn);
    shop.tradeBtn = findViewById(R.id.tradeBtn);
    shop.switchBtn = findViewById(R.id.switchBtn);
    shop.infoBtn = findViewById(R.id.infoBtn);
    shop.polishBtn = findViewById(R.id.polishBtn);
    shop.identifyBtn = findViewById(R.id.identifyBtn);
    shop.blurb = findViewById(R.id.shop_blurb);

    shop.build();
}

一切都很好,但是当我在游戏中运行商店时,我会不断发现identifyBtn.setVisibility(View.VISIBLE);行正在抛出:

  

尝试调用虚方法&#39; void android.view.View.setVisibility(int)&#39;在空对象引用上

导致此问题的原因是什么?如何解决此问题?

编辑 - 发布XML资源:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:id="@+id/shop_layout_container"
             tools:context="com.therealmsbeyond.dragoncourtmobile.ActivityShop">
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/layout_container">
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:layout_centerInParent="true"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:id="@+id/nextReset"/>
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="40sp"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:id="@+id/shop_top_right">
            <Button
                android:id="@+id/exitBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/exit"/>
            <Button
                android:id="@+id/tradeBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/trade"
                android:layout_toRightOf="@+id/exitBtn"
                android:layout_toEndOf="@+id/exitBtn"/>
            <RadioGroup
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_toEndOf="@id/tradeBtn"
                android:layout_toRightOf="@id/tradeBtn"
                android:id="@+id/switchBtn">
                <RadioButton
                    android:id="@+id/shop_buy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="Buy"
                    android:onClick="onShopToggle"/>
                <RadioButton
                    android:id="@+id/shop_sell"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Sell"
                    android:onClick="onShopToggle"/>
            </RadioGroup>
        </RelativeLayout>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/shop_top_right"
            android:contentDescription="@string/shop_char"
            android:id="@+id/shop_npc"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/shop_npc"
            android:textSize="20sp"
            android:id="@+id/shop_blurb"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/shop_blurb"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:text="Info"
            android:id="@+id/infoBtn"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/infoBtn"
            android:text="Identify $40"
            android:id="@+id/identifyBtn"
            android:visibility="gone"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/infoBtn"
            android:text="Polish $40"
            android:id="@+id/polishBtn"
            android:visibility="gone"/>
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/shop_blurb"
            android:layout_toRightOf="@+id/shop_blurb"
            android:layout_below="@+id/shop_top_right"
            android:id="@+id/shop_bottom_right"
            android:layout_marginBottom="40sp">

        </RelativeLayout>
        <include
            android:id="@+id/bottom_bar_inc"
            layout="@layout/stat_bar"/>
    </RelativeLayout>

</FrameLayout>

编辑2 - 调试信息

在实例化类之后但在调用构建方法并获得Log.i()之前,我在Activity中执行了一个快速而脏的I/identifyBtn: android.support.v7.widget.AppCompatButton{8c6d7d0 GFED..C.. ......I. 0,0-0,0 #7f0900e2 app:id/identifyBtn}按钮,所以我假设按钮是被正确引用。但是它仍然无法上课。

1 个答案:

答案 0 :(得分:-1)

清理并重建,现在工作正常......