0

I have an error when i'm trying to use a popover , the popover is used when i click on the button comment on my listview this is the adapter where the button is :

package com.example.mahdi.chat;

import circleview.CircleImageView;

public class FeedListAdapter extends BaseAdapter {

private static final String TAG = FeedListAdapter.class.getSimpleName();

private Activity activity;
private LayoutInflater inflater;
private List<FeedItem> feedItems;
private String URL = "http://192.168.1.10/social/like.php";
private String user_id;
ViewHolder holder;
private boolean[] mHighlightedPositions = new boolean[6];


private static final String TAG_SUCCESS = "success";
FeedItem item;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public FeedListAdapter(Activity activity, List<FeedItem> feedItems, String user_id) {
    this.activity = activity;
    this.feedItems = feedItems;
    this.user_id = user_id;
}

@Override
public int getCount() {
    return feedItems.size();
}

@Override
public Object getItem(int location) {
    return feedItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

public void refresh(List<FeedItem> newlist) {
    feedItems.addAll(newlist);
    this.notifyDataSetChanged();
}
public List<FeedItem> getData() {
    return feedItems;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View vi = convertView;
    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    if (inflater == null)
        inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null){
         convertView = inflater.inflate(R.layout.posts_row, null);

        holder = new ViewHolder();

        holder.name = (TextView)convertView .findViewById(R.id.name);
        holder.timestamp = (TextView) convertView
                .findViewById(R.id.time);
        holder.statusMsg = (TextView) convertView
                .findViewById(R.id.status);
        holder. url = (TextView) convertView.findViewById(R.id.url);
        holder.profilePic = (CircleImageView) convertView
                .findViewById(R.id.profile_img);
        holder.feedImageView = (FeedImageView) convertView
                .findViewById(R.id.image);


        holder.nbr_likes = (TextView) convertView.findViewById(R.id.nbr_likes);
        holder.nbr_comments = (Button)convertView.findViewById(R.id.nbr_comments);

        holder.like =(Button)convertView.findViewById(R.id.like);
        holder.comment =(Button)convertView.findViewById(R.id.comment);
        convertView.setTag(holder);
    }else{

        holder = (ViewHolder)convertView.getTag();
    }

    Log.d(TAG, "Button row pos click: " + getCount());

    holder.like.setTag(position);

    if(mHighlightedPositions[position]) {
        holder.like.setBackgroundColor(Color.BLACK);
        notifyDataSetChanged();

    }else {
        holder.like.setBackgroundColor(Color.BLUE);
        notifyDataSetChanged();

    }

        item = feedItems.get(position);






        holder.like.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {


               int position = (Integer)v.getTag();
                Log.d(TAG, "Button row pos click: " + getCount());
                RelativeLayout layout = (RelativeLayout)v.getParent();
                Button button = (Button)layout.getChildAt(0);
                if(mHighlightedPositions[position]) {
                    button.setBackgroundColor(Color.BLUE);
                    mHighlightedPositions[position] = false;
                    notifyDataSetChanged();
                }else {
                    button.setBackgroundColor(Color.BLACK);
                    mHighlightedPositions[position] = true;
                    notifyDataSetChanged();

                }

                item = feedItems.get(position);
                Map<String, String> params = new HashMap<String, String>();
                params.put("post_id", item.getId());
                params.put("user_id", user_id);
                Toast.makeText(activity, item.getId(), Toast.LENGTH_SHORT).show();
                Toast.makeText(activity, user_id, Toast.LENGTH_SHORT).show();
                // making fresh volley request and getting json
                CustomRequest jsonReq = new CustomRequest(Request.Method.POST,
                        URL,params , new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        VolleyLog.d(TAG, "Response: " + response.toString());
                        if (response != null) {

                            parseJsonFeed(response);


                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                    }
                });

                // Adding request to volley request queue
                AppController.getInstance().addToRequestQueue(jsonReq);



            }

        });



    holder.comment.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {
              Popover pop = new Popover();
                pop.onShowPopup(vi);


            }


        });


    holder.nbr_comments.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View v) {
            item = feedItems.get(position);
            Intent i = new Intent(activity, Comments.class);
            i.putExtra("post_id",item.getId());
            i.putExtra("name",item.getName());
            i.putExtra("image",item.getImge());
            i.putExtra("status",item.getStatus());
            i.putExtra("profile_img",item.getProfilePic());
            i.putExtra("time",item.getTimeStamp());
            i.putExtra("url",item.getUrl());
            i.putExtra("nbr_likes",item.getNbrLikes());
            i.putExtra("nbr_comments",item.getNbrComments());

            activity.startActivity(i);


        }


    });

    holder.name.setText(item.getName());

    holder.nbr_likes.setText(item.getNbrLikes()+ " Likes");
    holder.nbr_comments.setText(item.getNbrComments() + " Comments");


        // Converting timestamp into x ago format
        CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
                Long.parseLong(item.getTimeStamp()),
                System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
    holder.timestamp.setText(timeAgo);

        // Chcek for empty status message
        if (!TextUtils.isEmpty(item.getStatus())) {
            holder.statusMsg.setText(item.getStatus());
            holder.statusMsg.setVisibility(View.VISIBLE);
        } else {
            // status is empty, remove from view
            holder.statusMsg.setVisibility(View.GONE);
        }

        // Checking for null feed url
        if (item.getUrl() != null) {
            holder.url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">"
                    + item.getUrl() + "</a> "));

            // Making url clickable
            holder.url.setMovementMethod(LinkMovementMethod.getInstance());
            holder.url.setVisibility(View.VISIBLE);
        } else {
            // url is null, remove from the view
            holder.url.setVisibility(View.GONE);
        }

        // user profile pic
       holder.profilePic.setImageUrl(item.getProfilePic(), imageLoader);

        // Feed image
        if (item.getImge() != null) {
            holder.feedImageView.setImageUrl(item.getImge(), imageLoader);
            holder.feedImageView.setVisibility(View.VISIBLE);
            holder.feedImageView
                    .setResponseObserver(new FeedImageView.ResponseObserver() {
                        @Override
                        public void onError() {
                        }

                        @Override
                        public void onSuccess() {
                        }
                    });
        } else {
            holder.feedImageView.setVisibility(View.GONE);
        }


        return convertView;
    }


    static class ViewHolder {
        TextView name;
        TextView timestamp;
        TextView statusMsg;
        TextView url;
        CircleImageView profilePic;
        FeedImageView feedImageView;
        TextView nbr_likes;
        Button nbr_comments;
        Button like;
        Button comment;
    }


