Trying to call virtual method 'java.lang.Object android.content.Context.getSystemService (java.lang.String)

I want to ask why my code does not work when working on a device, but when I run on an Android emulator like Genymotion, it works fine.

someone said on this link as follows: you cannot call methods on the superclass Activity until super.onCreate (), except in some situations. Please defer your initialization of promptsView until the call to super.onCreate () is called.

I still do not understand, please tell me if you have the same problems.

In any case, 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;
    }
+4
source share
1

, getContext()!= null

:

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

getActivity()!= null

:

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

, , , !

+14

Source: https://habr.com/ru/post/1654811/


All Articles