所以我有一個顯示正確的列表視圖。當物品「開啓」時,文字爲白色,關閉時爲灰色。這部分功能很好。但是,當我將ImageView添加到混合中時,我得到一個空指針異常。我不明白爲什麼。我也嘗試使用位圖,並得到相同的問題。在ListView中顯示TextView旁邊的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);
}
每請求錯誤日誌(對不起的印象是空指針不必說什麼helpful..I知道錯誤是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"/>
顯示您的NewView的和row.xml –
確保您的ImageView的ID確實是「複選框」 –
沒有一個logcat的,很難說(IOW,發佈的logcat顯示碰撞吸:P)...你確定'R.id.checkBox'是正確的? – Barak