null reference on getArguments - java

I have a MainActivity (extends Activity) and the a FooFragment (extends Fragment).
Activity uses newFooInstance method to instantiate FooFragment and set some arguments to it:
public static FooFragment newInstance(String label) {
FooFragment fooFragment = new FooFragment();
Bundle args = new Bundle();
args.putString("fooLabel", label);
fooFragment.setArguments(args);
return fooFragment;
}
Now, inside FooFragments onCreate and onCreateView I do:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fooLabel = getArguments().getString("fooLabel"); // null error
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_balanse_period, container, false
);
TextView fooView = (TextView) rootView.findViewById(R.id.fooLabel);
fooView.setText(fooLabel);
return rootView;
}
And it produces:
Attempt to invoke virtual method java.lang.String android.os.Bundle.getString(java.lang.String, java.lang.String) on a null object reference*
Any idea what am I doing wrong?
EDIT:
Defining the main activity as:
public class MainActivity extends AppCompatActivity {
Then instantiated fooFragment as:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragments = new ArrayList<>();
fragments.add(newInstance("bar"));
// Instantiate a ViewPager and PagerAdapter
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new FooPagerAdapter(getSupportFragmentManager(), fragments);

I have found the root of evil.
Inside FooPagerAdapter the getItem was written wrong.
Wrong:
#Override
public Fragment getItem(int position) {
return new FooFragment();
}
Correct:
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}

Related

Working with fragment class

Hello i want to working with fragment class.
in this code what to write???
in the myDbHelper = new DataBaseHelper(???????);
public class Tozihat extends Fragment{
DataBaseHelper myDbHelper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tozihat, container, false);
myDbHelper = new DataBaseHelper();
return view;
}
}
Use getActivity(); as Context if it is Fragment
myDbHelper = new DataBaseHelper(getActivity());
You need Activity's Context. So in Fragment you have to get is using getActivity().
So pass it as follows
myDbHelper = new DataBaseHelper(getActivity());

Called method current selected fragment viewpager from parent activity?

Huy guys, I have problem with my App. I created viewpager with some fragment.
this my viewpager ini my parent activity
private int current_posisition_page ;
#Override
protected v
oid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
viewpager.setAdapter(new PagePagerAdapter(getSupportFragmentManager()));
viewpager.setCurrentItem(999);
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
// This method will be invoked when a new page becomes selected.
#Override
public void onPageSelected(int position) {
current_posisition_page = position;
}
// This method will be invoked when the current page is scrolled
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
.......
}
private class PagePagerAdapter extends FragmentPagerAdapter {
public PagePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
return PageFragment.newInstance(pos);
}
#Override
public int getCount() {
return 1000;
}
}
}
and this my fragment
public class PageFragment extends Fragment {
public static PageFragment newInstance(int page_index) {
PageFragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putString(Variabel.page_index, String.valueOf(page_index));
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
page_index = getArguments().getString(Variabel.page_index);
}
activity=(MainActivity)getActivity();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.item_page, container, false);
ButterKnife.bind(this, v);
return v;
}
public void setSelectedAyat(String aya_id, String sura_name) {
surat.setText("Surat " + sura_name);
ayat.setText("Ayat " + aya_id);
}
}
Now I called method current selected fragment viewpager from parent activity:
PageFragment page = (PageFragment) adapter.getItem(current_posisition_page);
page.setSelectedAyat(aya_id, sura_name);
but I get Error :
java.lang.ClassCastException: java.lang.Integer cannot be cast to
com.ad.alquran.kemendag.fragment.PageFragment
in line : PageFragment page = (PageFragment) adapter.getItem(current_posisition_page);
so how to fix it ? thanks
Probably this line causing issue:
viewpager.setAdapter(new PagePagerAdapter(getSupportFragmentManager()));
Because adapter object for calling getItem method but in ViewPager setAdapter method passing different object of PagePagerAdapter class:
adapter=new PagePagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
and also make sure calling getItem method after setting adapter for ViewPager.
EDIT:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null
object reference
Because not initializing surat and ayat. do it as:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.item_page, container, false);
//...
surat=(TextView)v. findViewById(R.id.<surat_id_in_xml>);
ayat=(TextView)v. findViewById(R.id.<ayat_id_in_xml>);
return v;
}

