OnClick事件期间的NullPointerException

时间:2015-02-12 02:11:18

标签: java android eclipse

您好我对Android开发非常陌生,我正在创建一个有4个图像按钮的活动,我尝试使用一个图像按钮首先链接到一个新活动,但在我甚至能够之前链接页面,似乎我的OnClick事件给了我错误。

从我的logcat中我知道我在第30行抛出了空指针异常:imageButton1.setOnClickListener(new View.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;
};
}

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>

1 个答案:

答案 0 :(得分:4)

使用rootView访问activity_recipes_fragment布局的视图:

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