2

i have a ListView with a custom row: detail_row1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="horizontal" 
    android:layout_height="wrap_content"
    android:padding="6dip"
    **android:background="@drawable/item_background_selector"**  ---> line 7
    android:id="@+id/detail_linearLayout"
  >

<TextView 
    android:id="@+id/propName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"   
    android:layout_weight="1"    
    android:textSize="16sp"
    android:textStyle="bold" 
    android:textColor="#000000"
    android:gravity="center_vertical|left"
    android:paddingTop="10sp"
    android:paddingBottom="10sp"
    android:paddingLeft="4sp"

/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:id="@+id/propValue"       
    android:textColor="#000000"
    android:textSize="16sp"
    android:textStyle="bold"
    android:paddingTop="10sp"
    android:paddingBottom="10sp"
    android:paddingRight="4sp"
    android:gravity="center_vertical|right"
    />  

</LinearLayout>

my /drawable/item_background_selector.xml (SAME if i put it under /color/item_background_selector.xml and properly reference it in detail_row1.xml)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false" android:color="#ACD52B">
    </item>
    <item android:state_selected="true" android:color="#0094CE">
    </item>
</selector>

when I try to inflate this view in my ListAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
    View v=convertView;
    if(v==null) {
    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v=vi.inflate(R.layout.detail_row1, null); **--> NullPointerException**
   }
    .
    .

It throws a NullPointerException.

This is not the case if i simply remove line 7 (android/background) from detail_row1.xml

Any clue / help on why this is happening (fairly new to android)

Thanks in advance!

(edit) Logcat:

**08-24 00:27:53.637: E/AndroidRuntime(15295): FATAL EXCEPTION: main**
08-24 00:27:53.637: E/AndroidRuntime(15295): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
08-24 00:27:53.637: E/AndroidRuntime(15295):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
08-24 00:27:53.637: E/AndroidRuntime(15295):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-24 00:27:53.637: E/AndroidRuntime(15295):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
08-24 00:27:53.637: E/AndroidRuntime(15295):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
08-24 00:27:53.637: E/AndroidRuntime(15295):    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
08-24 00:27:53.637: E/AndroidRuntime(15295):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-24 00:27:53.637: E/AndroidRuntime(15295):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-24 00:27:53.637: E/AndroidRuntime(15295):    at **com.dhomes.steel.DetailActivity$1.getView(DetailActivity.java:101)**

(2nd edit)

My listAdapter:

lvProperties.setAdapter(new ListAdapter() {
//Section section=new Section();

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v=convertView;

   if(v==null) {
   //LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   LayoutInflater vi =(LayoutInflater)DetailActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   v=vi.inflate(R.layout.detail_row1, null);
   }

   TextView name=(TextView)v.findViewById(R.id.propName);
   TextView value=(TextView)v.findViewById(R.id.propValue);

   if(name!=null) {
       name.setText(section.propNames.get(position));
   }
   if(value!=null) {
       value.setText(section.propValues.get(position));
   }        
   return v;
}
.
.
.
}
7
  • 1
    Try putting Line 7 back in and do a Project -> Clean, then reload the app Commented Aug 23, 2012 at 5:09
  • @JamesFazio +1 yes you were right.That is what i'm typing now. But, you'd put. Commented Aug 23, 2012 at 5:10
  • just did, same results. I didn't clean before btw but did manually delete my gen/R.java file. Could it have anything to do with the SDK version? I'm using 16 for target but 8 for minSdk Commented Aug 23, 2012 at 5:14
  • Are you doing this project with Fragments? Commented Aug 23, 2012 at 5:16
  • Shouldn't have to do with the SDK version Commented Aug 23, 2012 at 5:18

3 Answers 3

1
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.detail_row1, null);

From, above lines. You're inflating this in your customadapter class only it seems. So, you've the context object from Constructor so better use that like below -

...
LayoutInflater vi = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.detail_row1, null);
...

And, Don't remove the background from your xml file. Just try to clean your project and run after done this changes.

Sign up to request clarification or add additional context in comments.

1 Comment

it's an anonymous inner ListAdapter: lvProperties.setAdapter(new ListAdapter() {....... getting the same problem. Trying to use the activity's context gives the same problem.
0

My comment seems to be getting hidden. So posting as an answer.

The selector, unfortunately, cannot be used for colors. See this: Selector on background color of TextView

3 Comments

hi sameer, I'm going to try that answer right now and mark the answer as correct if it works. thanks for the help
Samir, the app is not crashing now using that answer, I still can't get the results I want though (forcing/faking a default selection on my listview with setSelection + selectors) but I'm going to mark this as correct as it was the source of the problem. If you do know though why my item doesn't change with listView setSelection that would be great (already made the list CHOICE_MODE_SINGLE, request focus, used both android:state_selected, state_checked, state_pressed to no avail)
Works for me. How are you "selecting" the item? You have to use the up-down keys or trackball to select item in the listview. You cannot select by touching (or clicking in the emulator). You can get a "Click" event when you touch an item. Refer to this : stackoverflow.com/questions/2433952/…
0

I am not sure but you can try this. Instead of calling inflater in getview call it in constructor of your adapter class as:

LayoutInflater vi =(LayoutInflater)DetailActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

and then call layout id in getView as:

if (convertView == null) {

            convertView = inflater.inflate(R.layout.cat_content, null);

            holder.name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.