我正在使用BaseAdapter创建列表视图,我想按下添加按钮时动态添加列表项。但是现在我得到了这个空指针异常。
07-02 06:40:41.650: E/AndroidRuntime(1940): FATAL EXCEPTION: main
07-02 06:40:41.650: E/AndroidRuntime(1940): java.lang.NullPointerException
07-02 06:40:41.650: E/AndroidRuntime(1940): at com.ui.activities.AddHorseCompetitionResult$1.onClick(AddHorseCompetitionResult.java:61)
07-02 06:40:41.650: E/AndroidRuntime(1940): at android.view.View.performClick(View.java:4204)
07-02 06:40:41.650: E/AndroidRuntime(1940): at android.view.View$PerformClick.run(View.java:17355)
07-02 06:40:41.650: E/AndroidRuntime(1940): at android.os.Handler.handleCallback(Handler.java:725)
07-02 06:40:41.650: E/AndroidRuntime(1940): at android.os.Handler.dispatchMessage(Handler.java:92)
07-02 06:40:41.650: E/AndroidRuntime(1940): at android.os.Looper.loop(Looper.java:137)
07-02 06:40:41.650: E/AndroidRuntime(1940): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-02 06:40:41.650: E/AndroidRuntime(1940): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 06:40:41.650: E/AndroidRuntime(1940): at java.lang.reflect.Method.invoke(Method.java:511)
07-02 06:40:41.650: E/AndroidRuntime(1940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-02 06:40:41.650: E/AndroidRuntime(1940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-02 06:40:41.650: E/AndroidRuntime(1940): at dalvik.system.NativeStart.main(Native Method)
这是我的java类
public class AddCompetitionResult extends Fragment {
private CompetitionResultListAdapter competitionResultListAdapter;
private DataManager datamanager;
ArrayList<Result> result;
ListView listView;
private Horse horse;
private Button addResults;
int clickCounter=0;
int resultId = 1;
public AddHorseCompetitionResult(Horse horse) {
this.horse = horse;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_horse_competition_result,
null);
addResults = (Button) view.findViewById(R.id.btn_add_competition_result);
listView = (ListView) view.findViewById(R.id.data_list);
// result = datamanager.create(getActivity());
competitionResultListAdapter = new CompetitionResultListAdapter(result, getActivity());
View footerView = ((LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.competition_result_listview_footer, null, false);
listView.addFooterView(footerView);
listView.setAdapter(competitionResultListAdapter);
addResults.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.add(new Result("resultId"));
competitionResultListAdapter.notifyDataSetChanged();
resultId++;
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (competitionResultListAdapter.getCount() == position) {
MainActivity mainActivity = MainActivity.getInstance();
mainActivity.setFragment(new AddHorseDetailedInfo(horse),
"Add horse more info");
// datamanager.createResult(result);
} else {
// ContactGroup cg = (ContactGroup)
// competitionResultListAdapter.getItem(position);
// MainActivity mainActivity = MainActivity.getInstance();
// mainActivity.setFragment(new
// ContactListView(cg.getGroupId()), "contacts");
}
}
});
return view;
}
}
适配器
public class CompetitionResultListAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<Result> result;
DataManager datamanager;
public CompetitionResultListAdapter( ArrayList<Result> result, Activity activity){
this.result = result;
this.activity = activity;
}
@Override
public int getCount() {
return result!=null ? result.size() : 0;
}
@Override
public Object getItem(int position) {
return result.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
// inflate the layout
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item_competition_result, parent, false);
}
EditText listItemDate = (EditText) convertView.findViewById(R.id.date);
EditText listItemPlace = (EditText) convertView.findViewById(R.id.place);
EditText listItemComment = (EditText) convertView.findViewById(R.id.comment);
result.get(position).setDate(listItemDate.getText().toString());
result.get(position).setPlace(listItemPlace.getText().toString());
result.get(position).setComment(listItemComment.getText().toString());
return convertView;
}
public void refreshResultList(ArrayList<Result> result){
this.result = result;
notifyDataSetChanged();
}
}
答案 0 :(得分:2)
您的result ArrayList
未初始化。所以在构造函数中初始化它 -
public class AddCompetitionResult extends Fragment {
private CompetitionResultListAdapter competitionResultListAdapter;
private DataManager datamanager;
ArrayList<Result> result;
ListView listView;
private Horse horse;
private Button addResults;
int clickCounter=0;
int resultId = 1;
public AddHorseCompetitionResult(ArrayList<Result> result,
Horse horse) {
this.horse = horse;
/*Initialize here if you
passed the value somewhere (1)*/
this.result = result;
}
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.
add_horse_competition_result, null);
/*Haven't passed the value to constructor*/
/*anywhere then do this (2)*/
result = new ArrayList<Result>();
result.add(new Result("resultId"));
return view;
}
}
注意:
(1)初始化的方式是将此值从其他地方传递给此构造函数。
(2)否则你可以按照方式进行2-
ArrayList<Result> result = new ArrayList<Result>();
答案 1 :(得分:0)
看起来result
永远不会被初始化bevor the line:
result.add(new Result("resultId"));
被召唤。
答案 2 :(得分:0)
在使用之前初始化arraylist。
ArrayList<Result> result = new ArrayList<Result>();