A problem with initializing textView in fragment - java

When I start activity, I also start the fragment
conversion_1 = new Conversion();
There is an OnCreateView in the fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_conversion, container, false);
curr_to_txt_1 = view.findViewById(R.id.curr_to_txt);
curr_to_txt_1.setText("rrr");
return view;
}
When I call curr_to_txt_1.setText("text"); , it works in OnCreateView but the app is stopping if I call it in a void funcion later (the function is called in activity and the fuction itself works)
public void set_curr_to_txt() {
curr_to_txt_1.setText("rrr");
}
It says that curr_ro_text is null, but it have been initialized

I tink....
You can use onViewCreated for create the 'curr_to_txt_1' and you not need the view variable.
public class Test extends Fragment {
private TextView text;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_layout, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/*
* Write your bind here
* */
text=view.findViewById(R.id.view_id);
}
}
considering the fragment life cycle, in the onViewCreated, the fragment exist and can bind with components
Any question, I will be happy to help you!

Related

open dialog fragment in Fragment

I have DialogFragment within just some text
and I want open it in Fragment
It's Dialog Fragment first
public class subscribe_dialog_frag extends DialogFragment {
#Override
#Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);
}
}
and it's my fragment code
public class Main4Fragment extends Fragment {
private ImageButton btn_money;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main4, container, false);
btn_money = (ImageButton)view.findViewById(R.id.btn_momey);
btn_money.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment subscribe_dialog_frag = new subscribe_dialog_frag();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.btn_momey, subscribe_dialog_frag).commit();
}
});
return view;
}
}
I just open the dialogFragment.
possible?? or i do another way?
first use naming convention for classes/variables... Rename your dialog to:
public class SubscribeDialog extends DialogFragment {
#Override
#Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);
}
}
in your Main4Fragment call something like this:
btn_money.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SubscribeDialog subscribeDialog = SubscribeDialog();
subscribeDialog.show(getChildFragmentManager(),"SubscribeDialog");
}
});

How to start On Boarding activity after Splash Screen activity in android?

I've made a splash screen(Introductory activity) using Cuberto Liquid Pager. My next activity is supposed to be an on boarding activity where 3 fragments are there. The problem is after running the code, Splash screen & 1st fragment from onboarding activity are getting merged. I've tried different solutions but nothing has worked. Please shed some light on this problem.
IntroductoryActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_introductory);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().hide();
h1= findViewById(R.id.houseintro);
h2= findViewById(R.id.houseintro2);
bottom= findViewById(R.id.bottom);
txt1= findViewById(R.id.atsingleplace);
txt2= findViewById(R.id.findallprop);
appname= findViewById(R.id.findmehome);
h1.animate().translationY(-2000).setDuration(1000).setStartDelay(6000);
h2.animate().translationY(2000).setDuration(1000).setStartDelay(6000);
bottom.animate().translationY(3000).setDuration(1000).setStartDelay(6000);
txt2.animate().translationY(2000).setDuration(1000).setStartDelay(6000);
txt1.animate().translationY(2000).setDuration(1000).setStartDelay(6000);
appname.animate().translationY(2000).setDuration(1000).setStartDelay(6000);
viewPager=findViewById(R.id.pager);
pagerAdapter=new ScreenSlidePagerAdapter(getSupportFragmentManager(),1);
viewPager.setAdapter(pagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
#SuppressLint("WrongConstant")
public ScreenSlidePagerAdapter(#NonNull FragmentManager fm, int i) {
super(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
#NonNull
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
OnBoardingFragment1 tab1 =new OnBoardingFragment1();
return tab1;
case 1:
OnBoardingFragment2 tab2 =new OnBoardingFragment2();
return tab2;
case 2:
OnBoardingFragment3 tab3 =new OnBoardingFragment3();
return tab3;
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
OnBoardingFragment1.java
public class OnBoardingFragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup root=(ViewGroup) inflater.inflate(R.layout.fragment_on_boarding1,container,false);
return root;
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
OnBoardingFragment2.java
public class OnBoardingFragment2 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup root=(ViewGroup) inflater.inflate(R.layout.fragment_on_boarding2,container,false);
return root;
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
OnBoardingFragment3
public class OnBoardingFragment3 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup root=(ViewGroup) inflater.inflate(R.layout.fragment_on_boarding3,container,false);
return root;
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
In case you have still not solved it, I think you can use a viewPager for a separate onboarding screen to swipe through the different layout views and either using different class for the splash or attaching the splash style to the onboarding screen to load before the onboarding screen .

i always get error on the button part and getActivity. its my first time using fragment in android

public class Supplier_Home extends Fragment {
private static final String TAG = "Supplier_Home";
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.s_1home, null);
view = inflater.inflate(R.layout.supply__home__po, container, false);
Button pOrders=(Button)view.findViewById(R.id.pOrders);
pOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked login1.");
Intent intent = new Intent(getContext(), Login.class);
}
});
}
}
Do like, Create View object like this. Pass container object while inflating layout
View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.dashbord, container, false);
Button btn=(Button)view.findViewById(R.id.btn)
return view;
}
You are returning the complete view in the starting itself making the code below return unreachable.
You have given two layouts, If I am not wrong s_1home is layout of your activity where view pager is resting and supply__home__po is your fragment layout. Considering my assumptions are correct please change the code as follows
public class Supplier_Home extends Fragment {
private static final String TAG = "Supplier_Home";
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.supply__home__po, container, false);
Button pOrders=(Button) view.findViewById(R.id.pOrders);
pOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked login1.");
Intent intent = new Intent(getContext(), Login.class);
}
});
return view;
}
}

Fragment throwing Nullpointer

So I have problem manipulating my TextView inside Fragment.
I have simple callback interface defining only onCallBackReceived(), when my method onCallbackReceived() is called back I want to change text of my TextView, however method setText("onCallback") throws NullPointerException. Exception says that the method is called on null object reference, but I initialized it first using findViewById(). Please help.
public class MyFragment extends Fragment implements MySimpleCallback {
TextView textView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.panel_fragment, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
textView = (TextView) view.findViewById(R.id.text_view);
textView.setText("Okay!");
}
#Override
public void onCallbackReceived() {
textView.setText("onCallback");
}
}
Maybe you can findTheViewById in the onCreateView
public class MyFragment extends Fragment implements MySimpleCallback {
TextView textView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.panel_fragment, container, false);
textView = (TextView) view.findViewById(R.id.text_view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
textView.setText("Okay!");
}
#Override
public void onCallbackReceived() {
textView.setText("onCallback");
}
}

How to SetText of One Activity/Fragment from another one

I want to change the text of the Fragment_B from the Fragment_A. How can I do this?
Something like this.
Fragment_A
ola = (TextView) v.findViewById(R.id.textView12);
ola2 = (TextView) (FRAGMENT_B).findViewById(R.id.textView2);
AsyncTask(ola,ola2).execute //this should set text on Fragment_A and Fragment_B
Fragment A:
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_second,container,false);
ola = (TextView) v.findViewById(R.id.textView12);
Asynctask(ola).execute
return v;
}
Fragment B:
public class Fragmento_B extends android.support.v4.app.Fragment{
public TextView ola2;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_third,container,false);
ola2 = (TextView) v.findViewById(R.id.textView2);
Asynctask(ola2).execute
return v;
}
}
What is the problem you are facing? Is it showing some sort of error or is it not changing the text as your or anything?

Categories