[英]ImageButton findViewById returns null
It should be pretty straightforward but something is wrong. 它应该很简单,但是出了点问题。
public class CreditView extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
setContentView(R.layout.creditlayout);
back = (TableRow) findViewById(R.id.backView);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
fbBtn = (ImageButton) findViewById(R.id.fbBtn);
fbBtn.setOnClickListener(new OnClickListener() {
....
I am getting a null pointer on fbBtn. 我在fbBtn上得到了一个空指针。
and the layout : 和布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backgraund"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/topbar" >
<TableRow
android:id="@+id/backView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:clickable="true"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text="Back"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="normal" />
</TableRow>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/roadmania" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/TopLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation = "vertical"
android:gravity="center"
android:padding="5dip" >
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/twitterBtn"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/twitter_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fbBtn"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/fb_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
</TableRow>
</RelativeLayout>
You accidentally didn't correctly define the id. 您不小心没有正确定义ID。 You probably copied and pasted, but forget to change
layout_below
to id
. 您可能已复制并粘贴,但是忘记将
layout_below
更改为id
。
You have this: 你有这个:
android:layout_below="@+id/fbBtn"
Change that to this: 更改为此:
android:id="@+id/fbBtn"
Change your ImageButtons
from: 从以下位置更改您的
ImageButtons
:
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/twitterBtn"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/twitter_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fbBtn"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/fb_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
to 至
<ImageButton
android:id="@+id/twitterBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/twitter_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
<ImageButton
android:id="@+id/fbBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="@null"
android:src="@drawable/fb_200x200"
android:padding="15dip"
android:layout_margin="15dip"
/>
You have missed the ids and thats why you have a NullPointerException
: 您错过了ID,这就是为什么您有
NullPointerException
:
fbBtn = (ImageButton) findViewById(R.id.fbBtn);
fbBtn
is null! fbBtn
为空!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.