所以我有一个正确显示的列表视图。当项目“打开”时,文本为白色,当文本关闭时,文本为灰色。那部分功能都很棒。但是,当我将ImageView添加到混合中时,我得到一个空指针异常。我不明白为什么。我也尝试过使用位图并遇到同样的问题。
以下是一些代码:
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView rRule = (TextView) view.findViewById(R.id.rule_text);
TextView rType = (TextView) view.findViewById(R.id.rule_type);
ImageView iChecked = (ImageView) view.findViewById(R.id.checkBox);
String ruleName = cursor.getString(1);
int ruleType = cursor.getInt(2);
String ruleEnabled = cursor.getString(3);
switch (ruleType) {
/*...some irrelevant code */
}
if (ruleEnabled.equals("true")) {
rRule.setTypeface(null, Typeface.BOLD);
rRule.setTextColor(Color.WHITE);
iChecked.setVisibility(View.VISIBLE); //line 271
} else if (ruleEnabled.equals("false")) {
rRule.setTypeface(null, Typeface.NORMAL);
rRule.setTextColor(Color.GRAY);
iChecked.setVisibility(View.GONE);
}
rRule.setText(ruleName);
}
按要求提供错误日志:(对不起,印象空指针不说任何有用的东西..我知道错误是imageview)
06-29 10:29:02.777: E/AndroidRuntime(29516): FATAL EXCEPTION: main
06-29 10:29:02.777: E/AndroidRuntime(29516): java.lang.NullPointerException
06-29 10:29:02.777: E/AndroidRuntime(29516): at com.company.app.DefaultRulesList$RulesAdapter.bindView(DefaultRulesList.java:271)
06-29 10:29:02.777: E/AndroidRuntime(29516): at com.company.app.DefaultRulesList$RulesAdapter.newView(DefaultRulesList.java:284)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.CursorAdapter.getView(CursorAdapter.java:246)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.ListView.makeAndAddView(ListView.java:1772)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.ListView.fillDown(ListView.java:672)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.ListView.fillFromTop(ListView.java:732)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.ListView.layoutChildren(ListView.java:1625)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.View.layout(View.java:11278)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.View.layout(View.java:11278)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.View.layout(View.java:11278)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.View.layout(View.java:11278)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.View.layout(View.java:11278)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.ViewGroup.layout(ViewGroup.java:4224)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.os.Looper.loop(Looper.java:137)
06-29 10:29:02.777: E/AndroidRuntime(29516): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-29 10:29:02.777: E/AndroidRuntime(29516): at java.lang.reflect.Method.invokeNative(Native Method)
06-29 10:29:02.777: E/AndroidRuntime(29516): at java.lang.reflect.Method.invoke(Method.java:511)
06-29 10:29:02.777: E/AndroidRuntime(29516): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-29 10:29:02.777: E/AndroidRuntime(29516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-29 10:29:02.777: E/AndroidRuntime(29516): at dalvik.system.NativeStart.main(Native Method)
iChecked的代码(调用id的地方)
<ImageView
android:id="@+id/checkBox"
android:padding="2dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/checkbox_on_background"/>
答案 0 :(得分:1)
在我看来,你必须为你的适配器提供错误的布局。
我看不出ImageView
排除NPE的其他原因,它在您正在寻找它的布局中不存在。