Java OnClick事件期间的NullPointerException

Java OnClick事件期间的NullPointerException,java,android,eclipse,Java,Android,Eclipse,嗨,我对android开发非常陌生,我正在创建一个有4个imagebutton的活动,我尝试先用一个imagebutton链接到一个新的活动,但在我能够链接页面之前,似乎我的OnClick事件给了我错误 从我的logcat中,我知道我在第30行抛出了一个空指针异常:imageButton1.setOnClickListener(newview.OnClickListener(){ 我怀疑可能是我的布局元素,如ImageButtons(不同的ID),导致我的findViewById()返回null

嗨,我对android开发非常陌生,我正在创建一个有4个imagebutton的活动,我尝试先用一个imagebutton链接到一个新的活动,但在我能够链接页面之前,似乎我的OnClick事件给了我错误

从我的logcat中,我知道我在第30行抛出了一个空指针异常:
imageButton1.setOnClickListener(newview.OnClickListener(){

我怀疑可能是我的布局元素,如ImageButtons(不同的ID),导致我的findViewById()返回null,但尽管尝试,我仍然找不到可能导致它的错误。请原谅我的理解,因为我还在学习。非常感谢您提供的任何帮助,谢谢

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;
};
}
package com.example.sgrecipe;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageButton;
导入android.content.Intent;
公共类片段扩展了片段{
图像按钮图像按钮1;
图像按钮图像按钮2;
图像按钮图像按钮3;
图像按钮图像按钮4;
意图;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.activity\u recipes\u fragment,container,false);
intent=newintent(getActivity(),favoriteFragment.class);
最终ImageButton imageButton1=(ImageButton)findViewById(R.id.imageButton1);

imageButton1.setOnClickListener(新视图.OnClickListener(){/使用
rootView
activity\u recipes\u fragment
布局访问视图:

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

使用
rootView
访问
activity\u recipes\u fragment
布局中的视图:

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