我有一个带有两个按钮的主活动类。按下的按钮将int []和int发送到片段活动,该活动由该活动中的特定片段访问。尝试将活动中的意图发送到片段活动,并从片段中检索片段活动中的包时,我得到了nullpointer异常。我知道这个解释听起来很混乱,这是我的代码:
从我的主要活动:
vessel.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//System.out.println(vessel.getText().toString()+" pushed");
int id = j;
datasource.openRead();
trips = datasource.tripsWithVesselId(id);
datasource.close();
Intent frags = new Intent(MainActivity.this, FragmentMenu.class);
frags.putExtra("trips", trips);
frags.putExtra("id", id);
startActivity(frags);
}
});
来自FragmentMenu:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag_menu);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
if(savedInstanceState != null)
{
TripReportsFragment frag = new TripReportsFragment();
frag.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();
}
}
来自我的片段类:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = getArguments();
if (bundle != null) {
trips = getArguments().getIntArray("trips");
vessel = getArguments().getInt("id");
}
System.out.println(vessel+" vessel id from mainactivity");
View rootView = inflater.inflate(R.layout.fragment_trip,
container, false);
Button newTrip = (Button) rootView.findViewById(R.id.newTrip);
Button pastTrips = (Button) rootView.findViewById(R.id.pastTrip);
newTrip.setOnClickListener(this);
pastTrips.setOnClickListener(this);
return rootView;
}
我的船只ID从片段类返回0,所以我知道该包是空的。我对片段非常不熟悉,所以任何见解都会非常有用,谢谢!
修改
我已经更改了要在SectionPagerAdapter中更改的代码,它现在看起来像这样:
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch (position) {
case 0:
TripReportsFragment frag = new TripReportsFragment();
frag.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();
return frag;
case 1:
return new InactivityReportFragment();
case 2:
return new SpeciesFragment();
}
return null;
}
现在我收到一个新错误:
01-10 14:26:04.915:E / AndroidRuntime(2538):java.lang.IllegalStateException:无法更改片段的容器ID TripReportsFragment {b1da3798 id = 0x1020002 android:switcher:2131230762:0}:是16908290现在2131230762
对此错误的看法?
答案 0 :(得分:0)
根据您的展示,您只在此设置参数:
if(savedInstanceState != null)
{
TripReportsFragment frag = new TripReportsFragment();
frag.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();
}
savedInstanceState仅在配置更改时为非null,因此最初您的片段将没有设置参数。您的片段是否从布局文件初始化? - 它是在mSectionsPagerAdapter中创建的吗?如果是这样,你可能不会在那里调用setArguments吗?
答案 1 :(得分:0)
您需要删除:
getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();
从:
case 0:
TripReportsFragment frag = new TripReportsFragment();
frag.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag).commit();
return frag;
成为:
case 0:
TripReportsFragment frag = new TripReportsFragment();
frag.setArguments(getIntent().getExtras());
return frag;