Java 视图可见性上的空对象引用
我有一个类扩展了另一个类。调用这个类时,应该根据需要设置变量和其他类。但是,XML元素返回空值 基类:Java 视图可见性上的空对象引用,java,android,class,Java,Android,Class,我有一个类扩展了另一个类。调用这个类时,应该根据需要设置变量和其他类。但是,XML元素返回空值 基类: public abstract class Shop implements AdapterView.OnItemClickListener { public interface Task { void completed(); } public Button identifyBtn; public abstract void setup();
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);
}
});
}
}
然后,在调用活动的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();
}
编辑2-调试信息
在类被实例化之后,但在调用build方法并获得
i/identifyBtn:android.support.v7.widget.AppCompatButton{8c6d7d0 GFED..C..i.0,0-0,0#7f0900e2 app:id/identifyBtn}之前,我在活动中快速而肮脏地对按钮执行了Log.i()
,所以我假设按钮被正确引用。但是,该类仍然不可用。已清理并重新生成,现在工作正常…能否显示布局xml?如果你注释掉那句话,场景看起来正确吗?@Buddy更新。注释没有帮助,因为正在引用的其他视图也会引发异常。此代码看起来不错。它应该会起作用。可以在创建方法上发布您的帖子。还要再次检查logcat,看看它是否是相同的下线投票,为什么?
<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>