更改其中的TextView时TabHost崩溃

时间:2013-11-30 13:22:19

标签: android android-tabhost

简而言之,我设置了一个tabhost,因为我希望标签的内容具有动态TextView(除其他外),我尝试初始化文本并崩溃。我不确定为什么,但是注释掉在TextView中设置文本的代码会停止崩溃。

使用意图来设置活动以确定标签的内容时,提到修复这个问题的一个参考,但显然这并没有实际修复崩溃,它以某种方式改变了它,然后那个导致他没有说他如何修复它就消失了,我的尝试也失败了。

package com.example.main;

//removed imports

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
private boolean atMainMenu;
TextView warframeText;
TextView primaryText;
TextView secondaryText;
TextView meleeText;
TextView sentinelText;
TextView sentinelWeaponText;

Warframe warframe;
PrimaryWeapon primary;
SecondaryWeapon secondary;
MeleeWeapon melee;
Sentinel sentinel;
Weapon sentinelWeapon;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
TabHost mTabHost = getTabHost();


//TabHost
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Build").setContent(R.id.build));
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Stats").setContent(R.id.stats));
    TextView title1 = (TextView)mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
    title1.setTextColor(Color.parseColor("#30A0F0"));
    TextView title2 = (TextView)mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title);
    title2.setTextColor(Color.parseColor("#30A0F0"));
    mTabHost.setCurrentTab(0);

 warframeText = (TextView)findViewById(R.id.warframe);
 primaryText = (TextView)findViewById(R.id.primary);
 secondaryText = (TextView)findViewById(R.id.secondary);
 meleeText = (TextView)findViewById(R.id.melee);
 sentinelText = (TextView)findViewById(R.id.sentinel);
 sentinelWeaponText = (TextView)findViewById(R.id.sentinelWeapon);

 //Change To First Time Setup
 warframe = new Excalibur();
 primary = new BratonMk1();
 secondary = new Lato();
 melee = new Skana();

    setBuild(); //<--removing this fixed the crashing, the method is included after onCreate, it sets the TextView's text

   atMainMenu = true;
}

 public void setBuild() {
 warframeText.setText(warframe.getName());
 primaryText.setText(primary.getName());
 secondaryText.setText(secondary.getName());
 meleeText.setText(melee.getName());
 sentinelText.setText(sentinel.getName());
 sentinelWeaponText.setText(sentinelWeapon.getName());
 }
}

所以我想知道,如果有人有答案,为什么编辑选项卡布局中包含的Textviews会导致应用程序崩溃,我该如何解决这个问题? :/ (我提供的代码也很简短,但我相信它包含问题的所有相关部分)

1 个答案:

答案 0 :(得分:1)

您没有initialize sentinelsentinelWeapon但是您正试图从中获取名称

sentinelText.setText(sentinel.getName());
sentinelWeaponText.setText(sentinelWeapon.getName());

这可能是导致您的应用崩溃的原因