0

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)

Edmond Tamas
  • 3,148
  • 9
  • 44
  • 89

1 Answers1

0

You have not initialized likers array.You have to do something like:

      TextView[] likers = new TextView[4];

with the number of views You want to have inside the array.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • instead `4` => `data.size()` fx in constructor but this whole code is a dead end, anyway – Selvin Dec 12 '14 at 15:16
  • unfortunatelly not working, basically I have initialised the view before like: TextView likers[position] = new TextView(context); but I think it should be casted to View view. I guess – Edmond Tamas Dec 12 '14 at 15:16
  • I know it is not really nice, but I need a OnClickListener on the names of the persons who like a picture. – Edmond Tamas Dec 12 '14 at 15:17
  • then do not store those TextViews at all ... use setTag and put the user and userid(fx as some POJO class) inside it then in onClick use getTag with`View v` ... also get rid of `separatedDire` it makes no sens becuase you are setting it every time getView is called – Selvin Dec 12 '14 at 15:23