嗨,我对android开发非常陌生,我正在创建一个包含4个imagebuttons的活动,我先尝试使用一个imagebutton链接到新活动,但是在我甚至无法链接页面之前,似乎OnClick事件给了我错误。
从我的logcat中,我了解到我在第30行抛出了空指针异常:imageButton1.setOnClickListener(new View.OnClickListener() {
我确实怀疑这可能是我的布局元素,例如ImageButtons(不同的Ids),这导致我的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>
最佳答案
使用rootView
从activity_recipes_fragment
布局访问视图:
final ImageButton imageButton1 = (ImageButton)
rootView.findViewById(R.id.imageButton1);