0

Im my app, I have 2 fragments. fragmentCal ,which is called from MainActivity as soon as the app starts- contains a CalenderView and a button. The user can select a date on the calender and this gets saved in the array date[] . Then the user clicks the button and this causes fragmentCal to call fragmentDiary and pass the contents of date[] to the fragment using a bundle. fragmentDiary then receives this data using getArguments() . I havent finished the code for fragmentDiary - Im just testing to see if the first fragment is working well right now.

However the app is crashing as soon as it starts.

The log:

java.lang.RuntimeException:  java.lang.NullPointerException: Attempt to 
invoke virtual method 'android.view.View  
android.view.View.findViewById(int)' on a null object reference

and

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first  

MainActivity.java

package com.example.nirvan.finaldiary;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fragmentCal frag=new fragmentCal();
    getSupportFragmentManager().beginTransaction().add(R.id.container,frag).commit();
    }


public void callFragmentDiary(int []date)
    {
    fragmentDiary frag=new fragmentDiary();

    Bundle bundle=new Bundle();
    bundle.putInt("day",date[0]);
    bundle.putInt("month",date[1]);
    bundle.putInt("year",date[2]);


    frag.setArguments(bundle);
    getSupportFragmentManager().beginTransaction().replace(R.id.container , frag).commit();
    }
}

fragmentCal.java

package com.example.nirvan.finaldiary;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CalendarView;




public class fragmentCal extends Fragment
{
int[] date;     //3 integers: 0-> day    1->month     2->year

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


    View parentHolder=inflater.inflate(R.layout.fragmentcal,container,false);
    //View parentHolder=super.onCreateView(inflater, container, savedInstanceState);
    Button button=(Button)parentHolder.findViewById(R.id.button);
    CalendarView calendarView=(CalendarView)parentHolder.findViewById(R.id.calenderView);

    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener()
    {
        @Override
        public void onSelectedDayChange(CalendarView calendarView, int i, int i1, int i2)
        {
            date[0]=i2;
            date[1]=i1;
            date[2]=i;
        }
    });

    button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            (new MainActivity()).callFragmentDiary(date);
        }
    });

    return parentHolder;
   }

public fragmentCal() {}
}

fragmentDiary.java

package com.example.nirvan.finaldiary;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;


public class fragmentDiary extends Fragment
{
int day,month,year;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
    View parentHolder =  inflater.inflate(R.layout.fragmentdiary,container,false);
    //=super.onCreateView(inflater, container, savedInstanceState);
    Toolbar toolbar = (Toolbar)parentHolder.findViewById(R.id.toolbar);
    ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

    Bundle bundle=getArguments();
    day=bundle.getInt("day");
    month=bundle.getInt("month");
    year=bundle.getInt("year");

    return parentHolder;
   }

   @Override
   public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
   {
    inflater.inflate(R.menu.diary_menu,menu);
    super.onCreateOptionsMenu(menu, inflater);
   }

public fragmentDiary() {}
}

0 Answers0