Error in the Fragment

Asked

Viewed 73 times

0

I’m trying to make a calendar for birthdays, but on Fragment where I put the calendar is giving an error that I do not know how to fix.

Error

06-02 01:17:33.346 15106-15106/com.example.educador.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.educador.myapplication, PID: 15106
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.sundeepk.compactcalendarview.CompactCalendarView.setUseThreeLetterAbbreviation(boolean)' on a null object reference
    at com.example.educador.myapplication.Aniversarios.onCreateView(Aniversarios.java:49)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Code of Fragment:

public class Aniversarios extends Fragment {

    CompactCalendarView compactCalendar;
    private SimpleDateFormat dateFormatMonth = new SimpleDateFormat("MMMM- yyyy", Locale.getDefault());


    public Aniversarios() {
        // Required empty public constructor
    }


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

        //getActivity().setContentView(R.layout.fragment_aniversarios);
        // Inflate the layout for this fragment

        final ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setTitle(null);

        compactCalendar = getActivity().findViewById(R.id.compactcalendar_view);
        compactCalendar.setUseThreeLetterAbbreviation(true);

        //Set an event for Teachers' Professional Day 2016 which is 21st of October

        Event ev1 = new Event(Color.RED, 1477040400000L, "Teachers' Professional Day");
        compactCalendar.addEvent(ev1);

        compactCalendar.setListener(new CompactCalendarView.CompactCalendarViewListener() {
            @Override
            public void onDayClick(Date dateClicked) {
                Context context = getActivity().getApplicationContext();

                if (dateClicked.toString().compareTo("Fri Oct 21 00:00:00 AST 2016") == 0) {
                    Toast.makeText(context, "Teachers' Professional Day", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(context, "No Events Planned for that day", Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onMonthScroll(Date firstDayOfNewMonth) {
                actionBar.setTitle(dateFormatMonth.format(firstDayOfNewMonth));
            }

        });



        return inflater.inflate(R.layout.fragment_aniversarios, container, false);
    }

}
  • 1

    The problem is probably in Main2activity.java., line 153. You are trying to use the method isDrawerOpen of a null element.

  • What’s next on line 153 on Main2activity.java. @Override
 public void onBackPressed() {
 DrawerLayout drawer = findViewById(R.id.drawer_layout);
 if (drawer.isDrawerOpen(GravityCompat.START)) {
 drawer.closeDrawer(GravityCompat.START);
 } else {
 super.onBackPressed();
 }
 }

  • findViewById is returning null for the element R.id.drawer_layout does not exist in the layout (XML).

  • Sorry I will reissue the error was another had done editing and n had put again...

  • 1

    The same thing. The excerpt getActivity().findViewById(R.id.compactcalendar_view) is not returning an object (probably does not exist in XML). That makes that variable compactCalendar receive a value null and when trying to access a method (setUseThreeLetterAbbreviation, for example) error occurs (...) on a null object reference

  • If you add the code System.out.println( compactCalendar ), you will see that the result, in the logcat, will be null.

  • the XML there is yes. <com.github.sundeepk.compactcalendarview.CompactCalendarView xmlns:app="http://schemas.android.com/apk/res-auto"&#xA; android:id="@+id/compactcalendar_view"&#xA;...&#xA;/>

  • The bizarre q when placed performs although it does not rotate perfectly but rotates 99%> public View onCreateView(LayoutInflater inflater, ViewGroup container,&#xA; Bundle savedInstanceState) {&#xA;&#xA; getActivity().setContentView(R.layout.fragment_aniversarios);

  • That one XML within the layout leading?

  • is inside the layout from Fragment where I want it to appear

  • So there’s no point in using getActivity().findViewById(). https://hastebin.com/litegokati.java

  • what suggestion ? q to use ?

  • @Valdeirpsr really happened as you said... 06-02 02:43:31.877 18996-18996/com.example.educador.myapplication I/System.out: null

  • code xml https://hastebin.com/dalowonezo.xml

  • I found out!!!!!!!

  • Implemented solution >> https://hastebin.com/rehaveveji.java

  • found the solution here>>> http://helpdev.com.br/2017/11/30/android-como-pega-uma-view-da-activity-dentro-de-um-fragment-getactivity-findviewbyid-returns-null-called-from-fragment-ctivitycreated/

  • Thanks @Valdeirpsr for the moral and helped me mt ^^

Show 13 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.