private void parseJsonFeed(JSONObject response) {
    int success;
    try {
        success = response.getInt(TAG_SUCCESS);
        if (success == 1) {
            notifyDataSetChanged();
        } else {
            Toast.makeText(activity, "erreur", Toast.LENGTH_SHORT).show();
        }


        //listAdapter.addAll(feedItems);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}

This is this the class for popover :

public class Popover extends Activity{

private PopupWindow popWindow;
LayoutInflater layoutInflater;
private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    // call this method when required to show popup
    public void onShowPopup(View v){

        layoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // inflate the custom popup layout
        final View inflatedView = layoutInflater.inflate(R.layout.fb_popup_layout, null,false);
        // find the ListView in the popup layout
        ListView listView = (ListView)inflatedView.findViewById(R.id.commentsListView);

        // get device size
        Display display = getWindowManager().getDefaultDisplay();
        final Point size = new Point();
        display.getSize(size);
        int mDeviceHeight = size.y;


        // fill the data to the list items
        setSimpleList(listView);


        // set height depends on the device size
        popWindow = new PopupWindow(inflatedView, size.x - 50,size.y - 400, true );
        // set a background drawable with rounders corners
        popWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.fb_popup_bg));
        // make it focusable to show the keyboard to enter in `EditText`
        popWindow.setFocusable(true);
        // make it outside touchable to dismiss the popup window
        popWindow.setOutsideTouchable(true);

        // show the popup at bottom of the screen and set some margin at bottom ie,
        popWindow.showAtLocation(v, Gravity.BOTTOM, 0,100);
    }


    void setSimpleList(ListView listView){

        ArrayList<String> contactsList = new ArrayList<String>();

        for (int index = 0; index < 10; index++) {
            contactsList.add("I am @ index " + index + " today " + Calendar.getInstance().getTime().toString());
        }

        listView.setAdapter(new ArrayAdapter<String>(Popover.this,
                R.layout.fb_comments_list_item, android.R.id.text1,contactsList));
    }

 }

I hope you can help me , i spent two days on this problem . Thanks.

Frank
  • 71
  • 2
  • 10
  • `mContext` is null at `mContext.getSystemService`... – OneCricketeer Jan 15 '16 at 20:53
  • I changed mContext with this but now i have another error : java.lang.IllegalStateException: System services not available to Activities before onCreate() .Do you know where this error can come from? – Frank Jan 15 '16 at 20:59
  • Pretty obvious that error says "System services not available to Activities **before onCreate()**" which means your `Popover` Activity has not yet called `onCreate()`. You can't just call Activity methods like `Activity a = new Activity()` then `a.someMethod()` as you have done in `holder.comment.setOnClickListener`. I suggest you read the Android documentation on [Dialogs](http://developer.android.com/guide/topics/ui/dialogs.html) since that is what it seems you are trying to create – OneCricketeer Jan 15 '16 at 21:10

0 Answers0