java - Android ImageAdapter with Gridview in Fragment -


i have adapter gridview works activity. trying place in fragment , converted things not work. when include iconfragmentsystem in activity force close when try open activity.

i know activity works because can use other fragments , okay know issue lies within file.

package com.designrifts.ultimatethemeui; import com.designrifts.ultimatethemeui.r; import android.content.context; import android.content.intent; import android.content.res.resources; import android.net.uri; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.*;  import java.util.arraylist;  public class iconfragmentsystem extends fragment implements adapterview.onitemclicklistener{ private static final string result_ok = null; public uri content_uri;  public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {      view view = inflater.inflate(r.layout.main, container, false);      super.oncreate(savedinstancestate);     int iconsize=getresources().getdimensionpixelsize(android.r.dimen.app_icon_size);      gridview gridview = (gridview) getactivity().findviewbyid(r.id.icon_grid);     gridview.setadapter(new iconadapter(this, iconsize));     gridview.setonitemclicklistener(this);     content_uri=uri.parse("content://"+iconsprovider.class.getcanonicalname());     return view; }  @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) {     string icon=adapterview.getitematposition(i).tostring();     intent result = new intent(null, uri.withappendedpath(content_uri,icon));     setresult(result_ok, result);     finish(); } private void setresult(string resultok, intent result) {     // todo auto-generated method stub  }  private void finish() {     // todo auto-generated method stub  } private class iconadapter extends baseadapter{     private context mcontext;     private int miconsize;     public iconadapter(context mcontext, int iconsize) {         super();         this.mcontext = mcontext;         this.miconsize = iconsize;         loadicon();     }      public iconadapter(iconfragmentsystem iconssystem, int iconsize) {         // todo auto-generated constructor stub     }      @override     public int getcount() {         return mthumbs.size();     }      @override     public object getitem(int position) {         return mthumbs.get(position);     }      @override     public long getitemid(int position) {         return position;     }      @override     public view getview(int position, view convertview, viewgroup parent) {         imageview imageview;         if (convertview == null) {             imageview = new imageview(mcontext);             imageview.setlayoutparams(new gridview.layoutparams(miconsize, miconsize));         } else {             imageview = (imageview) convertview;         }         imageview.setimageresource(mthumbs.get(position));         return imageview;     }      private arraylist<integer> mthumbs;     ////////////////////////////////////////////////     private void loadicon() {         mthumbs = new arraylist<integer>();          final resources resources = getresources();         final string packagename = getactivity().getapplication().getpackagename();          addicon(resources, packagename, r.array.systemicons);      }     private void addicon(resources resources, string packagename, int list) {         final string[] extras = resources.getstringarray(list);         (string : extras) {             int res = resources.getidentifier(extra, "drawable", packagename);             if (res != 0) {                 final int thumbres = resources.getidentifier(extra,"drawable", packagename);                 if (thumbres != 0) {                     mthumbs.add(thumbres);                 }             }             }            }         }     } 

i have tried different ways implement have failed , use pointing me in right direction.

this xml in layout

    <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:app="http://schemas.android.com/apk/res-auto"         xmlns:tools="http://schemas.android.com/tools"         android:layout_width="match_parent"         android:layout_height="match_parent" >              <gridview     android:id="@+id/gridview"     android:layout_width="fill_parent"      android:layout_height="fill_parent"     android:columnwidth="90dp"     android:numcolumns="auto_fit"     android:verticalspacing="10dp"     android:horizontalspacing="10dp"     android:stretchmode="columnwidth"     android:gravity="center" />      </relativelayout> 

i able fix issue

i changed

    gridview gridview = (gridview) getactivity().findviewbyid(r.id.icon_grid); 

to

    gridview gridview = (gridview) view.findviewbyid(r.id.icon_grid); 

and changed

     gridview.setadapter(new iconadapter(this, iconsize)); 

to

     gridview.setadapter(new iconadapter(getactivity(), iconsize)); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -