我要在我的片段上放一些(测试)listview,但应用程序立即崩溃了。这是代码。
public class ListPassword extends Fragment {
private String accountID , masterUsername;
private ListView lv;
private TextView masterUsernameTXT;
private String[][] showingData;
String[] title = { "a" , "b" , "c" , "d" , "e" };
String[] description = { "aaa" , "bbb" , "ccc" , "ddd" , "eee" };
String[] updateDate = { "000" , "111" , "222" , "333" , "444" };
int recordRow = 0;
public static ListPassword newInstance() {
ListPassword fragment = new ListPassword();
return fragment;
}
public ListPassword() { }
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_listpassword, container, false);
String[] title = { "a" , "b" , "c" , "d" , "e" };
String[] description = { "aaa" , "bbb" , "ccc" , "ddd" , "eee" };
String[] updateDate = { "000" , "111" , "222" , "333" , "444" };
lv = (ListView)rootView.findViewById(R.id.listview_password);
SpinnerAdapt adapter = new SpinnerAdapt(getActivity(),title, description, updateDate);
lv.setAdapter(adapter);
return rootView;
}
我尝试通过阅读logcat来解决它,并发现以下错误。
致命的例外:主要 处理:com.example.new_ang_ja_kai_la.masterpassword,PID:21591 java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setText(java.lang.CharSequence中)'在null 对象参考 在com.example.new_ang_ja_kai_la.masterpassword.SpinnerAdapt.getCustomView(SpinnerAdapt.java:63)
并且SpinnerAdapt如下(我从我的朋友那里得到了这个)
public class SpinnerAdapt extends BaseAdapter {
private Activity activity;
private String[] title;
private String[] description;
private String[] updateDate;
//
public SpinnerAdapt(Activity activity, String[] title, String[] description, String[] updateDate) {
this.activity = activity;
this.title = title;
this.description = description;
this.updateDate = updateDate;
}
@Override
public int getCount() {
return title.length;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
return getCustomView(i, view, viewGroup);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
holder holder;
if (convertView == null) {
// LayoutInflater inflater = (activity).getLayoutInflater();
LayoutInflater inflater = (LayoutInflater)activity.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_listview_storedpassword,null);
holder = new holder(convertView);
convertView.setTag(holder);
} else {
holder = (holder) convertView.getTag();
}
holder.rowTitle.setText(title[position]);
holder.rowDescription.setText(description[position]);
holder.rowUpdateDate.setText(updateDate[position]);
return convertView;
}
public class holder{
private TextView rowTitle;
private TextView rowDescription;
private TextView rowUpdateDate;
public holder(View v){
TextView rowTitle= (TextView) v.findViewById(R.id.passwordTitle);
TextView rowDescription = (TextView) v.findViewById(R.id.passwordDescription);
TextView rowUpdateDate = (TextView) v.findViewById(R.id.passwordDescription);
}
}
}
我调试了这个。它说......
错误发生在" lv = (ListView的)rootView.findViewById(R.id.listview_password);"
它表明,当调试完成时,lv的值为null。
我不知道如何解决它,因为我已经检查过R.id.listview_password是正确的引用(通过ctrl +点击它)所以,我可以把指针放好。
我已经检查了解决方案,但没有什么可以帮助我。
答案 0 :(得分:0)
您可以在错误消息中看到您的应用在SpinnerAdapter类的第63行崩溃。所以请发布这个课程的代码,以便我们为您提供帮助。
编辑: 在没有持有人的情况下尝试:
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
convertView = inflater.inflate(R.layout.item_listview_storedpassword, parent, false);
}
TextView rowTitle= (TextView) convertView.findViewById(R.id.passwordTitle);
TextView rowDescription = (TextView) convertView.findViewById(R.id.passwordDescription);
TextView rowUpdateDate = (TextView) convertView.findViewById(R.id.passwordDescription);
if(rowTitle != null) rowTitle.setText(title[position]);
if(rowDescription != null) rowDescription.setText(description[position]);
if(rowUpdateDate != null) rowUpdateDate.setText(updateDate[position]);
return convertView;