带Gridview的Alerdialog(Imageadpater)
活动代码:
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();
}
在IconsAdapter中的代码:
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>
日志:
致命异常:主进程:com.universe.time.officereminder,PID:25213 java.lang.NullPointerException:尝试在com.universe的空对象引用上调用虚方法'void android.widget.ImageView.setImageResource(int)' 。android.widget.GridView.onMeasure(GridView.java:1064)上的android.widget.AbsListView.obtainView(AbsListView.java:2828)上的.time.proffesionaldiary.adapters.IconsAdapter.getView(IconsAdapter.java:60)。 view.View.measure(View.java:18647)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)at android.widget.LinearLayout.measureVertical (LinearLayout.java:721)在android.view.View.measure(View.java:18647)android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:android.widget.LinearLayout.onMeasure(LinearLayout.java:612) 5832)at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)at android.view.View.measure(View.java:18647)at android .view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)在android.view.ViewGroup上的android.view.View.measure(View.java:18647)上的android.widget.FrameLayout.onMeasure(FrameLayout.java:430)。在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)android.widget.LinearLayout.measureVertical(LinearLayout.java:721)android.widget.LinearLayout.onMeasure(LinearLayout.java)上的measureChildWithMargins(ViewGroup.java:5832) :612)at android.view.View.measure(View.java:18647)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)at android 。android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)上的android.view.View.measure(View.java:18647)上的.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)。 widget.FrameLayout.onMeasure(FrameLayout.java:430)at android.view.View.measure(View.java:18647)at android.view.ViewGroup.measureChildWithMargins(ViewG roup.java:5832)在android.view.View.measure(View.java:18647)android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)android.widget.FrameLayout.onMeasure(FrameLayout.java:430) )android.widget.Lineout.measureChildBeforeLayout(LinearLayout.java:1435)在android.widget.LinearLayout.measureVertical(LinearLayout.java:721)在android.widget.LinearLayout.onMeasure(LinearLayout.java:612)在android.view .View.measure(View.java:18647)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)at com.android.internal.policy。在android.view.View.App.ViewRootImpl上的android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2298)上的android.view.View.measure(View.java:18647)上的impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:3012) .measureHierarchy(ViewRootImpl.java:1320)at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1588)at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230)a t android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6748)at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:777)at android.view.Choreographer.doCallbacks(Choreographer.java:590)at android.view.Choreographer.doFrame(Choreographer.java:560)at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:763)at android.os.Handler.handleCallback(Handler.java:739)at android.os .Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:145)at android.app.ActivityThread.main(ActivityThread.java:6126)at java.lang.reflect.Method。在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)com.android.internal处调用(Native Method)在java.lang.reflect.Method.invoke(Method.java:372)。 os.ZygoteInit.main(ZygoteInit.java:1194)
你可以尝试下面的代码,它可能会有帮助。
@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;
}
参考:https://www.caveofprogramming.com/guest-posts/custom-gridview-with-imageview-and-textview-in-android.html
错误清楚地表明你正试图在null
图像视图中设置图像,所以检查
ImageView imageView = (ImageView) grid.findViewById(R.id.imageItemIconGrid)
;
图像视图是网格布局的一部分,如果是,则检查网格布局的ID,如果图像视图正确,则检查图像视图。
请调试并检查。
链接地址: http://www.djcxy.com/p/84727.html上一篇: Alerdialog with Gridview(Imageadpater)
下一篇: Android text and image adapter for gridview in fragment