2016-03-23 16 views
-1

kod ile Alerdialog: IconsAdapter içindeGridview (Imageadpater) Faaliyet içinde

public void iconSelector(View view) { 
    CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.create_coordinator); 
    LayoutInflater inflater = getLayoutInflater(); 
    View dialogView = inflater.inflate(R.layout.item_icon_grid_layout, coordinatorLayout, false); 


    GridView gridView = (GridView) dialogView.findViewById(R.id.itemIconGridLayout); 
    database = new Database(this); 
    mId = database.getLastIdOD(); 
    if(mId>=1) { 
     iconList = database.getIconList(); 

    } 
    database.close(); 
    gridView.setAdapter(new IconsAdapter(getApplicationContext(),iconList)); 
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      mIcon = iconList.get(position).getName(); 
     } 
    }); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setView(dialogView); 
    builder.setTitle(R.string.iconChoose); 
    mIconSelectorDialog= builder.show(); 
} 

Kodu:

public class IconsAdapter extends BaseAdapter { 
    private Context mContext; 
List<Icon> iconList; 

public IconsAdapter(Context context,List<Icon> list){ 
    mContext = context; 
    iconList = list; 
    int d = iconList.size(); 
    String i= ""+d; 
    Log.d("MyLogIconAdapter",iconList.get(0).getName()); 
    Log.d("MyLog",i); 
} 

public int getCount() { 
    return iconList.size(); 

} 

public Object getItem(int position) { 
    return iconList.get(position).getId(); 
} 

public long getItemId(int position) { 
    return position; 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    View grid; 
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if (convertView == null) { 
     grid = new View(mContext); 
     grid = inflater.inflate(R.layout.item_icon_grid,null); 
     ImageView imageView = (ImageView) grid.findViewById(R.id.imageItemIconGrid); 
     int iconResId = mContext.getResources().getIdentifier(iconList.get(position).getName(), "drawable", mContext.getPackageName()); 
     imageView.setImageResource(iconResId); 
    } else { 
     grid = (View) convertView; 
    } 


    return grid; 
} 

}

Item_icon_grid:

<ImageView 
    android:id="@+id/imageItemIconGrid" 
    android:src="@drawable/ic_drafts_white_24dp" 
    android:layout_width="18dp" 
    android:layout_height="18dp" 
    android:layout_gravity="center" /> 

item_icon_grid_layout:

<GridView 
    android:id="@+id/itemIconGridLayout" 
    android:numColumns="auto_fit" 
    android:background="#ffffff" 
    android:verticalSpacing="5dp" 
    android:horizontalSpacing="5dp" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</GridView> 

Giriş:

