Ich habe versucht, Ansichten des Fragments aus Aktivität zu aktualisieren. Und ich bekomme nullpointerException.Zugriff auf die Ansicht außerhalb onCreateView() fehlgeschlagen
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());
}
}
Ich rufe public void setSummary (MyData myData) von meinem FragmentActivity die Daten über Fragment zu aktualisieren.
Hier bekomme ich null Zeiger Ausnahme wie unten.
java.lang.NullPointerException: Versuch auf einem Null-Objektreferenz virtuelle Methode 'android.view.View android.view.View.findViewById (int)' aufrufen
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);
}
}
Können Sie zeigen, wie genau Sie in Ihrer Aktivität auf die Instanz von MyFragment verweisen? – user35603
Es ist nicht klar, wie Sie das Fragment zur Aktivität hinzugefügt haben. Hast du das im XML getan? –
ja ich habe xmls verwendet – KCRaju