Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)

Tags:

java

android

i want to ask, why my code doesnt work when running on device, but when i run on emulator android like a Genymotion, it's working perfectly..

someone said on this link like this : you cannot call methods on the Activity superclass until after super.onCreate(), except in certain situations. Please postpone your initialization of promptsView until after super.onCreate() has been called.

i still dont get it, please tell me if you have the same problems..

anyway , i'm sorry if my explanation is bad..

public class DestinationListAdapter extends ArrayAdapter<DestinationModel> {

    Context context;
    int layoutResourceId;
    List<DestinationModel> data = Collections.emptyList();

    public DestinationListAdapter(Context context, int layoutResourceId, List<DestinationModel> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

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

        View row = convertView;
        DestinationHolder holder = null;

        if(row == null)
        {
            // Predicted Error At Line Below
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new DestinationHolder();
            holder.destination_id = (TextView) row.findViewById(R.id.destination_id);
            holder.destination_name = (TextView) row.findViewById(R.id.destination_name);

            row.setTag(holder);
        }
        else
        {
            holder = (DestinationHolder)row.getTag();
        }

        DestinationModel weather = data.get(position);
        holder.destination_id.setText(weather.getDestination_id());
        holder.destination_name.setText(weather.getDestination_name());

        return row;
    }
like image 384
Iam ByeBlogs Avatar asked Sep 16 '16 13:09

Iam ByeBlogs


2 Answers

for make sure when return back & continuing previous activity i just Added & checking for getContext()!=null

Here's an good example :

Before block

adapter = new ExampleAdapter(getContext());
adapter.setData(items);
listView.setAdapter(adapter);

And better replace for getActivity()!=null

For example:

if (getActivity()!=null){
    adapter = new ExampleAdapter(getActivity());
    adapter.setData(items);
    listView.setAdapter(adapter);
}

I think this is solved all problem which got the same error like my problems !

like image 165
Iam ByeBlogs Avatar answered Nov 15 '22 05:11

Iam ByeBlogs


Be conscious of where in the lifecycle you are. The value of getContext() may not be available yet.

For example, in a DialogFragment, the context will not be available until onCreateDialog() is called. So don't try and create an adapter in the constructor, because the context will still be null at that point.

like image 3
Paul LeBeau Avatar answered Nov 15 '22 03:11

Paul LeBeau