2014-11-28 2 views
1

Это должно быть довольно просто, но что-то не так.ImageButton findViewById возвращает null

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> 

ответ

3

Вы случайно не правильно определить идентификатор. Вероятно, вы скопировали и вставили, но забудьте изменить layout_below на id.

У вас есть это:

android:layout_below="@+id/fbBtn" 

Изменения, которые к этому:

android:id="@+id/fbBtn" 
+0

О, мужик, я потратил 1 час на это - я никогда его не поймал. Глупая ошибка копирования и вставки. Спасибо, я приму это через пару минут. – ghostrider

+0

@ghostrider отлично :) это случается со всеми время от времени! –

1

Измените ваш 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" 
     /> 

в

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

Вы пропустили идентификаторами и вот почему у вас есть NullPointerException:

fbBtn = (ImageButton) findViewById(R.id.fbBtn); 

fbBtn равна нулю!

Смежные вопросы