I have a map activity. When I click on the info window of the marker an intent will send the id to my fragment. In my map activity :
map.setOnInfoWindowClickListener(
new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker m) {
Intent nextScreen = new Intent (Map.this,
PHDetail.class);
Bundle bundle = new Bundle();
bundle.putString("test", "id");
Callfor c = new Callfor();
c.setArguments(bundle);
startActivityForResult(nextScreen, 101);
}
});
In my fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_call_for, container, false);
TextView TXT = (TextView) rootView.findViewById(R.id.test);
Bundle test = getArguments();
if(test!=null){
String d = test.getString("test", " ");
TXT.setText(d);
}
return rootView;
}
The problem is the bundle is null. How can I fix this?
This is my PHDetail FragmentActivity class :
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.phdetail);
Intent intent = getIntent();
if (intent != null) {
String id = intent.getStringExtra("test");
}
// Initialization :
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs :
for (String tab_name : tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
UPDATE
On your onTabSelected you need to create your Fragment, set the bundle and then do the transaction like this.
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
Bundle bundle = new Bundle();
bundle.putString("id", id);
MyFragment fragment=new MyFragment();
fragment.setArguments(bundle);
ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragment_container,fragment);
ft.commit();
}
FIRST ANSWER
The right way to pass values to other Activities like a FragmentActivity it´s adding those values to the intent that´s going to start them.
map.setOnInfoWindowClickListener(
new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker m) {
Intent nextScreen = new Intent (Map.this, PHDetail.class);
intent.putExtra("test", "id");
startActivityForResult(nextScreen, 101);
}
});
Then on your FragmentActivity called PHDetail you should get the values from the Intent.
Intent intent = getIntent();
if (intent != null) {
String id = intent.getStringExtra("test");
}
After that when you load your Fragment on your container view on PHDetail, you should add the Bundle and then do the transaction.
Bundle bundle = new Bundle();
bundle.putString("test", "id");
Callfor c = new Callfor();
c.setArguments(bundle);
After that you'll be able to retrieve the values inside your Fragment like you stated above
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_call_for, container, false);
TextView TXT = (TextView) rootView.findViewById(R.id.test);
Bundle test = getArguments();
if(test!=null){
String d = test.getString("test", " ");
TXT.setText(d);
}
return rootView;
}
Hope it helps! :)
Related
I am getting a null object reference when passing a variable from an activity to a fragment and I can't find out what the problem is. The variable I am trying to pass is currentPollName. Any help would be appreciated.
Here is the GroupActivity:
public class GroupActivity extends AppCompatActivity {
private ViewPager myViewPager;
private TabLayout myTabLayout;
private GroupTabsAccessorAdapter myTabsAccessorAdapter;
private String currentPollName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group);
currentPollName = getIntent().getExtras().get("pollName").toString();
Toast.makeText(GroupActivity.this, currentPollName, Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("pollName", currentPollName);
PollFragment fragInfo = new PollFragment();
fragInfo.setArguments(bundle);
Toolbar toolbar = (Toolbar) findViewById(R.id.group_page_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(currentPollName);
myViewPager = (ViewPager) findViewById(R.id.group_tabs_pager);
myTabsAccessorAdapter = new GroupTabsAccessorAdapter(getSupportFragmentManager());
myViewPager.setAdapter(myTabsAccessorAdapter);
myTabLayout = (TabLayout) findViewById(R.id.group_tabs);
myTabLayout.setupWithViewPager(myViewPager);
}
public String getGroupName() {
return currentPollName;
}
}
Here is the PollFragment OnCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
pollView = inflater.inflate(R.layout.fragment_poll, container, false);
currentPollName = getArguments().getString("pollName");
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
GroupNameRef = FirebaseDatabase.getInstance().getReference().child("Groups").child(currentPollName);
InitializeFields();
RetrieveDisplayPollOptions();
newOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RequestNewPollOption();
}
});
submitOption.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SavePollOption();
}
});
return pollView;
}
Here is the Itent that starts the GroupActivity
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String currentPollName = list_view.getItemAtPosition(position).toString();
Intent pollIntent = new Intent(getContext(), GroupActivity.class);
pollIntent.putExtra("pollName", currentPollName);
startActivity(pollIntent);
}
});
I found the following things in the code
fragInfo Fragment is not passed to GroupTabsAccessorAdapter. So
add the fragment to the adapter and then write
myViewPager.setAdapter(myTabsAccessorAdapter);
In PollFragment move currentPollName =
getArguments().getString("pollName"); in onViewCreated as below.
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
currentPollName = getArguments().getString("pollName");
}`
I have 2 java classes (NewCard and inputText) with variable data which I would like to pass to a third class (FinalSubmit). I've done some research on how this is done and I think I've done everything I'm supposed to but when the target class tries to get the data, the app crashes due to a NullPointerException. Here are the onCreateView methods for the 3 classes.
NewCard:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_new_card, container, false);
ImageButton blueBtn = view.findViewById(R.id.blueButton);
ImageButton greenBtn = view.findViewById(R.id.greenButton);
ImageButton redBtn = view.findViewById(R.id.redButton);
blueBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cardNumber = 1;
FinalSubmit fragment = new FinalSubmit();
Bundle bundle = new Bundle();
bundle.putInt("dataPointOne", cardNumber);
fragment.setArguments(bundle);
Toast.makeText(getContext(), "Blue", Toast.LENGTH_SHORT).show();
inputText fragment4 = new inputText();
FragmentTransaction ft4 = getFragmentManager().beginTransaction();
ft4.replace(R.id.frameLayout, fragment4, "FragmentName");
ft4.commit();
}
});
greenBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cardNumber = 2;
inputText finalSubmit = new inputText();
Bundle bundle = new Bundle();
bundle.putInt("dataPointOne", cardNumber);
finalSubmit.setArguments(bundle);
Toast.makeText(getContext(), "Green", Toast.LENGTH_SHORT).show();
inputText fragment4 = new inputText();
FragmentTransaction ft4 = getFragmentManager().beginTransaction();
ft4.replace(R.id.frameLayout, fragment4, "FragmentName");
ft4.commit();
}
});
redBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cardNumber = 3;
FinalSubmit fragment = new FinalSubmit();
Bundle bundle = new Bundle();
bundle.putInt("dataPointOne", cardNumber);
fragment.setArguments(bundle);
Toast.makeText(getContext(), "Red", Toast.LENGTH_SHORT).show();
inputText fragment4 = new inputText();
FragmentTransaction ft4 = getFragmentManager().beginTransaction();
ft4.replace(R.id.frameLayout, fragment4, "FragmentName");
ft4.commit();
}
});
return view;
}
The part dealing with data passing is the
FinalSubmit fragment = new FinalSubmit();
Bundle bundle = new Bundle();
bundle.putInt("dataPointOne", cardNumber);
fragment.setArguments(bundle);
inputText
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_input_text, container, false);
final EditText editText = (EditText) view.findViewById(R.id.text);
String cardText = editText.getText().toString();
Button submit = view.findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), editText.getText().toString(), Toast.LENGTH_SHORT).show();
FinalSubmit finalSubmit = new FinalSubmit();
Bundle bundle = new Bundle();
bundle.putString("dataPointTwo", editText.getText().toString());
finalSubmit.setArguments(bundle);
recordAudio fragment5 = new recordAudio();
FragmentTransaction ft5 = getFragmentManager().beginTransaction();
ft5.replace(R.id.frameLayout, fragment5, "FragmentName");
ft5.commit();
}
});
return view;
}
Data passing part:
FinalSubmit finalSubmit = new FinalSubmit();
Bundle bundle = new Bundle();
bundle.putString("dataPointTwo", editText.getText().toString());
finalSubmit.setArguments(bundle);
Final Submit:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_final_submit, container, false);
final TextView info = view.findViewById(R.id.info);
Button finalSubmit = view.findViewById(R.id.finalS);
finalSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bundle bundle = getArguments();
int cardNumber = bundle.getInt("dataPointOne", 0);
String inputText = bundle.getString("dataPointTwo", "");
info.setText(cardNumber+" "+inputText);//+inputText);
}
});
return view;
}
The exception occurs when trying to get the data here:
Bundle bundle = getArguments();
int cardNumber = bundle.getInt("dataPointOne", 0);
String inputText = bundle.getString("dataPointTwo", "");
Logcat:
06-15 12:53:44.064 16325-16325/com.parrotcards.kyle.parrotcards E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.parrotcards.kyle.parrotcards, PID: 16325
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String, int)' on a null object reference
at com.parrotcards.kyle.parrotcards.FinalSubmit$1.onClick(FinalSubmit.java:42)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
According to my research I should have done everything right, so I would really appreciate if someone could point out what I've done wrong here. Thanks a million!
Your problem here, you create a FinalSubmit with bundle what contain an Int value
But here, you create a new one and set a String value..
So you need to pass bundle with Int value to your inputText and then add to it String value.
Try to use sharedPreference, something like this:
blueBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cardNumber = 1;
FinalSubmit fragment = new FinalSubmit();
Toast.makeText(getContext(), "Blue", Toast.LENGTH_SHORT).show();
getContext().getSharedPreferences("pref",0)
.edit().putInt("int",cardNumber).apply();
FragmentTransaction ft4 = getFragmentManager().beginTransaction();
ft4.replace(R.id.frameLayout, fragment4, "FragmentName");
ft4.commit();
}
});
And extract it
finalSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int cardNumber= getContext().getSharedPreferences("pref",0)
.getInt("int",0);
}
});
And all the same with String value)
you must have to try intent instead put this code in newcard.java when onclick event call it will pass your data from current class to another class
Intent i = new Intent(MainActivity.this,Filesubmit.class);
i.putExtra("value", cardText);
and second activity you can access your value with
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("value");
here is a link for more here
I want to call class FragmentDiagnosis from image view click listener. Im so confused about how to call the other class from click listener? I've tried use intent, but there was error on it. For anyone who understand about this problem, please help me. I will appreciate it, thank you so much.
package com.pakarayam;
import android.app.Fragment;
public class FragmentHome extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.activity_fragment_home, container, false);
configureImageView(view);
return view;
}
private void configureImageView(View view) {
// TODO Auto-generated method stub
ImageView mulai = (ImageView) view.findViewById(R.id.mulai);
mulai.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent iMulai = new Intent(getApplicationContext(), FragmentDiagnosis.class);
startActivity(iMulai);
}});
}}
try this:
public void onClick(View v)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
YourFragment yourFragment= new YourFragment();
fragmentTransaction.add(R.id.your_activity_id, yourFragment, "FRAGMENT");
fragmentTransaction.commit();
}
#SuppressLint("NewApi")
public void addFragment(Fragment fragment, Bundle bundle) {
try {
if (bundle != null) {
fragment.setArguments(bundle);
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_left,
R.anim.slide_right, R.anim.slide_left, R.anim.slide_right);
fragmentTransaction.add(R.id.frame1, fragment).addToBackStack(null)
.commit();
} catch (Exception e) {
Log.e("MenuChangeActivity", "Add fragment error");
}
}
i have four tabs in the tab bar when i select the contact tab from the tab bar the tab bar is disappeared.i want to show the tab bar when i select the contact tab.
Here is code
TabActivity.java
public class Tab_Activity extends TabActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources obj_res = getResources();
TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
TabSpec m_tab1 = tabHost.newTabSpec("First Tab");
TabSpec m_tab2 = tabHost.newTabSpec("Second Tab");
Intent intent1 = new Intent().setClass(this, Phone.class);
Intent intent3 = new Intent().setClass(this, Settings.class);
Intent intent4 = new Intent().setClass(this, Contact.class);
m_tab4.setIndicator("Tab4");
m_tab4=tabHost.newTabSpec("tab4").setIndicator("", obj_res.getDrawable(R.drawable.pic1)).setContent(intent3);
m_tab5.setIndicator("Tab4");
m_tab5=tabHost.newTabSpec("tab4").setIndicator("", obj_res.getDrawable(R.drawable.pic3)).setContent(intent4);
tabHost.addTab(m_tab2);
tabHost.addTab(m_tab4);
tabHost.addTab(m_tab5);
}}
Contact.java
public class Contact extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
//setContentView(R.layout.contact);
Intent Contacts = new Intent(Intent.ACTION_GET_CONTENT);
Contacts.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Contacts.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(Contacts, 1);
}}
This is your activity in which u can put how many tab you want to display.
public class GuestListAdminActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Bride", "Groom", "Create Gueset List" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guest_list_admin);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
getActionBar().setHomeButtonEnabled(true);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
This is pager adapter which will show your all fragments.
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new BrideGuestList();
case 1:
return new GroomGuestList();
case 2:
return new NewGuestListActivity();
default:
break;
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
And make your fragment activity like following
public class BrideGuestList extends android.support.v4.app.Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.bride_guest_list, container, false);
return rootView;
}
In my calendar application, I am using ActionBarSherlock to display two tabs - 1) Calendar 2) Converter. On the click event of an ActionBar menu item, I'm using SlidingMenu drawer to display event summary for that month.
I want to use viewpager specifically for the Calendar tab and I have implemented it this way:
Main.java:
public class BaseActivity extends SlidingSherlockFragmentActivity
{
private ViewPager pager;
CalendarFragmentPagerAdapter mPagerAdapter;
private static final int MONTHS_LIMIT = 5;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.app_name);
setContentView(R.layout.main);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main, new CalendarFragment())
.commit();
setBehindContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new EventsSummary())
.commit();
setSlidingActionBarEnabled(false);
final ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setHomeButtonEnabled(true);
ActionBar.Tab calendar = bar.newTab();
ActionBar.Tab converter = bar.newTab();
calendar.setText(this.getResources().getString(R.string.calendar));
converter.setText(this.getResources().getString(R.string.converter));
calendar.setTabListener(new MyTabListener());
converter.setTabListener(new MyTabListener());
bar.addTab(calendar);
bar.addTab(converter);
// customize the SlidingMenu
SlidingMenu sm = getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.actionbar_home_width);
sm.setSlidingEnabled(false);
mPagerAdapter = new CalendarFragmentPagerAdapter(getSupportFragmentManager());
pager = (ViewPager) this.findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
pager.setCurrentItem(MONTHS_LIMIT / 2);
}
private class MyTabListener implements ActionBar.TabListener
{
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(tab.getPosition()==0)
{
CalendarFragment frag = new CalendarFragment();
ft.replace(R.id.main, frag);
}
else
{
ConverterFragment frag = new ConverterFragment();
ft.replace(R.id.main, frag);
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
public boolean onOptionsItemSelected(MenuItem item) {
Intent newActivity;
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_slidingmenu:
toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public class CalendarFragmentPagerAdapter extends FragmentPagerAdapter {
public CalendarFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
CalendarFragment fragment = new CalendarFragment();
Bundle args = new Bundle();
args.putInt("offset", position - MONTHS_LIMIT / 2);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return MONTHS_LIMIT;
}
}
}
CalendarFragment.java:
public class CalendarFragment extends Fragment
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
int offset = args.getInt("offset"); //throws NullPointerException
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.simple_calendar_view, container, false);
.....
return v;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("dummy", true);
}
}
The problem is at this line in the CalendarFragment:
int offset = args.getInt("offset");
It throws the error NullPointerException and debugging shows this is because Bundle args = getArguments() is null.
I tried to put the breakpoint at this line in the PagerAdapter:
Bundle args = new Bundle();
but it never reaches here and the application crashes throwing the NullPointerException.
Where am I going wrong?
Here
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.main, new CalendarFragment())
.commit();
and here
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (tab.getPosition() == 0) {
CalendarFragment frag = new CalendarFragment();
ft.replace(R.id.main, frag);
} else {
ConverterFragment frag = new ConverterFragment();
ft.replace(R.id.main, frag);
}
}
you're just creating an empty CalendarFragment instance without any arguments.
So your activity launches, creating an empty Fragment without arguments, and then crashes when trying to get those