当我尝试在片段之间传递数据时,应用程序崩溃。我该如何解决?

时间:2017-04-02 12:56:04

标签: android android-fragments

我的应用程序,我有2个片段。 fragmentCal,一旦应用程序启动,就会从MainActivity调用 - 包含CalenderViewbutton。用户可以在日历上选择日期,并将其保存在数组date[]中。然后,用户点击button,这会导致fragmentCal调用fragmentDiary,并使用捆绑包将date[]的内容传递给片段。 fragmentDiary然后使用getArguments()接收此数据。我还没有完成fragmentDiary的代码 - 我只是在测试第一个片段是否正常运行。

然而,应用程序一启动就会崩溃。

日志:

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

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 个答案:

没有答案