2 luofei987 luofei987 于 2016.05.06 17:06 提问

andriod gridview 空指针异常

package com.example.weishi03;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.weishi03.utils.Md5;
import com.example.weishi03.utils.MyConstant;
import com.example.weishi03.utils.SpTool;

public class HomeActivity extends Activity {

private int[] icons = {R.drawable.one,R.drawable.two,R.drawable.three,
        R.drawable.four,R.drawable.five,R.drawable.six,
        R.drawable.seven,R.drawable.eight,R.drawable.nine};
private String[] names = {"one","two","three","four","five","six","seven","eight","nine"};
private GridView gv;
private MyAdapter adapter;  
private AlertDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    initView();
    initData();
    initEvent();
}

private void initEvent() {
    gv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            switch (position) {
            case 0:
                if (TextUtils.isEmpty(SpTool.getString(getApplicationContext(), MyConstant.PASSWORD, ""))){
                    showSetPassWord();
                }else {
                    showEnterPassWord();
                }
                break;
            default:
                break;
            }
        }
    });

}

protected void showEnterPassWord() {
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    View view =View.inflate(getApplicationContext(), R.layout.dialog_enterpass, null);
    final EditText pass = (EditText) findViewById(R.id.et_dialogenterpassword_pass);
    Button confim = (Button) findViewById(R.id.bt_dialogenterpassword_confim);
    Button cancel = (Button) findViewById(R.id.bt_dialogenterpassword_cancel);
    ab.setView(view);
    confim.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String passWord = pass.getText().toString();
            String truePass = SpTool.getString(getApplicationContext(), MyConstant.PASSWORD, "");
            passWord = Md5.md5(Md5.md5(passWord));
            if (passWord == null || passWord != truePass ) {
                Toast.makeText(getApplicationContext(), "Wrong pass", 1).show();
                return;
            }else {
                Intent intent = new Intent(HomeActivity.this,LostFindActivity.class);
                startActivity(intent);
            }
            dialog.dismiss();
        }
    });
    cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog = ab.create();
    dialog.show();
}

protected void showSetPassWord() {
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    View view = View.inflate(getApplicationContext(), R.layout.dialog_setpass, null);
    final EditText et_passone = (EditText) findViewById(R.id.et_dialogsetpass_passone);
    final EditText et_passtwo = (EditText) findViewById(R.id.et_dialogsetpass_passtwo);
    Button bt_confim = (Button) findViewById(R.id.bt_dialogsetpass_confim);
    Button bt_cancel = (Button) findViewById(R.id.bt_dialogsetpass_cancel);
    ab.setView(view);
    bt_confim.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            String passone = et_passone.getText().toString().trim();
            String passtwo = et_passtwo.getText().toString().trim();
            if (passone.equals("") || passtwo.equals("")) {
                Toast.makeText(getApplicationContext(), "Pass is null", 1).show();
                return;
            }else if (!passone.equals(passtwo)) {
                Toast.makeText(getApplicationContext(), "Pass is nu", 1).show();
                return;
            }else {
                passone = Md5.md5(Md5.md5(passone));
                SpTool.putString(getApplicationContext(), MyConstant.PASSWORD, passone);
                dialog.dismiss();
            }
        }
    });
    bt_cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    dialog = ab.create();
    dialog.show();
}

private void initData() {
    adapter = new MyAdapter();
    gv.setAdapter(adapter);
}

private class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return icons.length;
    }

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

        View view = View.inflate(getBaseContext(), R.layout.item_gridview, null);
        ImageView iv = (ImageView) findViewById(R.id.iv_item_gridview_icon);
        TextView tv = (TextView) findViewById(R.id.tv_item_gridview_name);
        iv.setImageResource(icons[position]);
        tv.setText(names[position]);
        return view;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
}

private void initView() {
    setContentView(R.layout.activity_home);
    gv = (GridView) findViewById(R.id.gv_home_gridview);
}

}

上面的代码我是按照视频里敲的,但是在 iv.setImageResource(icons[position]);这里报空指针异常,我该怎么改?

2个回答

danielinbiti
danielinbiti   Ds   Rxr 2016.05.06 17:24
已采纳
 View view = View.inflate(getBaseContext(), R.layout.item_gridview, null);
        ImageView iv = (ImageView) findViewById(R.id.iv_item_gridview_icon);
        TextView tv = (TextView) findViewById(R.id.tv_item_gridview_name);
    改成
    View view = View.inflate(getBaseContext(), R.layout.item_gridview, null);
        ImageView iv = (ImageView)view.findViewById(R.id.iv_item_gridview_icon);
        TextView tv = (TextView) view.findViewById(R.id.tv_item_gridview_name);
luofei987
luofei987   2016.05.06 17:31

太粗心了.多谢danielinbiti

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!