I am having trouble creating a TextView dynamically inside my custom ArrayAdapter. I guess it has something to do with the inflated layout, but I can't find a similar case anywhere around.
I get NPE at this line:
likers[position] = new TextView(context);
I would be really thankful for a suggestion.
custom ArrayAdapter:
public class FeedGridViewAdapter extends ArrayAdapter<FeedItemsSetter> {
private Context context;
private int layoutResourceId;
private ArrayList<FeedItemsSetter> data = new ArrayList<FeedItemsSetter>();
private TextView locationName;
private ImageView image;
private ImageView feedUserImage;
private TextView cityName;
private TextView userInfo;
private TextView[] likers;
private String[] separatedDire;
public FeedGridViewAdapter(Context context, ArrayList<FeedItemsSetter> data) {
super(context, android.R.id.content, data);
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.feed_grid, null);
FeedItemsSetter item = data.get(position);
locationName = (TextView) view.findViewById(R.id.locName);
locationName.setText(item.getTitle());
cityName = (TextView) view.findViewById(R.id.cityName);
cityName.setText(item.getCategory());
userInfo = (TextView) view.findViewById(R.id.feedUserName);
userInfo.setText(item.getUser());
feedUserImage = (ImageView) view.findViewById(R.id.feedUserImage);
Picasso.with(context)
.load("https://graph.facebook.com/" + item.getUserId() + "/picture?type=large")
.placeholder(R.drawable.placeholder)
.error(R.drawable.ic_launcher)
.transform(new RoundedTransformation(90, 4))
.resize(100, 100)
.centerCrop()
.into(feedUserImage);
image = (ImageView) view.findViewById(R.id.image);
Picasso.with(context).load("http://www.edmondvarga.com/gastrolove/uploads/" + item.getImage())
.into(image);
separatedDire = item.getLiker().split(",");
LinearLayout layout = (LinearLayout) view.findViewById(R.id.likerNames);
likers[position] = new TextView(context); // <----- EXCEPTION AT THIS LINE
likers[position].setText(separatedDire[0] + ", ");
likers[position].setTextColor(Color.parseColor("#d52e2e"));
likers[position].setTypeface(null, Typeface.BOLD_ITALIC);
likers[position].setTextSize(13);
layout.addView(likers[position]);
likers[position].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent in= new Intent(context, UserProfile.class);
in.putExtra("user", separatedDire[0]);
in.putExtra("userId", separatedDire[1]);
context.startActivity(in);
}
});
return view;
}
}
12-12 16:55:38.056: E/AndroidRuntime(28749): FATAL EXCEPTION: main 12-12 16:55:38.056: E/AndroidRuntime(28749): Process: com.fideli, PID: 28749 12-12 16:55:38.056: E/AndroidRuntime(28749): java.lang.NullPointerException 12-12 16:55:38.056: E/AndroidRuntime(28749): at com.fideli.userimages.FeedGridViewAdapter.getView(FeedGridViewAdapter.java:78) 12-12 16:55:38.056: E/AndroidRuntime(28749): at com.fideli.userimages.OffersFragmentActivity.setListViewHeightBasedOnChildren(OffersFragmentActivity.java:368) 12-12 16:55:38.056: E/AndroidRuntime(28749): at com.fideli.userimages.OffersFragmentActivity.addToFeedGrid(OffersFragmentActivity.java:354) 12-12 16:55:38.056: E/AndroidRuntime(28749): at com.fideli.userimages.OffersFragmentActivity.getFeed(OffersFragmentActivity.java:343) 12-12 16:55:38.056: E/AndroidRuntime(28749): at com.fideli.userimages.OffersFragmentActivity$4.onSuccess(OffersFragmentActivity.java:172) 12-12 16:55:38.056: E/AndroidRuntime(28749): at com.loopj.android.http.JsonHttpResponseHandler$1$1.run(JsonHttpResponseHandler.java:126) 12-12 16:55:38.056: E/AndroidRuntime(28749): at android.os.Handler.handleCallback(Handler.java:733) 12-12 16:55:38.056: E/AndroidRuntime(28749): at android.os.Handler.dispatchMessage(Handler.java:95) 12-12 16:55:38.056: E/AndroidRuntime(28749): at android.os.Looper.loop(Looper.java:146) 12-12 16:55:38.056: E/AndroidRuntime(28749): at android.app.ActivityThread.main(ActivityThread.java:5602) 12-12 16:55:38.056: E/AndroidRuntime(28749): at java.lang.reflect.Method.invokeNative(Native Method) 12-12 16:55:38.056: E/AndroidRuntime(28749): at java.lang.reflect.Method.invoke(Method.java:515) 12-12 16:55:38.056: E/AndroidRuntime(28749): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 12-12 16:55:38.056: E/AndroidRuntime(28749): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 12-12 16:55:38.056: E/AndroidRuntime(28749): at dalvik.system.NativeStart.main(Native Method)