Unable to call Fragment's Method from Parent View

I am trying to call a Fragment's public Method from its Parent View and I followed this, this and these questions on Stack. However the method I need is not avilable when I try to call it from the Parent view.
Here's my code:
Fragment:
public class FirstTabView extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_first_tab_view, container, false);
return rootView;
}
//The method I need to call
public void PostStuff(String caption){
}
}
Parent View: (Which contains the Main View Pager)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_view);
pager = (ViewPager) findViewById(R.id.mainpager);
mAdapter = new HomePagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
pager.setOffscreenPageLimit(4);
//Getting the Fragment's Tag. o is the Pager's Fragment index. i.e.: The first fragment
String fragment_tag = "android:switcher:" + pager.getId() + ":" + 0;
FirstTabView fragment = (FirstTabView) getSupportFragmentManager().findFragmentByTag(fragment_tag);
fragment. <This is where I need to call the method and pass the parameter> ();
//The method however doesn't appear when I try to reference it
}
Am I calling the Fragment's method correctly from my Parent View? What am I doing wrong?
EDIT
This is the adapter that I used to push the Fragments:
public class HomePagerAdapter extends FragmentPagerAdapter {
public HomePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FirstTabView();
case 1:
return new SecondTabView();
case 2:
return new ThirdTabView();
case 3:
return new FourthTabView();
case 4:
return new FifthTabView();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 5;
}
}
You can save a reference of the FirstTabView to your MainActivity, then call the method of the fragment.
FirstTabView
public class FirstTabView extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_first_tab_view, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//set this fragment to your MainActivity
MainActivity mainActivity = (MainActivity)getActivity();
mainActivity.setFirstTabView(this);
}
//The method I need to call
public void PostStuff(String caption){
}
}
MainActivity
private FirstTabView firstTabView;
#Override
public void onCreate(Bundle bundle) {
........
if(firstTabView != null) {
firstTabView.PostStuff("Hello World");
}
}
public void setFirstTabView(FirstTabView firstTabView){
this.firstTabView = firstTabView
}

How to change views in ViewPager Fragment

I have a ViewPager with fragments in my activity, each page has a textview, I want to change the text of the textview from the onCreate method in the activity that contains the viewpager. How should I go about doing that?
below is the code that would return a null object exception:
Fragment:
public class MyFragment extends Fragment {
private TextView textView;
public static MyFragment getInstance(){
return new MyFragment();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
textView = (TextView) rootView.findViewById(R.id.textView);
return rootView;
}
public void setText(String text){
textView.setText(text);
}
}
Adapter:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return MyFragment.getInstance();
}
#Override
public int getCount() {
return 3;
}
}
Activity onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
MyFragment fragment = (MyFragment) adapter.getItem(0);
fragment.setText("Altered"); // and this returns null pointer exception
}
Thank you!
Most FragmentPagerAdapters will have an addFragment method and will keep track various fragments that are added to it via an internal object:
public class MyPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
As it stands, your implementation of getItem returns a new instance of MyFragment every time it is called. As such, MyFragment fragment = (MyFragment) adapter.getItem(0); is a new instance that will never be loaded into the ViewPager, will never be inflated, and hence has a null textView reference.
The other issue is that within the onCreate of your activity, the fragments have not yet been inflated, so the textView will be null unless you delay the call:
final MyFragment fragment = (MyFragment) mPagerAdapter.getItem(0);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
fragment.setText("Altered");
}
}, 100);
But really, why not just set it to the correct value when it is initialized?
Try This
Fragment:
public class MyFragment extends Fragment {
private TextView textView;
private String Text;
public MyFragment (String Text){
this.Text=Text;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
textView = (TextView) rootView.findViewById(R.id.textView);
textView.setText(Text);
return rootView;
}
}
Adapter :
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new MyFragment("Altered");
}
#Override
public int getCount() {
return 3;
}
}
Activity onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
}

