2015-02-12 5 views
0

Привет, я очень новичок в разработке Android, и я создаю операцию, которая имеет 4 кнопки изображения, и я пытаюсь с одним изображением сначала связать себя с новым действием, но до того, как я даже смогу связать на странице, похоже, что мое событие OnClick дает мне ошибки.NullPointerException во время события OnClick

С моей LogCat я понял, что у меня есть исключение нулевого указателя брошенного в строке 30: imageButton1.setOnClickListener(new View.OnClickListener() {

Я подозреваю, что это может быть моим макет элемент как ImageButtons (разные идентификаторы), что привело к моей findViewById() возвращение нуля , но попробуйте, поскольку я могу все еще не найти ошибку, которая может вызвать ее. Простите мое понимание, пока я все еще участвую. Радуйся за любую помощь, спасибо!

RecipesFragment.java

package com.example.sgrecipe; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageButton; 
import android.content.Intent; 

public class RecipesFragment extends Fragment { 

ImageButton imageButton1; 
ImageButton imageButton2; 
ImageButton imageButton3; 
ImageButton imageButton4; 
Intent intent; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.activity_recipes_fragment, container, false); 
    intent = new Intent(getActivity(), FavouriteFragment.class); 
    final ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1); 

    imageButton1.setOnClickListener(new View.OnClickListener() { //<--error here 
      public void onClick(View v) { 
      //  startActivity(intent); 
      } 
     }); 

    return rootView; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_recipes_fragment); 

    addListenerOnButton(); 

} 

private void setContentView(int activityRecipesFragment) { 
    // TODO Auto-generated method stub 

} 

public void addListenerOnButton() { 

    imageButton1 = (ImageButton) findViewById(R.id.imageButton1); 
    imageButton2 = (ImageButton) findViewById(R.id.imageButton2); 
    imageButton3 = (ImageButton) findViewById(R.id.imageButton3); 
    imageButton4 = (ImageButton) findViewById(R.id.imageButton4); 
    } 

private ImageButton findViewById(int imagebuttonselector) { 
    // TODO Auto-generated method stub 
    return null; 
}; 
} 

activity_recipes_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<TableLayout 
android:id="@+id/tableLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" > 

<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/chinese_button" /> 

<ImageButton 
    android:id="@+id/imageButton2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/malay_button" /> 
</TableRow> 

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" > 

<ImageButton 
    android:id="@+id/imageButton3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/indian_button" /> 

<ImageButton 
    android:id="@+id/imageButton4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@drawable/others_button" /> 
</TableRow> 

</TableLayout> 
</LinearLayout> 

ответ

4

Использование rootView для доступа с видом из activity_recipes_fragment макета:

final ImageButton imageButton1 = (ImageButton) 
         rootView.findViewById(R.id.imageButton1); 
+0

Оооо я вижу, спасибо я понимаю, моя ошибка сейчас ! – zana

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