我有一个带有视图寻呼机和简单日期选择器的主要活动。
public static class MyPagerAdapter extends SmartFragmentStatePagerAdapter {
private static int NUM_ITEMS = 3;
private List<String> titleList = Arrays.asList("Week", "Month", "Year");
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
@Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return graph_week.newInstance(0, "Week");
case 1:
return graph_month.newInstance(1, "Month");
case 2:
return graph_year.newInstance(2, "Year");
default:
return null;
}
}
// Returns the page titles
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dateSetter = new DateSetter();
setContentView(R.layout.activity_graph);
ViewPager vpPager = (ViewPager) findViewById(R.id.vpPager);
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);
PagerTabStrip strip = (PagerTabStrip) findViewById(R.id.pager_header);
strip.setDrawFullUnderline(false);
vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// This method will be invoked when a new page becomes selected.
@Override
public void onPageSelected(int position) {
}
// This method will be invoked when the current page is scrolled
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
@Override
public void onPageScrollStateChanged(int state) {
}
});
fragment = (graph_month) adapterViewPager.getItem(1);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
当我选择日期时,我在我的片段中调用setCalendar来更改值。在createChart中我创建了一个简单的图表,异常在第一行出现,所以我切断了剩下的代码。
public static graph_month newInstance(int page, String title) {
graph_month fragment = new graph_month();
Bundle args = new Bundle();
args.putInt(PAGE, page);
args.putString(TITLE, title);
fragment.setArguments(args);
return fragment;
}
public graph_month() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
page = getArguments().getInt(PAGE);
title = getArguments().getString(TITLE);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_graph_month, container, false);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.createChart();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public void setCalendar(Calendar cal) {
this.cal = cal;
maxMonth = cal.getActualMaximum(cal.DAY_OF_MONTH);
this.createChart();
}
public void createChart() {
lineChartW= (LineChart) getView().findViewById(R.id.lineChartW);
但现在我在null对象引用上得到一个NullPointerException,而我的视图现在为null。 我不知道为什么,我想不出任何其他方式来更新我的活动中的片段。
完整错误代码:
处理:com.example.snyap.graphdemo,PID:1491 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String android.view.View.toString()' 在com.example.snyap.graphdemo.graphs.graph_month.createChart(graph_month.java:125) 在com.example.snyap.graphdemo.graphs.graph_month.setCalendar(graph_month.java:120) 在com.example.snyap.graphdemo.Graph.setDate(Graph.java:138) 在com.example.snyap.graphdemo.Graph.onDateSet(Graph.java:144) 在android.app.DatePickerDialog.onClick(DatePickerDialog.java:134) 在com.android.internal.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:165) 在android.os.Handler.dispatchMessage(Handler.java:111) 在android.os.Looper.loop(Looper.java:194) 在android.app.ActivityThread.main(ActivityThread.java:5549) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:964) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
答案 0 :(得分:0)
这是一种正确的方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_graph_month, container, false);
lineChartW= (LineChart) view.findViewById(R.id.lineChartW);
return view;
}
没有必要覆盖onViewCreated
这样的事情。您可以在充气后访问该视图。您可以阅读更多关于此here的内容。