Showing ImageView next to TextView in a ListView

Posted by KDEx on Stack Overflow See other posts from Stack Overflow or by KDEx
Published on 2012-06-29T14:11:52Z Indexed on 2012/06/29 15:16 UTC
Read the original article Hit count: 222

Filed under:
|
|

So I have a listview that is displaying correctly. When the item is "turned on" the text is white and when it's turned off it's grey. That part all functions great. However when I add the ImageView into the mix I get a null pointer exception. I don't understand why. I've tried using bitmaps as well and get the same problem.

Here is some code:

@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);
}

Per request the error log: (Sorry was under the impression null pointers dont say anything helpful..I know the error is the 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)

Code for iChecked (where the id is called)

        <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"/>

© Stack Overflow or respective owner

Related posts about java

Related posts about android