Android 中的自定义适配器
Posted
技术标签:
【中文标题】Android 中的自定义适配器【英文标题】:CustomAdapter in Android 【发布时间】:2015-03-31 14:01:52 【问题描述】:我正在尝试创建自定义 ListView,但是当我尝试创建 RegistrationInfo 对象时,我的应用程序崩溃了。 我收到 NULLPointerException。 谁能告诉我为什么?
SecondActivity.java
public class SecondActivity extends Activity implements OnItemLongClickListener
SharedPreferences pref;
CustomAdapter adapter;
ListView lv;
ArrayList<RegistrationInfo> registrationlist;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.showstoreddata);
lv = (ListView) findViewById(R.id.showdata);
File prefsdir = new File(getApplicationInfo().dataDir,"shared_prefs");
String[] list = prefsdir.list();
String[] preflist = new String[list.length];
for(int i=0;i<list.length;i++)
String preffile = list[i].substring(0, list[i].length()-4);
preflist[i]=preffile;
registrationlist = new ArrayList<RegistrationInfo>();
for(int index=0;index<preflist.length;index++)
SharedPreferences spref = getSharedPreferences(preflist[index], MODE_PRIVATE);
String firstName = spref.getString("firstName", "");
String lastName = spref.getString("lastName", "");
String email = spref.getString("email", "");
String contactno = spref.getString("contactno", "");
Toast.makeText(this, firstName+" "+lastName+" "+email+" "+contactno, Toast.LENGTH_LONG).show();
//Crashing due to the statement below
RegistrationInfo user = new RegistrationInfo(firstName, lastName, email, contactno);
Toast.makeText(this, "abcd", Toast.LENGTH_LONG).show();
registrationlist.add(user);
Toast.makeText(this, "a", Toast.LENGTH_LONG).show();
adapter = new CustomAdapter(registrationlist, this);
Toast.makeText(this, "b", Toast.LENGTH_LONG).show();
lv.setAdapter(adapter);
注册信息.java
public class RegistrationInfo
private String firstName,lastName,email,contactno;
public RegistrationInfo(String fName,String lName,String eid,String cno)
firstName = fName;
lastName = lName;
email = eid;
contactno = cno;
public String getFirstName()
return firstName;
public void setFirstName(String firstName)
this.firstName = firstName;
public String getLastName()
return lastName;
public void setLastName(String lastName)
this.lastName = lastName;
public String getEmail()
return email;
public void setEmail(String email)
this.email = email;
public String getContactno()
return contactno;
public void setContactno(String contactno)
this.contactno = contactno;
CustomAdapter.java
public class CustomAdapter extends BaseAdapter
Context context;
ArrayList<RegistrationInfo> registration;
public CustomAdapter(ArrayList list,Context c)
registration = list;
context = c;
@Override
public int getCount()
return registration.size();
@Override
public RegistrationInfo getItem(int pos)
return registration.get(pos);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int pos, View convertView, ViewGroup parent)
View v = convertView;
if(v==null)
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflator.inflate(R.layout.customlistviewlayout, null);
TextView name = (TextView) v.findViewById(R.id.name);
TextView contactno = (TextView) v.findViewById(R.id.contactno);
TextView email = (TextView) v.findViewById(R.id.email);
RegistrationInfo user = registration.get(pos);
//name.setText("Name : "+user.getFirstName()+" "+user.getLastName());
//email.setText("Email id : "+user.getEmail());
//contactno.setText("Contact No : "+user.getContactno());
return v;
return null;
Logcat
02-01 20:20:30.984: E/androidRuntime(12355): FATAL EXCEPTION: main
02-01 20:20:30.984: E/AndroidRuntime(12355): java.lang.NullPointerException
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.widget.AbsListView.obtainView(AbsListView.java:1581)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.widget.ListView.measureHeightOfChildren(ListView.java:1251)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.widget.ListView.onMeasure(ListView.java:1162)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.view.View.measure(View.java:8313)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.view.View.measure(View.java:8313)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.view.View.measure(View.java:8313)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.view.View.measure(View.java:8313)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.view.View.measure(View.java:8313)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.os.Handler.dispatchMessage(Handler.java:99)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.os.Looper.loop(Looper.java:130)
02-01 20:20:30.984: E/AndroidRuntime(12355): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-01 20:20:30.984: E/AndroidRuntime(12355): at java.lang.reflect.Method.invokeNative(Native Method)
02-01 20:20:30.984: E/AndroidRuntime(12355): at java.lang.reflect.Method.invoke(Method.java:507)
02-01 20:20:30.984: E/AndroidRuntime(12355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
02-01 20:20:30.984: E/AndroidRuntime(12355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
02-01 20:20:30.984: E/AndroidRuntime(12355): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
请发布您的 CustomAdapter 课程 你为什么不添加你的logcat? 【参考方案1】:您不能在 getView 中返回 null。改变
return v;
return null;
到
return convertView;
您在getView
中返回的视图是您希望在GridView
/ListView
中显示的叶子之一。系统必须测量和绘制它。
【讨论】:
【参考方案2】:试试下面的代码:
public class CustomAdapter extends BaseAdapter
Context context;
ArrayList<RegistrationInfo> registration;
public CustomAdapter(ArrayList list,Context c)
registration = list;
context = c;
@Override
public int getCount()
return registration.size();
@Override
public RegistrationInfo getItem(int pos)
return registration.get(pos);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int pos, View convertView, ViewGroup parent)
ViewHolder holder = null;
if (convertView == null)
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.customlistviewlayout, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.contactno = (TextView) convertView.findViewById(R.id.contactno);
holder.email = (TextView) convertView.findViewById(R.id.email);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
//write your code
return convertView;
private class ViewHolder
TextView name,contactno,email;
【讨论】:
【参考方案3】:在您的getView()
中,您可能会返回null
。你总是需要在这里返回一个视图。
更改语句:
return null;
到
return v;
【讨论】:
以上是关于Android 中的自定义适配器的主要内容,如果未能解决你的问题,请参考以下文章
ArrayIndexOutOfBoundsException 与 ListView 中的多个视图的自定义 Android 适配器
如何将最新数据附加到android中的自定义基本适配器列表视图?
android中自定义ArrayAdapter中的自定义getFilter