亲爱的我正在使用以下代码来创建带有图像视图的自定义标题。但是它显示了imageview的空指针异常。怎么解决这个问题?
public class MyCustomTab2Activity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
//Tab Content
setContentView(R.layout.my_tab_home);
ImageView imag = (ImageView) findViewById(R.id.iv_logout);
imag.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
}
});
//custom title bar content
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.logintitle);
......
...........
}
}
答案 0 :(得分:0)
只需将以下代码放在TabHost
主要活动 -
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
ImageView image = (ImageView)findViewById(R.id.header);
image.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(CustomWindowTitle.this, "This is sample", Toast.LENGTH_SHORT).show();
return false;
}
});
}
<强> Window_title.xml 强>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#323331">
<ImageView
android:id="@+id/header"
android:src="@drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
希望这对你有所帮助。请参阅下面的输出 -
<强>更新强>
我明白了。您在图片OnclickListener
之后宣布自定义标题在代码中进行更改,如下所示 -
public class MyCustomTab2Activity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
//Tab Content
setContentView(R.layout.my_tab_home);
//custom title bar content
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.logintitle);
ImageView imag = (ImageView) findViewById(R.id.iv_logout);
imag.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
}
});
......
...........
}
}
希望这会对你有所帮助。
答案 1 :(得分:0)
ImageView
并非真正意图以这种方式使用,您可以尝试两件事。在ImageView
,imag.setFocusable=(true)
上设置以下标记并查看是否有效,或者尝试使用ImageButton
,或只使用Button
属性background
设置为您的图像,因为它设计为以这种方式使用。