我在做什么 ::
this post for my solution
我能做什么 ::
出现了什么问题 ::
mCheckBox.setTag(position);
item_meal_type.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="150dip"
android:layout_height="150dip"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#000" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="CheckBox" />
</RelativeLayout>
FrgMdFilter.java
public class FrgMdFilter extends Fragment {
Button btnFilter;
Spinner spnFilterByMealType;
private TextView txtMinPrice,txtMaxPrice,txtMinDistance,txtMaxDistance,txtMinRating,txtMaxRating;
private CheckBox chkPrice,chkDistance,chkRating;
private static RelativeLayout progressLayout;
private static String[] dataObjects = new String[]{ "Text #1",
"Text #2",
"Text #3" };
//Constructor declaration on type newInstance
public static FrgMdFilter newInstance() {
FrgMdFilter fragment = new FrgMdFilter();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.frg_md_filter, container, false);
//setHasOptionsMenu(true);//For using actionbar menu from fragment
setRetainInstance(true);
progressLayout=(RelativeLayout) view.findViewById(R.id.progressLayout);
progressLayout.setBackgroundColor(Color.parseColor("#2da2cd"));
HorizontalListView hListView = (HorizontalListView) view.findViewById(R.id.listview);
hListView.setAdapter(mAdapter);
return view;
}
/*@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.actionbar_sort_menu, menu);
}*/
<------------Rest-----of-------------------------------------code-----in b/w-------------->
/* private static String[] dataObjects = new String[]{ "Text #1",
"Text #2",
"Text #3" };*/
private BaseAdapter mAdapter = new BaseAdapter() {
/*CheckBox mCheckBox;*/
SparseBooleanArray mSparseBooleanArray = new SparseBooleanArray();
@Override
public int getCount() {
return dataObjects.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_meal_type, null);
TextView title = (TextView) retval.findViewById(R.id.title);
title.setText(dataObjects[position]);
CheckBox mCheckBox=(CheckBox) retval.findViewById(R.id.ch_bx);
mCheckBox.setTag(position);
mCheckBox.setChecked(mSparseBooleanArray.get(position));
mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);
return retval;
}
OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
}
};
};
}
日志
06-04 12:45:53.270: E/AndroidRuntime(7658): FATAL EXCEPTION: main
06-04 12:45:53.270: E/AndroidRuntime(7658): java.lang.NullPointerException
06-04 12:45:53.270: E/AndroidRuntime(7658): at com.findmybuffet.fragments.FrgMdFilter$1.getView(FrgMdFilter.java:293)
06-04 12:45:53.270: E/AndroidRuntime(7658): at com.findmybuffet.utilities.HorizontalListView.fillListRight(HorizontalListView.java:232)
06-04 12:45:53.270: E/AndroidRuntime(7658): at com.findmybuffet.utilities.HorizontalListView.fillList(HorizontalListView.java:217)
06-04 12:45:53.270: E/AndroidRuntime(7658): at com.findmybuffet.utilities.HorizontalListView.onLayout(HorizontalListView.java:195)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1617)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.LinearLayout.onLayout(LinearLayout.java:1401)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:678)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.View.layout(View.java:11180)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewGroup.layout(ViewGroup.java:4203)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1468)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.os.Looper.loop(Looper.java:137)
06-04 12:45:53.270: E/AndroidRuntime(7658): at android.app.ActivityThread.main(ActivityThread.java:4340)
06-04 12:45:53.270: E/AndroidRuntime(7658): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 12:45:53.270: E/AndroidRuntime(7658): at java.lang.reflect.Method.invoke(Method.java:511)
06-04 12:45:53.270: E/AndroidRuntime(7658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-04 12:45:53.270: E/AndroidRuntime(7658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-04 12:45:53.270: E/AndroidRuntime(7658): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
此处(CheckBox) retval.findViewById(R.id.ch_bx)
正在返回null
个实例,因为您的布局中没有标识为R.id.ch_bx
的复选框...
所以替换
retval.findViewById(R.id.ch_bx)
带
retval.findViewById(R.id.checkBox1)
查看您的布局文件。该复选框的ID为checkBox1
...