Spinner adapter issue in Android 5.0

2019-05-28 09:47发布

问题:

My app is not compatible with Android 5.

When I run it in any other version it works with no issues.

This is what appears in the logcat when run it in Android 5:

05-16 23:44:10.287    1969-1969/com.barakah.camel E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.barakah.camel, PID: 1969
java.lang.IllegalArgumentException: Spinner adapter view type count must be 1

Here's where the problem lies based on the logcat:

 public void regionSpinnerSetup() {
    ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {
            ParseQuery query = new ParseQuery("Region");
            return query;
        }
    };

    regionAdapter = new ParseQueryAdapter<ParseObject>(getActivity(),
            factory);
    regionAdapter.setTextKey("name");
    regionSpinner.setAdapter(regionAdapter);
    regionSpinner.setSelection(1);
    regionSpinner.setOnItemSelectedListener(new RegionSpinnerListener());
}

public void CategorySpinnerSetup() {
    ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {
            ParseQuery query = new ParseQuery("Category");
            return query;
        }
    };

    categoryAdapter = new ParseQueryAdapter<ParseObject>(getActivity(),
            factory);
    categoryAdapter.setTextKey("name");
    categorySpinner.setAdapter(categoryAdapter);
    categorySpinner.setSelection(1);
    categorySpinner
            .setOnItemSelectedListener(new CategorySpinnerListener());
}

class RegionSpinnerListener implements Spinner.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView parent, View v, int position,
                               long id) {
        // TODO Auto-generated method stub

        ParseObject theSelectedObject = regionAdapter.getItem(position);
        Log.e("ABC", "Name is : " + theSelectedObject.getString("name") + " objectId is : " + theSelectedObject.getObjectId());
        regionobject = theSelectedObject;
    }

Both regionSpinner and categorySpinner are objects of type Spinner ...

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    regionSpinnerSetup();
    CategorySpinnerSetup();

}

Any idea how to solve it?

回答1:

I don't have experience with ParseQueryAdapter, but from reading up on it, it seems you could do something like this.

First, create your own class that extends ParseQueryAdapter, and override getViewTypeCount():

public class MyParseAdapter extends ParseQueryAdapter<ParseObject> {

  public MyParseAdapter(Context context, QueryFactory<ParseObject> clazz){        
        super(context, clazz); 
  }

  @Override
  public int getViewTypeCount() {
     return 1;
  }
}

Then, just every place you have ParseQueryAdapter in your code, replace it with MyParseAdapter:

public void regionSpinnerSetup() {
    MyParseAdapter.QueryFactory<ParseObject> factory = new MyParseAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {
            ParseQuery query = new ParseQuery("Region");
            return query;
        }
    };

    regionAdapter = new MyParseAdapter<ParseObject>(getActivity(),
            factory);
    regionAdapter.setTextKey("name");
    regionSpinner.setAdapter(regionAdapter);
    regionSpinner.setSelection(1);
    regionSpinner.setOnItemSelectedListener(new RegionSpinnerListener());
}

public void CategorySpinnerSetup() {
    MyParseAdapter.QueryFactory<ParseObject> factory = new MyParseAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {
            ParseQuery query = new ParseQuery("Category");
            return query;
        }
    };

    categoryAdapter = new MyParseAdapter<ParseObject>(getActivity(),
            factory);
    categoryAdapter.setTextKey("name");
    categorySpinner.setAdapter(categoryAdapter);
    categorySpinner.setSelection(1);
    categorySpinner
            .setOnItemSelectedListener(new CategorySpinnerListener());
}

class RegionSpinnerListener implements Spinner.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView parent, View v, int position,
                               long id) {
        // TODO Auto-generated method stub

        ParseObject theSelectedObject = regionAdapter.getItem(position);
        Log.e("ABC", "Name is : " + theSelectedObject.getString("name") + " objectId is : " + theSelectedObject.getObjectId());
        regionobject = theSelectedObject;
    }


回答2:

You cannot solve this because in lollypop this problem is pertaining. So this app can run on API 19 or below.

If you are developing this application, then only you can do a small change in project. Change your adapter.getViewTypeCount() to 1. That is the only solution.

Here you can read for more reference