我正在片段上创建自定义listView ..Attendance是一个带有可跳转标签的活动 我希望自定义列表出现在fragment_attendance_mark
上这个代码在一个活动中工作得很好......但是当我试图在一个可跳过的片段上做同样的事情时会出现一些问题......
04-21 19:21:45.859: W/dalvikvm(18052): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
04-21 19:21:45.879: E/AndroidRuntime(18052): FATAL EXCEPTION: main
04-21 19:21:45.879: E/AndroidRuntime(18052): java.lang.NullPointerException
04-21 19:21:45.879: E/AndroidRuntime(18052): at com.example.markitberry.AttendanceMarkFragment.displayListView(AttendanceMarkFragment.java:66)
04-21 19:21:45.879: E/AndroidRuntime(18052): at com.example.markitberry.AttendanceMarkFragment.onCreateView(AttendanceMarkFragment.java:35)
04-21 19:21:45.879: E/AndroidRuntime(18052): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-21 19:21:45.879: E/AndroidRuntime(18052): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
04-21 19:21:45.879: E/AndroidRuntime(18052): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-21 19:21:45.879: E/AndroidRuntime(18052): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-21 19:21:45.879: E/AndroidRuntime(18052): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
Fragment_attandance_mark.java
public class AttendanceMarkFragment extends Fragment {
MyCustomAdapter dataAdapter = null;
ListView listView ;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_attendance_mark, container, false);
listView = (ListView) rootView.findViewById(R.id.listView1);
//View rootView = inflater.inflate(R.layout.fragment_attendance_mark, container, false);
// Generate list View from ArrayList
displayListView();
checkButtonClick(rootView);
return rootView;
}
private void displayListView() {
// Array list of countries
ArrayList<AttendanceMarkListRow> stateList = new ArrayList<AttendanceMarkListRow>();
AttendanceMarkListRow _AttendanceMarkListRow = new AttendanceMarkListRow("Ravi", "Male", "7 Aug",4,false,2);
stateList.add(_AttendanceMarkListRow);
_AttendanceMarkListRow = new AttendanceMarkListRow("Ravi", "Male", "7 Aug",4,false,2);
stateList.add(_AttendanceMarkListRow);
_AttendanceMarkListRow = new AttendanceMarkListRow("Div", "Male", "4 Aug",2,false,2);
stateList.add(_AttendanceMarkListRow);
_AttendanceMarkListRow = new AttendanceMarkListRow("Hari", "Male", "1 Aug",1,false,2);
stateList.add(_AttendanceMarkListRow);
_AttendanceMarkListRow = new AttendanceMarkListRow("Suu", "Male", "3 Aug",5,false,2);
stateList.add(_AttendanceMarkListRow);
_AttendanceMarkListRow = new AttendanceMarkListRow("Dhaka", "Male", "5 Aug",0,false,2);
stateList.add(_AttendanceMarkListRow);
_AttendanceMarkListRow = new AttendanceMarkListRow("Chu", "Male", "8 Aug",4,false,2);
stateList.add(_AttendanceMarkListRow);
_AttendanceMarkListRow = new AttendanceMarkListRow("Nikita", "Female", "9 Aug",4,false,2);
stateList.add(_AttendanceMarkListRow);
_AttendanceMarkListRow = new AttendanceMarkListRow("Ravi Ranjan", "Male", "0 Aug",4,false,2);
stateList.add(_AttendanceMarkListRow);
// create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this.getActivity(), R.layout.list_row, stateList);
ListView listView = (ListView) getView().findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
AttendanceMarkListRow state = (AttendanceMarkListRow) parent.getItemAtPosition(position);
Toast.makeText(getActivity().getApplicationContext(),
"Clicked on : " + state.getName(), Toast.LENGTH_LONG)
.show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<AttendanceMarkListRow> {
private ArrayList<AttendanceMarkListRow> stateList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<AttendanceMarkListRow> stateList) {
super(context, textViewResourceId, stateList);
this.stateList = new ArrayList<AttendanceMarkListRow>();
this.stateList.addAll(stateList);
}
private class ViewHolder {
TextView gender;
TextView lastseen;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.gender = (TextView) convertView.findViewById(R.id.gender);
holder.lastseen = (TextView) convertView.findViewById(R.id.lastSeen);
holder.name = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
AttendanceMarkListRow _state = (AttendanceMarkListRow) cb.getTag();
Toast.makeText(
getActivity().getApplicationContext(),
"Checkbox: " + cb.getText() + " -> "
+ cb.isChecked(), Toast.LENGTH_LONG)
.show();
_state.setSelected(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
AttendanceMarkListRow state = stateList.get(position);
holder.gender.setText(state.getGender());
holder.lastseen.setText(state.getLastSeen());
holder.name.setText(state.getName());
holder.name.setChecked(state.isSelected());
holder.name.setTag(state);
return convertView;
}
}
private void checkButtonClick(View view) {
Button myButton = (Button) view.findViewById(R.id.butBdaySelectNext);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("Selected Countries are...\n");
ArrayList<AttendanceMarkListRow> stateList = dataAdapter.stateList;
for (int i = 0; i < stateList.size(); i++) {
AttendanceMarkListRow state = stateList.get(i);
if (state.isSelected()) {
responseText.append("\n" + state.getName());
}
}
Toast.makeText(getActivity().getApplicationContext(), responseText,
Toast.LENGTH_LONG).show();
}
});
}
}
答案 0 :(得分:2)
您从displayListView()
致电onCreateView()
,并在那里致电getView()
。现在,onCreateView()
尚未返回视图,因此getView()
返回null。而是将视图作为参数从onCreateView()
传递到displayListView()
。
答案 1 :(得分:1)
以下内容可能会导致NullPointerException
。
ListView listView = (ListView) getView().findViewById(R.id.listView1);
所以改为
ListView listView ;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_attendance_mark, container, false);
listView = (ListView) rootView.findViewById(R.id.listView1);
// Generate list View from ArrayList
displayListView();
checkButtonClick();
return rootView;
}
或者laalto建议
displayListView(rootView);
然后
private void displayListView(View View) {
ListView listView = (ListView) view.findViewById(R.id.listView1);
}
或
在onActivityCreated
ListView listView = (ListView) getView().findViewById(R.id.listView1);
编辑:
checkButtonClick(rootView);
然后
private void checkButtonClick(View view) {
Button myButton = (Button) view.findViewById(R.id.butBdaySelectNext);