列表视图上的空指针异常
Null pointer exception on list view
我正在创建动态视图。我想添加视图以列出并删除它们 onActivityResult。
但是当我将视图添加到列表时,我在列表视图上收到 NullPointerException。
我应该在列表视图中的什么地方添加视图?
这是我的代码:
public class Mon extends Fragment {
private FrameLayout fab;
private EventTableHelper mDb;
private Intent i;
private ViewGroup dayplanView;
private int minutesFrom,minutesTo;
private List<EventData> events;
private List<View> list;
private EventData e;
private LayoutInflater inflater;
public boolean editMode;
private RelativeLayout container;
RelativeLayout parent;
View eventView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mon, container, false);
fab = (FrameLayout) view.findViewById(R.id.main_fab);
ImageButton imageButton = (ImageButton) view.findViewById(R.id.imgbtn_fab);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(), AddEventActivity.class);
editMode = false;
i.putExtra("EditMode", editMode);
startActivityForResult(i, 1);
}
});
dayplanView = (ViewGroup) view.findViewById(R.id.hoursRelativeLayout);
showEvents();
return view;
}
private void createEvent(LayoutInflater inflater, ViewGroup dayplanView, int fromMinutes, int toMinutes, String title,String location,final int id) {
eventView = inflater.inflate(R.layout.event_view, dayplanView, false);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();
container = (RelativeLayout) eventView.findViewById(R.id.container);
TextView tvTitle = (TextView) eventView.findViewById(R.id.textViewTitle);
if (tvTitle.getParent() != null)
((ViewGroup) tvTitle.getParent()).removeView(tvTitle);
if(location.equals(""))
{
tvTitle.setText("Event : " + title);
}
else
{
tvTitle.setText("Event : " + title + " (At : " + location +")");
}
int distance = (toMinutes - fromMinutes);
layoutParams.topMargin = dpToPixels(fromMinutes + 9);
layoutParams.height = dpToPixels(distance);
eventView.setLayoutParams(layoutParams);
dayplanView.addView(eventView);
container.addView(tvTitle);
eventView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(), AddEventActivity.class);
editMode = true;
i.putExtra("EditMode", editMode);
i.putExtra("id", id);
startActivityForResult(i, 1);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
removeView();
showEvents();
}
public void showEvents()
{
mDb = new EventTableHelper(getActivity());
events = mDb.getAllEvents("Mon");
for (EventData eventData : events) {
int id = eventData.getId();
String datefrom = eventData.getFromDate();
if (datefrom != null) {
String[] times = datefrom.substring(11, 16).split(":");
minutesFrom = Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
}
String title = eventData.getTitle();
String location = eventData.getLocation();
String dateTo = eventData.getToDate();
if (dateTo != null) {
//times = dateTo.substring(11,16).split(":");
String[] times1 = dateTo.substring(11, 16).split(":");
minutesTo = Integer.parseInt(times1[0]) * 60 + Integer.parseInt(times1[1]);
}
createEvent(inflater, dayplanView, minutesFrom, minutesTo, title, location, id);
id++;
list.add(eventView);
}
}
public void removeView()
{
for(int i=0; i<list.size(); i++)
{
dayplanView.removeView(eventView);
}
}
private int dpToPixels(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density);
}
}
在 list.add(事件视图)发生异常;
请帮忙..
您的列表从未初始化,当您返回结果时正在尝试将元素添加到空引用列表
解法:
在 onCreate
中做:
list = new ArrayList<View>();
我正在创建动态视图。我想添加视图以列出并删除它们 onActivityResult。
但是当我将视图添加到列表时,我在列表视图上收到 NullPointerException。
我应该在列表视图中的什么地方添加视图?
这是我的代码:
public class Mon extends Fragment {
private FrameLayout fab;
private EventTableHelper mDb;
private Intent i;
private ViewGroup dayplanView;
private int minutesFrom,minutesTo;
private List<EventData> events;
private List<View> list;
private EventData e;
private LayoutInflater inflater;
public boolean editMode;
private RelativeLayout container;
RelativeLayout parent;
View eventView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mon, container, false);
fab = (FrameLayout) view.findViewById(R.id.main_fab);
ImageButton imageButton = (ImageButton) view.findViewById(R.id.imgbtn_fab);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(), AddEventActivity.class);
editMode = false;
i.putExtra("EditMode", editMode);
startActivityForResult(i, 1);
}
});
dayplanView = (ViewGroup) view.findViewById(R.id.hoursRelativeLayout);
showEvents();
return view;
}
private void createEvent(LayoutInflater inflater, ViewGroup dayplanView, int fromMinutes, int toMinutes, String title,String location,final int id) {
eventView = inflater.inflate(R.layout.event_view, dayplanView, false);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();
container = (RelativeLayout) eventView.findViewById(R.id.container);
TextView tvTitle = (TextView) eventView.findViewById(R.id.textViewTitle);
if (tvTitle.getParent() != null)
((ViewGroup) tvTitle.getParent()).removeView(tvTitle);
if(location.equals(""))
{
tvTitle.setText("Event : " + title);
}
else
{
tvTitle.setText("Event : " + title + " (At : " + location +")");
}
int distance = (toMinutes - fromMinutes);
layoutParams.topMargin = dpToPixels(fromMinutes + 9);
layoutParams.height = dpToPixels(distance);
eventView.setLayoutParams(layoutParams);
dayplanView.addView(eventView);
container.addView(tvTitle);
eventView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(), AddEventActivity.class);
editMode = true;
i.putExtra("EditMode", editMode);
i.putExtra("id", id);
startActivityForResult(i, 1);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
removeView();
showEvents();
}
public void showEvents()
{
mDb = new EventTableHelper(getActivity());
events = mDb.getAllEvents("Mon");
for (EventData eventData : events) {
int id = eventData.getId();
String datefrom = eventData.getFromDate();
if (datefrom != null) {
String[] times = datefrom.substring(11, 16).split(":");
minutesFrom = Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
}
String title = eventData.getTitle();
String location = eventData.getLocation();
String dateTo = eventData.getToDate();
if (dateTo != null) {
//times = dateTo.substring(11,16).split(":");
String[] times1 = dateTo.substring(11, 16).split(":");
minutesTo = Integer.parseInt(times1[0]) * 60 + Integer.parseInt(times1[1]);
}
createEvent(inflater, dayplanView, minutesFrom, minutesTo, title, location, id);
id++;
list.add(eventView);
}
}
public void removeView()
{
for(int i=0; i<list.size(); i++)
{
dayplanView.removeView(eventView);
}
}
private int dpToPixels(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density);
}
}
在 list.add(事件视图)发生异常; 请帮忙..
您的列表从未初始化,当您返回结果时正在尝试将元素添加到空引用列表
解法:
在 onCreate
中做:
list = new ArrayList<View>();