Android NullPointerException错误的上下文?
我不明白怎么了 列表项Android NullPointerException错误的上下文?,android,nullpointerexception,android-context,findviewbyid,Android,Nullpointerexception,Android Context,Findviewbyid,我不明白怎么了 列表项 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_par
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/no_avatar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:layout_gravity="top|center_horizontal"
android:layout_marginLeft="10dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkbox"
android:id="@+id/checkBox"
android:clickable="true"
android:paddingLeft="@dimen/abc_action_bar_overflow_padding_start_material"
android:singleLine="false"
android:textAppearance="@color/colorAccent"
android:layout_gravity="right" />
</LinearLayout>
}
为什么CheckBox=(CheckBox)v.findviewbyd(R.id.CheckBox)为空?
我尝试了很多变化,但都变傻了((
您正在扩展不包含复选框、图像视图或文本视图的片段列表布局。这就是为什么会出现此错误。 实现这个游标适配器
public class MyCursorAdapter extends CursorAdapter {
public MyCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.mylist_row, parent, false);
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
if (cursor.moveToFirst()){
//получение данных из курсора
nameText = cursor.getString(1);
isAvatar = (cursor.getInt(2)==1);
//заполнение чекбокса, тест имени и аватар
CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);
TextView textView = (TextView) v.findViewById(R.id.name);
ImageView image = (ImageView) v.findViewById(R.id.imageView);
checkBox.setChecked(isAvatar);
textView.setText(nameText);
if (checkBox.isChecked()) {
image.setImageResource(R.drawable.avatar);
} else image.setImageResource(R.drawable.no_avatar);
}
}
}
现在在onCreateview中
MyCursorAdapter todoAdapter = new MyCursorAdapter(this.getActivity(), cursor, 0);
lvItems.setAdapter(todoAdapter);
您正在扩展不包含复选框、图像视图或文本视图的片段列表布局。这就是为什么会出现此错误。 实现这个游标适配器
public class MyCursorAdapter extends CursorAdapter {
public MyCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.mylist_row, parent, false);
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
if (cursor.moveToFirst()){
//получение данных из курсора
nameText = cursor.getString(1);
isAvatar = (cursor.getInt(2)==1);
//заполнение чекбокса, тест имени и аватар
CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);
TextView textView = (TextView) v.findViewById(R.id.name);
ImageView image = (ImageView) v.findViewById(R.id.imageView);
checkBox.setChecked(isAvatar);
textView.setText(nameText);
if (checkBox.isChecked()) {
image.setImageResource(R.drawable.avatar);
} else image.setImageResource(R.drawable.no_avatar);
}
}
}
现在在onCreateview中
MyCursorAdapter todoAdapter = new MyCursorAdapter(this.getActivity(), cursor, 0);
lvItems.setAdapter(todoAdapter);
您正在查看视图(
v
)上的checkBox
):checkBox=(checkBox)v.findViewById(R.id.checkBox);
但是v
被分配到(fragment_list
)这里视图v=充气机。充气(R.layout.fragment_list,container,false)
但是片段列表
不包含复选框
和sov.findViewById(R.id.checkBox)
返回null。
当您想使用SimpleCursorAdapter时,需要创建一个扩展SimpleCursorAdapter的类,然后可以在
getView(final int pos,View View View,ViewGroup parent)
中访问/膨胀“row item”布局文件,然后您可以说:复选框=(CheckBox)View.findViewById(R.id.checkBox)
您正在查看视图(v
)上的复选框
:checkBox=(checkBox)v.findViewById(R.id.checkBox);
但是v
被分配到(fragment\u list
)这里查看v=充气机。充气(R.layout.fragment\u列表,容器,false)
但是片段列表
不包含复选框
和sov.findViewById(R.id.checkBox)
返回null。
当您想使用SimpleCursorAdapter时,需要创建一个扩展SimpleCursorAdapter的类,然后可以在
getView(final int pos,View View View,ViewGroup parent)
中访问/膨胀“row item”布局文件,然后您可以说:复选框=(CheckBox)View.findViewById(R.id.checkBox)
感谢您粘贴屏幕截图,但也请在ru.bunakov.testapplication.fragments.FragmentList.onCreateView(FragmentList.java:46)粘贴实际的stacktrace.java.lang.NullPointerException完整stacktrace有95000多个符号感谢您粘贴屏幕截图,但也请将实际的stacktrace.java.lang.NullPointerException粘贴到ru.bunakov.testapplication.FragmentList.onCreateView(FragmentList.java:46)full stacktrace有95000多个符号我必须做什么来修复它?你能支持我吗?请参阅更新的答案。我已经为你实现了游标适配器。为什么实现了CursorAdapter而不是SimpleCursorAdapter?CursorAdapter很容易用于自定义列表视图这就是我建议的原因。你也可以使用BaseAdapter。如果我的答案对你有帮助,请告诉我。unfor我很困惑我要做些什么来修复它?你能支持我吗?请看更新的答案。我已经为你实现了游标适配器。为什么实现了CursorAdapter而不是SimpleCursorAdapter?CursorAdapter很容易用于自定义列表视图这就是我建议的原因。你也可以使用BaseAdapter。如果我的答案对你有帮助,请告诉我。不幸的是,我我不明白在这种情况下我是如何制作自己的适配器的。你能给我写几行代码吗?我已经花了6个多小时来解决这个问题,绝望地看着-也看着-虽然这个人想要一个不同的功能,但这演示了如何在适配器中包含一个复选框-在你的情况下,我假设根据光标的某些值选中/取消选中复选框。不幸的是,这并不是我真正需要的。如果你卡住了,祝你好运,并大声喊叫。请参阅Vikash的更新答案-他包含了自定义光标适配器的必要实现。我不明白在这种情况下如何制作自己的适配器。你能给我写一些代码示例行吗?我是我已经花了6个多小时处理这个问题,绝望地看着-也看着-虽然这个人想要一个不同的功能,但这演示了如何在适配器中包含一个复选框-在你的情况下,我假设复选框是基于你的光标的某个值选中/取消选中的。不幸的是,它不是真的我需要很好,很好运气好,如果你被卡住了就大声喊叫。参见Vikash的更新答案-他包括了定制光标适配器的必要实现。