我有一個活動,其中有一個片段,我必須從活動傳遞字符串數組,當我通過以下代碼執行此操作時出現空指針異常請幫助getArguments()返回null當從容器活動傳遞字符串數組活動到片段
Main2Activity.java
public class Main2Activity extends AppCompatActivity implements OnMapReadyCallback, AsyncResponse{
String[] check = {
"KFC bundle",
"Pizza Hut b",
"Pizza Point b"
} ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
newString = intent.getLongExtra("user_id", newString);
Bundle bundle = new Bundle();
bundle.putStringArray("name", check);
Log.i("okkkkk", "Name Put: " + check[1]);
Deals deals = new Deals();
deals.setArguments(bundle);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
try {
switch (position) {
case 0: {
MapsFragment mapsActivity=new MapsFragment();
return mapsActivity;
}
case 1: {
Deals deals = new Deals();
return deals;
}
}
}
catch (Exception e)
{
Log.i("okkk", "Exception 1 : " + e);
}
Deals.java`
public class Deals extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =LayoutInflater.from(getActivity()).inflate(R.layout.fragment_main2,null);
// bundle = this.getArguments();
String[] name = getArguments().getStringArray("name");
Log.i("okkkkk", "Name String: " + name);
// String[] deal = getArguments().getStringArray("deal");
ListView listview =(ListView)view.findViewById(R.id.DealsListView);
customList adapter = new customList(getActivity(), web, imageId,deal);
listview.setAdapter(adapter);
return view;
}`
java.lang.NullPointerException
at app.com.example.saeed.fypephaseone.Deals.onCreateView(Deals.java:42)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1235)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1083)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1609)
at android.view.View.measure(View.java:16842)
不要你錯過了'mSectionsPagerAdapter.add(交易);'?或者你怎麼稱呼它? –
@EduardoHerzer請檢查編輯過的帖子 – SFAH
您必須使用相同的實例設置參數。如果您使用交易的新實例,則不會擁有其他實例的參數。 –