活动代码:
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:尝试调用虚方法&#39; void android.widget.ImageView.setImageResource(INT)&#39;在null对象上 参考 在 com.universe.time.proffesionaldiary.adapters.IconsAdapter.getView(IconsAdapter.java:60) 在android.widget.AbsListView.obtainView(AbsListView.java:2828) 在android.widget.GridView.onMeasure(GridView.java:1064) 在android.view.View.measure(View.java:18647) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.LinearLayout.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) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在android.view.View.measure(View.java:18647) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在android.view.View.measure(View.java:18647) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.LinearLayout.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) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在 android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 在android.view.View.measure(View.java:18647) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在android.view.View.measure(View.java:18647) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在android.view.View.measure(View.java:18647) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.LinearLayout.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) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在 com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:3012) 在android.view.View.measure(View.java:18647) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2298) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1320) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1588) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230) 在 android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6748) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:777) 在android.view.Choreographer.doCallbacks(Choreographer.java:590) 在android.view.Choreographer.doFrame(Choreographer.java:560) 在 android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:763) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:145) 在android.app.ActivityThread.main(ActivityThread.java:6126) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
答案 0 :(得分:1)
您可以尝试以下代码,它可能会有所帮助。
@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;
}
答案 1 :(得分:0)
错误清楚地表明您正试图在null
图片视图中设置图片,因此请检查
ImageView imageView = (ImageView) grid.findViewById(R.id.imageItemIconGrid)
;
如果是,则图像视图是gridlayout的一部分,而不是检查网格布局的ID和图像视图是否正确。
请调试并检查。