2016-04-08 24 views
0

私は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()) 
      ); 
     } 
    }); 
} 
} 
+0

チェックcalendar_grid_viewは、同じIDを使用してfragment_calendarレイアウトで働いていた –

+0

@Pravin Fofariyaのおかげで可能です – PersianBlue

答えて

0
Try this 


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; 
View rootView; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    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)rootView.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()) 
      ); 
     } 
    }); 
} 
} 
+0

まだ動作していません – PersianBlue

+0

check id calendar_grid_viewは、同じIDのfragment_calendarレイアウトで使用できます –

0

(OncreateView内のカレンダーを宣言してください)

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    Calendar calendar; 
    View rootView = inflater.inflate(R.layout.fragment_calendar, container, false); 

    return rootView; 
} 
+0

まだ、同じエラーが発生していません。 – PersianBlue

関連する問題