自定义标题条形码打破onCreate()

时间:2012-08-15 20:56:08

标签: android

我创建了一个自定义标题栏,我想在我的应用中的所有活动中显示。它在我的主要活动中完美运作:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);

但是我的主要活动会调用另一个活动,当我尝试执行相同的操作时,它会失败:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.layout_2);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);  

尝试执行 findViewById()崩溃,我在try / catch中包含一个以查看发生了什么:

TextView tv1;
try {    
    tv1 = (TextView)findViewById(R.id.header);
}
catch (Exception e)  {
    Log.d("MyActivity2", "boom");  
}

例外情况说 原因= ClassCastException异常 detailMessage = android.widget.RelativeLayout

如果我注释掉 requestWindowFeature(),我不知道这意味着什么, NB 没有问题getWindow()调用。

提前感谢您对此的任何见解!!!

1 个答案:

答案 0 :(得分:1)

如果您收到ClassCastException,则表示R.id.header不是TextView。在您的布局中,header是Relativelayout的名称。因此,将其更改为与TextView的匹配。

相关问题