0 votos

me da java nullpoint exeption en la linea

ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
 

public class AdvancedListViewActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.country_names);
TypedArray icons = res.obtainTypedArray(R.array.country_icons);
setListAdapter(new ImageAndTextAdapter(ctx, R.layout.main_list_item, options, icons));
}
}


 

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="country_names">
<item>Bhutan</item>
<item>Colombia</item>
<item>Italy</item>
<item>Jamaica</item>
<item>Kazakhstan</item>
<item>Kenya</item>
</string-array>
<array name="country_icons">
<item>@drawable/bhutan</item>
<item>@drawable/colombia</item>
<item>@drawable/italy</item>
<item>@drawable/jamaica</item>
<item>@drawable/kazakhstan</item>
<item>@drawable/kenya</item>
</array>
</resources>

 

public class ImageAndTextAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private String[] mStrings;
private TypedArray mIcons;

private int mViewResourceId;
public ImageAndTextAdapter(Context ctx, int viewResourceId,
String[] strings, TypedArray icons) {
super(ctx, viewResourceId, strings);
mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mViewResourceId = viewResourceId;
}
@Override
public int getCount() {
return mStrings.length;
}
@Override
public String getItem(int position) {
return mStrings[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
return convertView;
}
}


 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/option_icon"
android:layout_width="48dp"
android:layout_height="fill_parent"/>
<TextView
android:id="@+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
</LinearLayout>
And this is the content of the main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
</LinearLayout>

 

preguntado por mact Mar 29, 2016 en Android

1 Respuesta

0 votos
Asegúrate que esté llegando algún valor a la variable "convertView", está siendo inflada por lo que mira si te está llegando correctamente por esto (más información y ejemplos en http://jarroba.com/inflate-en-android-inflar-y-adjuntar-views/).

Por otro lado comprueba que está llegando valor al id: "R.id.option_icon"
respondido por Ramon [Admin] Abr 2, 2016