Java ImageButton findViewById返回null

Java ImageButton findViewById返回null,java,android,Java,Android,这应该很简单,但有点不对劲 public class CreditView extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // The activity is being created. setContentView(R.layout.creditlayout);

这应该很简单,但有点不对劲

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() {
....
我在fbBtn上得到一个空指针

布局如下:

<?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>

您不小心没有正确定义id。您可能复制并粘贴了,但忘记将下面的
布局更改为
id

你有这个:

android:layout_below="@+id/fbBtn"
将其更改为:

android:id="@+id/fbBtn"

您不小心没有正确定义id。您可能复制并粘贴了,但忘记将下面的
layout_
更改为
id

你有这个:

android:layout_below="@+id/fbBtn"
将其更改为:

android:id="@+id/fbBtn"

将您的
图像按钮更改为:

  <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"
         />

fbBtn
为空

从以下位置更改您的
图像按钮

  <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"
         />

fbBtn
为空

哦,天哪,我浪费了一个小时在这上面-我从来没有抓住它。愚蠢的复制粘贴错误。谢谢-我会在几分钟内接受它。@ghostrider好极了:)每个人偶尔都会遇到这种情况!哦,天哪,我浪费了一个小时在这上面-我从来没有抓住它。愚蠢的复制粘贴错误。谢谢-我会在几分钟内接受它。@ghostrider好极了:)每个人偶尔都会遇到这种情况!