我试图从activity更新片段的视图。我得到nullpointerException。
public class MyFragment extends Fragment {
TextView tv_date, tv_memo_title;
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_day_summary, container, false);
tv_date = (TextView) view.findViewById(R.id.tv_date);
tv_memo_title = (TextView) view.findViewById(R.id.tv_memo_title);
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy");
//get current date time with Date()
Date date = new Date();
day = dateFormat.format(date);
setSummary(day);
return view;
}
public void setSummary(String day) {
tv_date.setText(this.day);
tv_memo_title.setText(äbcd");
}
public void setSummary(MyData myData) {
tv_date.setText(""+myData.getDate());
tv_memo_title.setText(""+myData.getDate());
}
}
我从FragmentActivity调用 public void setSummary(MyData myData)来更新片段上的数据。
这里我得到如下所示的空指针异常。
java.lang.NullPointerException:尝试调用虚拟方法' android.view.View android.view.View.findViewById(int)'在空对象引用上
public class SummaryActivity extends FragmentActivity {
public View onCreateView(String name, @NonNull Context context, @NonNull AttributeSet attrs) {
return super.onCreateView(name, context, attrs);
}
/**
* Dispatch to Fragment.onCreateOptionsMenu().
*
* @param featureId
* @param menu
*/
@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
return super.onCreatePanelMenu(featureId, menu);
}
Calendar cal = Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.activity_summary);
cal.set(Calendar.YEAR, 2016);
cal.set(Calendar.MONTH, 02);
cal.set(Calendar.DAY_OF_MONTH, 01);
// Locate the viewpager in activity_main.xml
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
String[] titles = {mContext.getString(R.string.TITLE_SUMMARY), mContext.getString(R.string.SUMMARY_ITEM_1), mContext.getString(R.string.SUMMARY_ITEM_2), mContext.getString(R.string.SUMMARY_ITEM_3), mContext.getString(R.string.SUMMARY_ITEM_4)};
// Set the SummaryViewPagerAdapter into ViewPager
viewPager.setAdapter(new SummaryViewPagerAdapter(getSupportFragmentManager(), mContext, titles));
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) - 24);
String prevDay = (cal.get(Calendar.MONTH) + 1) + "/" + (cal.get(Calendar.DAY_OF_MONTH)) + "/" + cal.get(Calendar.YEAR);
int topFragment = getSupportFragmentManager().getFragments().size() - 2;
if (getSupportFragmentManager().getFragments().get(topFragment) instanceof DaySummaryFragment) {
myData = DBWrapper.getAllData(prevDay, mContext);
myFragment.setSummary(myData );
}
break;
case KeyEvent.KEYCODE_VOLUME_UP:
cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) + 24);
String nextDay = (cal.get(Calendar.MONTH) + 1) + "/" + (cal.get(Calendar.DAY_OF_MONTH)) + "/" + cal.get(Calendar.YEAR);
topFragment = getSupportFragmentManager().getFragments().size() - 2;
if (getSupportFragmentManager().getFragments().get(topFragment) instanceof DaySummaryFragment) {
myData = DBWrapper.getAllData(prevDay, mContext);
myFragment.setSummary(myData );
} break;
}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:0)
当你给视图充气时,你需要做什么
View view = inflater.inflate(R.layout.fragment_day_summary, container, false);
return view;
这样你得到Null指针Exseption
答案 1 :(得分:0)
在创建setSummary
视图之前调用Fragment
。您是使用Activity
的{{1}}方法调用它吗?