markella markella - 6 months ago 39
Android Question

non-static method getActivity()

I made the method "showResult" static but i have problem with :

"Toast.makeText(`getActivity()`.getApplication(), result2 + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();"


How can resolve my problem?
I will share my code.

THANKS IN ADVANCE EVERYBODY!

Here is my first class Fragment.java

public class MyListFragment extends Fragment implements
android.widget.CompoundButton.OnCheckedChangeListener {

ListView lv;
ArrayList<Planet> planetList;
static PlanetAdapter plAdapter;
BirraAdapter biAdapter;
PlanetAdapter.PlanetHolder holder;




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);

Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showResult(v);


}
});
//return inflater.inflate(R.layout.fragment_list2, container, false);
return rootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);


lv = (ListView)getView().findViewById(R.id.listview);
displayPlanetList();


}


private void displayPlanetList() {

planetList = new ArrayList<Planet>();
planetList.add(new Planet("Margherita", 6, "€",1));
planetList.add(new Planet("Diavola", 7,"€",2));
planetList.add(new Planet("Bufalina", 5,"€",3));
planetList.add(new Planet("Marinara", 5, "€",4));
planetList.add(new Planet("Viennese", 4, "€", 5));

plAdapter = new PlanetAdapter(planetList, getContext()) {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);


/*Toast.makeText(
getActivity(),
"Clicked on Pizza: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*/
}


}
};

lv.setAdapter(plAdapter);
}



public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

/*int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);


*//*Toast.makeText(
getActivity(),
"Clicked on Planet: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*//*
}*/

}


public static void showResult(View v) {
String result = "Selected Product are :";
int totalAmount=0;
String result2 = "";
int totalAmount2=0;

String a="";
String z="";
/*for (Birra b : biAdapter.getBox()){

if (b.selected){

result += "\n" + b.name+" "+b.distance+"€"+"q.tà :"+b.getQuantità();
int quantitaInt= Integer.parseInt(b.getQuantità());
totalAmount+=b.distance * quantitaInt;
//a=String.valueOf(totalAmount);


}
}*/
for (Planet p : plAdapter.getBox()) {
if (p.isSelected()){


result2 += "\n" + p.getName()+" "+p.getDistance()+"€"+"q.tà :"+p.getQuantità();
int quantitaInt= Integer.parseInt(p.getQuantità() );
totalAmount2+=p.getDistance() * quantitaInt;
//z=String.valueOf(totalAmount2);


}
int totale=totalAmount+totalAmount2;
z=String.valueOf(totale);

}

Toast.makeText(getActivity().getApplication(), result2 + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();
//Toast.makeText(getActivity(), result2 + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();


Bundle bun2 = new Bundle();
bun2.putString("scelta", result2);
ThreeFragment fgsearch2 = new ThreeFragment();
fgsearch2.setArguments(bun2);
// android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
// transaction2.replace(R.id.content_main, fgsearch2);
// transaction2.commit();




/*Bundle bun = new Bundle();
bun.putString("totale", a);
TwoFragment fgsearch = new TwoFragment();
fgsearch.setArguments(bun);
android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main2, fgsearch);
transaction.commit();*//*
*/
/*



Bundle bun = new Bundle();
bun.putString("scelta", result2);
TwoFragment fgsearch = new TwoFragment();
fgsearch.setArguments(bun);
android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main2, fgsearch);
transaction.commit();

*/









}



}


And another class ThreeFragment.java

public class ThreeFragment extends Fragment implements
android.widget.CompoundButton.OnCheckedChangeListener {

ListView lv2;
ArrayList<Planet> planetList;
ListView lv;

ArrayList<Birra> birraList;
BirraAdapter biAdapter;
PlanetAdapter plAdapter;
Planet p;
String myInt="";
//MyListFragment mlf;



PlanetAdapter.PlanetHolder holder;


public ThreeFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_three, container, false);

Button mButton = (Button) rootView.findViewById(R.id.button2);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// mlf.showResult(v);
MyListFragment.showResult(v);
showResult2(v);


}
});
//return inflater.inflate(R.layout.fragment_list2, container, false);
return rootView;
}


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

lv2 = (ListView) getView().findViewById(R.id.listview2);
displayBirraList();
}


private void displayBirraList() {

birraList = new ArrayList<Birra>();
birraList.add(new Birra("Paulaner", 6, "€"));
birraList.add(new Birra("Forst", 7, "€"));
birraList.add(new Birra("Peroni", 5, "€"));
birraList.add(new Birra("Corona", 5, "€"));
birraList.add(new Birra("Nastro Azzurro", 4, "€"));
biAdapter = new BirraAdapter(birraList, getContext()) {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = lv2.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Birra b = birraList.get(pos);
b.setSelected(isChecked);


/*Toast.makeText(
getActivity(),
"Clicked on Pizza: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*/
}


}


};


lv2.setAdapter(biAdapter);
}


public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


/*int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);


*//**//**//**//*Toast.makeText(
getActivity(),
"Clicked on Planet: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*//**//**//**//*
}
*/
}


public void showResult2(View v) {
String result = "Selected Product are :";
int totalAmount = 0;
String c = "";


for (Birra b : biAdapter.getBox()) {

if (b.selected) {

result += "\n" + b.name + " " + b.distance + "€" + "q.tà :" + b.getQuantità();
int quantitaInt = Integer.parseInt(b.getQuantità());
totalAmount += b.distance * quantitaInt;
//a=String.valueOf(totalAmount);


}
}
/* for (Planet p : plAdapter.getBox()) {
if (p.isSelected()) {


result += "\n" + p.getName() + " " + p.getDistance() + "€" + "q.tà :" + p.getQuantità();
int quantitaInt = Integer.parseInt(p.getQuantità());
//totalAmount2+=p.distance * quantitaInt;
//z=String.valueOf(totalAmount2);


}
}*/



Toast.makeText(getActivity(), result + "\n" +myInt + "\n" + "Total Amount:=" + totalAmount + "€", Toast.LENGTH_LONG).show();
/* Bundle bun2 = new Bundle();
bun2.putString("scelta2", result);
TwoFragment fgsearch2 = new TwoFragment();
fgsearch2.setArguments(bun2);
android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
transaction2.replace(R.id.content_main, fgsearch2);
transaction2.commit();


Bundle bun = new Bundle();
bun.putString("totale2", c);
TwoFragment fgsearch = new TwoFragment();
fgsearch.setArguments(bun);
android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main2, fgsearch);
transaction.commit();

*/


}
}

Answer

Try this

private static Context context = null;

initialize it in onCreateView() like

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        context = getActivity(); //check this
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);

        Button mButton = (Button) rootView.findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showResult(v);


            }
        });
        //return inflater.inflate(R.layout.fragment_list2, container, false);
        return rootView;
    }

And in that toast change like this

Toast.makeText(context, result2 + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();