我正在关注AbhiAndroid教程,我希望我的第一个,MainActivity启动一个新的SecondActivity,我的应用程序主要用于显示不同Android徽标的网格,并在点击时将它们放大到原始大小; 但是,我得到了
FATAL EXCEPTION: main
Process: fuckoff.com.gridviewexample, PID: 3127
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at fuckoff.com.gridviewexample.CustomAdapter.getView(CustomAdapter.java:30)
是的,我已经阅读了所有现有的帖子,从此开始 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference, 这个 findViewByID returns null
和其他一些人,仍然没有找到如何将其应用于我的案例
问题的原因是:
package fuckoff.com.gridviewexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class CustomAdapter extends BaseAdapter{
Context context;
int logos[];
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] logos){
this.context = applicationContext;
this.logos = logos;
inflter = (LayoutInflater.from(applicationContext));}
@Override
public int getCount(){return logos.length;}
@Override
public Object getItem(int i){return null;}
@Override
public long getItemId(int i){return 0;}
@Override
public View getView(int i,View view,ViewGroup viewGroup){
view = inflter.inflate(R.layout.activity_gridview,null);
ImageView icon = (ImageView)view.findViewById(R.id.icon);
icon.setImageResource(logos[i]);
return view;}
}
答案 0 :(得分:0)
您需要在viewGroup
方法中添加null
参数代替inflate
。在getView
方法中将其替换为这样。
view = inflter.inflate(R.layout.activity_gridview, viewGroup);