Device 6.0上的致命IndexOutOfBoundsException

时间:2016-08-02 07:28:23

标签: android android-dialogfragment

  

请不要将其标记为重复的问题。

public class CategoriesFragment extends DialogFragment {
private static CategoriesFragment instance;
GridView gv_categories;
CategoriesAdapter categoriesAdapter;
private ArrayList<Category> mItems = new ArrayList<>();
private String[] mCategories;


public CategoriesFragment() {
    // Required empty public constructor
}

public static synchronized CategoriesFragment newInstance() {
    if (instance == null) {
        instance = new CategoriesFragment();
    }
    return instance;
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_categories, container, false);
    gv_categories = (GridView) v.findViewById(R.id.gv_categories);

    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    categoriesAdapter = new CategoriesAdapter(getActivity(),R.layout.row_categoeis, mItems );
    gv_categories.setAdapter(categoriesAdapter);
    mCategories = getActivity().getResources().getStringArray(R.array.main_category_array);

   loadCategories();
}


private void loadCategories() {

    mItems.clear();
    Category category;
    /*This is the array, looping through.

    <string-array name="main_category_array">
        <item>Electronics</item> 
        <item>Vehicle</item>
        <item>Property</item>
        <item>Services</item>
        <item>Job</item>
        <item>Education</item>
        <item>Sports</item>
        <item>Agriculture</item>
        <item>Clothing</item>
        <item>Food</item>
        <item>Animal</item>
        <item>Other</item>
    </string-array>


    */
    for (int i = 0; i < mCategories.length; i++) {
            category =  new Category();
            category.setDes_en(mCategories[i]);            
            mItems.add(category);
            categoriesAdapter.notifyDataSetChanged();
        }
}



class CategoriesAdapter extends ArrayAdapter<Category> {
    private Context mContext;
    private ArrayList<Category> listItems;
    // references to our images


    public CategoriesAdapter(Context context, int resource, ArrayList<Category> objects) {
        super(context, resource, objects);
        this.listItems = objects;
        this.mContext = context;
    }

    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_categoeis, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.tv_category);
            holder.icon = (ImageView) convertView.findViewById(R.id.iv_category);
            holder.rl_container = (RelativeLayout) convertView.findViewById(R.id.rl_container);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Category category = getItem(position);
        holder.title.setText(category.getDes_en());

        holder.rl_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {  
                // This is the callback for start asynctask in MainActivity.
                Communicator communicator = (Communicator) getActivity();
                communicator.selectedCategory(position, instance);
                //dismiss();  
            }
        });

        return convertView;
    }

   public class ViewHolder {
        TextView title;
        RelativeLayout rl_container;
        ImageView icon;
    }

}

interface Communicator {
   void selectedCategory(int category);

}

这是我实施的活动。

public class MainActivity extends AppCompatActivity
    implements CategoriesFragment.Communicator{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    }




@Override
public void selectedCategory(int category,DialogFragment fragment) {
    // this is the callback get called from CategoriesFragment.     
    fragment.dismiss();  // I get crash here. I tried to make dismiss inside the Adapter also.
    getAnnouncementByQuery("main_category", String.valueOf(category),true); //This is an async Task.
    //        try {
    //            Thread.sleep(1000);
    //            fragment.dismiss();
    //        } catch (Exception e) {
    //            e.printStackTrace();
    //        }
}



@Override
public void selectedDistrict(int district, DialogFragment fragment) {
    int dis = district;
    fragment.dismiss();
    getItemsByQuery();  
    // This is an async Task, which is called on Category item is clicked.
    //        try {
    //          Thread.sleep(1000);
    //            fragment.dismiss();
    //        } catch (InterruptedException e) {
    //            e.printStackTrace();
    //        }
    //


}

我已经写过我面对设备6.0的问题,在其他设备中它工作正常。但是在6.0中,设备通过Array索引超出范围。  我试图在几秒后关闭对话框片段,但它不能100%工作,我在6.0设备上得到ArrayIndexOutOfBoundsException,其他设备为例         //三星s4和其他Genymotion仿真器工作正常。         //有关我在下面提供数组的更多信息。

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
                                                                     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                     at java.util.ArrayList.get(ArrayList.java:308)
                                                                     at android.widget.HeaderViewListAdapter.isEnabled(HeaderViewListAdapter.java:164)
                                                                     at android.widget.ListView.dispatchDraw(ListView.java:3330)
                                                                     at android.view.View.draw(View.java:16241)
                                                                     at android.widget.AbsListView.draw(AbsListView.java:4375)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15235)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3637)
                                                                     at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3617)
                                                                     at android.view.View.updateDisplayListIfDirty(View.java:15195)
                                                                     at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
                                                                     at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
                                                                     at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
                                                                     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2682)
                                                                     at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2496)
                                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2125)
                                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1140)
                                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6233)
                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                     at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5527)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:7

0 个答案:

没有答案