私はAndroidが初めてです。プログラムの実行罰金の他の部分エラー:java.lang.NullPointerException:ヌルオブジェクト参照で仮想メソッドを呼び出そうとしました
Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewTreeObserver android.widget.GridView.getViewTreeObserver()' on a null object reference
が、私はCalendarFragment
をクリックすると、アプリがクラッシュし、私はこのエラーを取得する:そして、私はこのエラーを取得しておきます。
お願いします。ありがとう。
CalendarFragment
のコード:
package app.pal.study.samplestudy;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import static android.app.Activity.RESULT_OK;
import app.pal.study.samplestudy.monthgrid.GridItem;
import app.pal.study.samplestudy.monthgrid.MonthDisplayAdapter;
public class CalendarFragment extends Fragment {
private static final int REQUEST_CODE = 1;
private Calendar calendar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar, container, false);
return rootView;
}
@Override
public void onResume() {
super.onResume();
if (calendar == null) {
calendar = CalUtil.getCalendar(new Date());
}
refresh();
}
private void refresh() {
final GridView calendarView = (GridView) getView().findViewById(R.id.calendar_grid_view);
setTitle();
initListAdapter(calendarView);
calendarView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
GridItem item = (GridItem) parent.getAdapter().getItem(position);
if (item != null) {
startListActivity(item);
}
}
});
}
private void startListActivity(GridItem item) {
calendar.set(Calendar.DATE, item.getDay());
Intent intent = new Intent(getActivity(), EventListActivity.class);
intent.putExtra(Constants.DATE_KEY, calendar.getTime());
startActivityForResult(intent, REQUEST_CODE);
}
public void loadNextMonth() {
calendar.setTime(CalUtil.addMonth(calendar.getTime(), 1));
refresh();
}
public void loadLastMonth() {
calendar.setTime(CalUtil.subtractMonth(calendar.getTime(), 1));
refresh();
}
public void showAllEvents() {
Intent intent = new Intent(getActivity(), AllEventsActivity.class);
intent.putExtra(Constants.DATE_KEY, calendar.getTime());
startActivityForResult(intent, REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
calendar.setTime((Date) data.getExtras().get(Constants.DATE_KEY));
}
}
private void setTitle() {
getActivity().setTitle(CalUtil.dateToFormattedString(calendar.getTime(), "MMMM, yyyy"));
}
private void initListAdapter(final GridView calendarView) {
calendarView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
calendarView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
calendarView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
CalendarEventDataSource dataSource = new CalendarEventDataSource(getActivity());
dataSource.openReadOnlyDB();
List<GridItem> gridList = CalUtil.getGridList(calendar.getTime(),
dataSource.getDaysWithEventsForMonth(calendar.getTime()));
dataSource.close();
calendarView.setAdapter(new MonthDisplayAdapter(
gridList, calendarView.getMeasuredHeight())
);
}
});
}
}
チェックcalendar_grid_viewは、同じIDを使用してfragment_calendarレイアウトで働いていた –
@Pravin Fofariyaのおかげで可能です – PersianBlue