问题描述
- 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.oneR.drawable.twoR.drawable.three R.drawable.fourR.drawable.fiveR.drawable.six R.drawable.sevenR.drawable.eightR.drawable.nine};private String[] names = {""one""two""three""four""five""six""seven""eight""nine""};private GridView gv;private MyAdapter adapter; private AlertDialog dialog;@Overrideprotected 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.thisLostFindActivity.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]);这里报空指针异常我该怎么改?
解决方案
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);
解决方案二:
太粗心了.多谢danielinbiti