我正在尝试创建自定义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);
}
}
}
RegistrationInfo.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)
答案 0 :(得分:3)
你的getView中不能返回null。改变
return v;
}
return null;
到
}
return convertView;
您在getView
中返回的视图是您希望在GridView
/ ListView
中显示的 leaf 之一。系统必须测量和绘制它。
答案 1 :(得分:0)
在getView()
内,您可能会返回null
。你总是需要在这里返回一个视图。
更改声明:
return null;
到
return v;
答案 2 :(得分:0)
尝试以下代码:
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;
}
}