my app crashes when I swipe from one fragment to another (error: canScrollHorizontally()' on a null object reference)

I've been trying to resolve this problem but with no result. My app is made up of 4 fragments, the second one with a recycler view. When I swipe from the first to the second my app instantly crashes. And in the logCat this error appears:
Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:2022)
Here's my code for the main activity:
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.app_bar);
if (toolbar != null) {
toolbar.setTitle("Quick!");
}
setSupportActionBar(toolbar);
fragmentList.add(HomeFragment.getInstance(0));
fragmentList.add(KartFragment.getInstance(1));
fragmentList.add(FavouriteFragment.getInstance(2));
fragmentList.add(ScannerFrag.getInstance(3));
pager = (ViewPager) findViewById(R.id.Pager);
pager.setAdapter(new myPagerAdapter(getSupportFragmentManager()));
tab = (SlidingTabLayout) findViewById(R.id.SlidingTab);
tab.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.accentColor);
}
});
tab.setViewPager(pager);
}
class myPagerAdapter extends FragmentPagerAdapter {
String[] tabs;
public myPagerAdapter(FragmentManager fm) {
super(fm);
tabs = getResources().getStringArray(R.array.tabs);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
public int getCount() {
return fragmentList.size();
}
}
Home fragment:
public class HomeFragment extends Fragment {
private Toolbar toolbar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout;
layout = inflater.inflate(R.layout.home_layout, container, false);
toolbar = (Toolbar) layout.findViewById(R.id.subToolbar);
toolbar.setTitle("Dove mangi oggi?");
return layout;
}
public static HomeFragment getInstance(int position) {
HomeFragment frag = new HomeFragment();
Bundle args = new Bundle();
args.putInt("position", position);
frag.setArguments(args);
return frag;
}
}
KartFragment:
public class KartFragment extends Fragment {
private Toolbar toolbar;
private ItemAdapter ad;
private RecyclerView rv;
private ArrayList list = new ArrayList<>();
private static final String SAVELIST = "SAVELIST";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View layout;
if (savedInstanceState != null) {
list = savedInstanceState.getParcelableArrayList(SAVELIST);
} else {
list = KartObjectClass.listBuilder(10);
}
layout = inflater.inflate(R.layout.fragment_layout, container, false);
toolbar = (Toolbar) layout.findViewById(R.id.subToolbar);
toolbar.setTitle("I tuoi ordini:");
rv = (RecyclerView) layout.findViewById(R.id.list);
rv.setLayoutManager(new LinearLayoutManager(getActivity()));
ad = new ItemAdapter(list);
rv.setHasFixedSize(true);
rv.setAdapter(ad);
return layout;
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putParcelableArrayList(SAVELIST, list);
super.onSaveInstanceState(outState);
}
public static KartFragment getInstance(int position) {
KartFragment frag = new KartFragment();
Bundle args = new Bundle();
args.putInt("position", position);
frag.setArguments(args);
return frag;
}
}
Favourite fragment:
public class FavouriteFragment extends Fragment {
private Toolbar toolbar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = null;
layout = inflater.inflate(R.layout.scanner_layout, container, false);
toolbar = (Toolbar) layout.findViewById(R.id.subToolbar);
toolbar.setTitle("I tuoi preferiti:");
return layout;
}
public static FavouriteFragment getInstance(int position) {
FavouriteFragment frag = new FavouriteFragment();
Bundle args = new Bundle();
args.putInt("position", position);
frag.setArguments(args);
return frag;
}
}
Try to set the LayoutManager before you set the Adapter on the RecyclerView.
I solved the error, the problem was that I had set a recycler view in the homeFragment XML. As I forgot to have it, I didn't call it in the onCreateView method and the app was crashing because no adapter was set.

Categories

Resources