Android:使用Gridview

时间:2015-11-24 18:48:47

标签: android nullpointerexception

您好,感谢您的帮助!

单击按钮后,alertdialog应该打开,里面有一个gridview。

这是我的代码:

    loginWith.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
            builder.setTitle(getResources().getString(R.string.loginWith));
            final AlertDialog dialog = builder.create();

            LayoutInflater inflater = getLayoutInflater();
            View layout = inflater.inflate(R.layout.dialog_loginwith, (ViewGroup) findViewById(R.id.layout_loginwith));
            builder.setView(layout);

            GridView gridView = (GridView) layout.findViewById(R.id.gvLoginwith);
            gridView.setAdapter(new LoginWithAlertDialogImageAdapter(context));
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    switch (position) {
                        case 0:
                            loginWithFacebook();
                            break;
                        case 1:
                            loginWithGoogle();
                            break;
                        case 2:
                            loginWithLinkedIn();
                            break;
                        case 3:
                            loginWithTwitter();
                            break;
                    }
                    dialog.dismiss();
                }
            });
            dialog.show();
        }
    });

但它最终会在上面的gridView.setAdapter()部分和下一个类中的context.getResources().getString()方法中留下NullPointerException。

public class LoginWithAlertDialogImageAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mLayoutInflater;

    public LoginWithAlertDialogImageAdapter(Context context) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.categorycontent_log_in_with, null);
            convertView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.loginwith_title);
            holder.icon = (ImageView) convertView.findViewById(R.id.loginwith_icon);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.icon.setAdjustViewBounds(true);
        holder.icon.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.icon.setPadding(8, 8, 8, 8);
        holder.title.setText(items[position]);
        holder.icon.setImageResource(icons[position]);

        return convertView;
    }

    class ViewHolder {
        TextView title;
        ImageView icon;
    }

    private Integer[] icons = {
            R.drawable.ic_facebook,
            R.drawable.ic_google,
            R.drawable.ic_linkedin,
            R.drawable.ic_twitter
    };

    private String[] items = {
            context.getResources().getString(R.string.facebook),
            context.getResources().getString(R.string.google),
            context.getResources().getString(R.string.linkedin),
            context.getResources().getString(R.string.twitter)
    };
}

如何克服这个问题?

已添加我按照以下建议更正了适配器代码。

public class LoginWithAlertDialogImageAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mLayoutInflater;
    private String items[];

    public LoginWithAlertDialogImageAdapter(Context context) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);

        items = new String[] {
                context.getResources().getString(R.string.facebook),
                context.getResources().getString(R.string.google),
                context.getResources().getString(R.string.linkedin),
                context.getResources().getString(R.string.twitter)
        };
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.categorycontent_log_in_with, null);
            convertView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.loginwith_title);
            holder.icon = (ImageView) convertView.findViewById(R.id.loginwith_icon);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.icon.setAdjustViewBounds(true);
        holder.icon.setScaleType(ImageView.ScaleType.FIT_XY);
        holder.icon.setPadding(8, 8, 8, 8);
        holder.title.setText(items[position]);
        holder.icon.setImageResource(icons[position]);

        return convertView;
    }

    class ViewHolder {
        TextView title;
        ImageView icon;
    }

    private Integer[] icons = {
            R.drawable.ic_facebook,
            R.drawable.ic_google,
            R.drawable.ic_linkedin,
            R.drawable.ic_twitter
    };
}

现在不再抛出NullPointerException了,但事情是图标和图标标题以及GridView本身没有显示,但只显示了AlertDialog的标题。你明白下一个问题吗?

1 个答案:

答案 0 :(得分:1)

下面:

context.getResources().getString(R.string.facebook)

contextnull

<强>为什么吗

itemsitems数组中添加项目期间,使用context访问getResources,声明并初始化数组为类级变量方法null因为contextLoginWithAlertDialogImageAdapter类的构造函数内初始化。

在执行context类的构造函数之前访问方法LoginWithAlertDialogImageAdapter

因此,要解决此问题,请将items数组声明为类级别变量,但在初始化context后在其中添加项目:

private String[] items;
public LoginWithAlertDialogImageAdapter(Context context) {
        this.context = context;
        ...
        items = {
            context.getResources().getString(R.string.facebook),
              ...
             };
    }