这是我的mainActivity.java
package com.domore.gridviewtutorial;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
public class AndroidGridLayoutActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_grid_layout);
GridView gridView=(GridView)findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
}
}
我有一个grid_layout.xml,它可以从drawable目录中看到我的所有图像
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
我还有一个类适配器类,它会使这个grid_layout.xml膨胀,即ImageAdapter.java
package com.domore.gridviewtutorial;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
/**
* Created by MY WORLD on 11/28/2015.
*/
public class ImageAdapter extends BaseAdapter {
private Context context;
//Keep all images into an array
public Integer[] mThumbIds={R.drawable.first,R.drawable.second,R.drawable.third,R.drawable.fourth,
R.drawable.fifth,R.drawable.sixth,R.drawable.seventh,R.drawable.eighth,R.drawable.ninth,R.drawable.tenth,
R.drawable.eleventh,R.drawable.twelth,R.drawable.thirteenth,R.drawable.fourtheenth,R.drawable.fiftheenyth};
public ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView=new ImageView(context);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70,70));
return imageView;
}
}
在这里完成LOGCAT
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.domore.gridviewtutorial.AndroidGridLayoutActivity.onCreate(AndroidGridLayoutActivity.java:14)
任何人都可以解决这个问题吗?
答案 0 :(得分:3)
替换此 -
singleton
with - setContentView(R.layout.activity_android_grid_layout);
您使用了错误的setContentView(R.layout.grid_layout);
。
答案 1 :(得分:2)
以下是您问题的完整解决方案
ImageAdapter类
public class ImageAdapter extends BaseAdapter {
private Context context;
// Keep all images into an array
public Integer[] mThumbIds = { R.drawable.ic_communities,
R.drawable.ic_home, R.drawable.ic_launcher, R.drawable.ic_pages,
R.drawable.ic_people, R.drawable.ic_people, R.drawable.ic_whats_hot };
public ImageAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}}
将代码置于oncreate MainActivity
中setContentView(R.layout.grid_layout.xml);// Missing this to explain
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
second.xml文件
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
这对我有用,请查看。
快乐的编码,乐于助人。
答案 2 :(得分:0)
您已将内容视图活动设置为不同的布局
setContentView(R.layout.activity_android_grid_layout);
将其更改为
setContentView(R.layout.grid_layout.xml);
包括网格布局所在的布局。