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");
}
}
Related
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");
}
});
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!
It sounds there is a bug with this library: https://github.com/shivasurya/materialsteppers
The bug is the following: if we are in step 2, for example, and we click next, the oncreate of step 4 is called instead of oncreate of step 3. When we click next, neither the oncreate nor oncreateview of next fragment is called. Which method of the next fragment is called?
Does somebody faced the same problem?
public class ExampleActivity extends ProgressMobileStepper {
List<Class> stepperFragmentList = new ArrayList<>();
#Override
public List<Class> init() {
stepperFragmentList.add(Step1Fragment.class);
stepperFragmentList.add(Step2Fragment.class);
stepperFragmentList.add(Step3Fragment.class);
stepperFragmentList.add(Step4Fragment.class);
stepperFragmentList.add(Step5Fragment.class);
return stepperFragmentList;
}
}
Fragment 1:
public class Step1Fragment extends StepperFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onNextButtonHandler() {
return true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return view;
}
}
Fragment 2
public class Step2Fragment extends StepperFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onNextButtonHandler() {
return true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return view;
}
}
Fragment 3:
public class Step3Fragment extends StepperFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onNextButtonHandler() {
return true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return view;
}
}
Fragment 4:
public class Step4Fragment extends StepperFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onNextButtonHandler() {
return true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return view;
}
}
Fragment 5:
public class Step5Fragment extends StepperFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onNextButtonHandler() {
return true;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return view;
}
}
I am trying to create a memory game, at the first page when I click on the play button it shows the categories and I used fragments. When I click on a button to go to another fragment it doesn't work.
Mainactivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageViewA = findViewById(R.id.play_title);
imageViewA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
FragmentPlay fragA = new FragmentPlay();
transaction.add(R.id.fragmentContainer_main, fragA);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}
FragmentPlay.java
public class FragmentPlay extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_play,container,false);
return rootView;
}
public void addFragB(){
FragmentManager childFragMan=getChildFragmentManager();
FragmentTransaction childFragTrans = childFragMan.beginTransaction();
FragmentAnimal fragmentAnimal = new FragmentAnimal();
childFragTrans.add(R.id.fragmentContainer_main2, fragmentAnimal);
childFragTrans.addToBackStack(null);
childFragTrans.commit();
}
}
FragmentAnimal.java
public class FragmentAnimal extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_animal,container,false);
return rootView;
}
}
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;
}
}