ÖLÜMCÜL İSTİSNA: Ana Süreci: com.universe.time.officereminder PID: 25213 java.lang.NullPointerException: girişimi sanal yöntemi çağırmak için 'boşluk android.widget.ImageView com.universe.time.proffesionaldiary.adapters.IconsAdapter.getView (IconsAdapter.java:60) adresindeki null object reference adlı bir boş nesne başvurusundaki .setImageResource (int) 'android.widget.AbsListView.obtainView (AbsListView.java:2828) adresinden android.widget.GridView.onMeasure adresinden GridView.java:1064) android.widget.LinearLayout.measureChildBeforeLayout de android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5832) de android.view.View.measure (View.java:18647) de (LinearLayout. java: 1435) android.widget.LinearLayout.measureVertical adresinde (LinearLayout.java:721) at android.widget.LinearLayout.onMeasure (LinearLayout.java:612) at android.view.View.measure (View.java: 18647) android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5832) at android.widget.FrameLayout.onMeasure (FrameLayout.java:430) android.view.View.measure (View.java:18647) at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5832) at android.widget.FrameLayout.onMeasure (FrameLayout.java:430) Android'de android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1435) de android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5832) de .view.View.measure (View.java:18647) de android.widget .LinearLayout.measureVertical (LinearLayout.java:721) android.widget.LinearLayout.onMeasure (LinearLayout.java:612) at android.view.View.measure (View.java:18647)Android.view.ViewGroup.measureChildWithMargins'teki (ViewGroup.java:5832) , android.widget.FrameLayout.onMeasure (FrameLayout.java:430) at android.support.v7.widget.ContentFrameLayout.onMeasure (ContentFrameLayout.java: 135) at android.view.View.measure (View.java:18647) at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5832) at android.widget.FrameLayout.onMeasure (FrameLayout.java:430) Android.view.View.measure adresinden (View.java:18647) android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5832) adresindeki android.widget adresindeki .Android.view.View.measure adresindeki FrameLayout.onMeasure (FrameLayout.java:430) (View.java:18647) android.view.ViewGroup.measureChildWithMargins öğesinde (ViewGroup.java:5832) , android.widget.LinearLayout adresinde . android.view.View.measure de measureChildBeforeLayout android.widget.LinearLayout.onMeasure (LinearLayout.java:612) de android.widget.LinearLayout.measureVertical (LinearLayout.java:721) 'den (LinearLayout.java:1435) (View.java:18647) android.view.ViewGroup.measureChildWithMargins adresinden (ViewGroup.java:5832) android.widget.FrameLayout.onMeasure (FrameLayout.java:430)Com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure (PhoneWindow.java:3012) adresindeki android.view.View.measure (View.java:18647) adresindeki android.view.ViewRootImpl.performMeasure () ViewRootImpl.java:2298) android.view.ViewRootImpl.doTraversal de android.view.ViewRootImpl.performTraversals de android.view.ViewRootImpl.measureHierarchy (ViewRootImpl.java:1320) (ViewRootImpl.java:1588) de (ViewRootImpl. java: 1230) android.view.ViewRootImpl $ TraversalRunnable.run adresindeki (ViewRootImpl.java:6748) , android.view.Choreographer $ CallbackRecord.run (Choreographer.java:777) android.view.Choreographer $ FrameDisplayEventReceiver.run (Choreographer.java:763) de android.view.Choreographer.doFrame (Choreographer.java:560) de android.view.Choreographer.doCallbacks (Choreographer.java:590) de android.os.Handler.handleCallback'de (Handler.java:739) at android.os.Handler.dispatchMessage (Handler.java:95) at android.os.Looper.loop (Looper.java:145) android olarak java.lang.reflect.Method.invoke (Yerel Yöntem) at java.lang.reflect.Method.invoke (Yöntem.java:372) (http://support.microsoft.com/downloads/default.htm). com.android.internal.os.ZygoteInit.main de com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1399) (ZygoteInit.java:1194) de

+0

Eğer setImageResources sırasında boş görüntüler elde edilir – Manifest

+0

Olası kopyalar [I düzeltirim Ne Boş İşaretçi İstisna nedir ve nasıl?] (Http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) –

cevap

0

hata açıkça size düşündürmektedir görüntüyü null görüntü görünümünde ayarlamaya çalışıyoruz; bu nedenle,

;

görüntü görüntüsü, grid düzeninin kimliğini kontrol etmekten ve eğer doğruysa ya da olmasın görüntü için kimlikten ziyade, gridlayout öğenizin bir parçasıdır.

hata ayıklama ve kontrol edin.

1

Aşağıdaki kodu deneyebilirsiniz, yararlı olabilir.

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     View grid = inflater.inflate(R.layout.item_icon_grid, null); 

     if (convertView == null) { 
      ImageView imageView = (ImageView) grid.findViewById(R.id.imageItemIconGrid); 
      int iconResId = mContext.getResources().getIdentifier(iconList.get(position).getName(), "drawable", mContext.getPackageName()); 
      imageView.setImageResource(iconResId); 
     } else { 
      grid = (View) convertView; 
     } 
     return grid; 
    } 

Referans: senin adaptörden https://www.caveofprogramming.com/guest-posts/custom-gridview-with-imageview-and-textview-in-android.html