Alerdialog with Gridview(Imageadpater)
Code in Activity:
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();
}
Code in 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>
Log:
FATAL EXCEPTION: main Process: com.universe.time.officereminder, PID: 25213 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at com.universe.time.proffesionaldiary.adapters.IconsAdapter.getView(IconsAdapter.java:60) at android.widget.AbsListView.obtainView(AbsListView.java:2828) at android.widget.GridView.onMeasure(GridView.java:1064) at android.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) at android.widget.LinearLayout.onMeasure(LinearLayout.java: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.view.View.measure(View.java:18647) at android .view.ViewGroup.measureChildWithMargins(ViewGroup.java: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) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435) at android.widget.LinearLayout.measureVertical(LinearLayout.java:721) at android.widget.LinearLayout.onMeasure(LinearLayout.java: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.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) at android.view.View.measure(View.java:18647) at android.view.ViewGroup.measureChildWithMargins(ViewG roup.java: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) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435) at android.widget.LinearLayout.measureVertical(LinearLayout.java:721) at android.widget.LinearLayout.onMeasure(LinearLayout.java: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 com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3012) at android.view.View.measure(View.java:18647) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2298) at android.view.ViewRootImpl.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.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
You can try the following code, it may helpful.
@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;
}
Reference: https://www.caveofprogramming.com/guest-posts/custom-gridview-with-imageview-and-textview-in-android.html
error clearly suggest that you are trying to set image in null
image view so check
ImageView imageView = (ImageView) grid.findViewById(R.id.imageItemIconGrid)
;
is image view is part of your gridlayout if yes than check id for grid layout and for imageview if it is right or not .
please debug and check.
链接地址: http://www.djcxy.com/p/84728.html