非静态方法" getActivity"无法从静态上下文引用

时间:2016-05-20 09:59:30

标签: java android android-fragments android-studio static-methods

我使方法静态,但我有这个问题: android.support.v4.app.FragmentTransaction transaction2 = getActivity()。getSupportFragmentManager()。beginTransaction(); ....

getActivity()由IDE强调为红色?

我告诉你我的代码

感谢每一个人!

FRAGMENT:

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

    ListView lv;
    ArrayList<Planet> planetList;
    static PlanetAdapter plAdapter;
    BirraAdapter biAdapter;
    PlanetAdapter.PlanetHolder holder;
    private static Context context = null;
    private static Main mInstance;





    @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);
        context = getActivity();
        mInstance= getActivity();
        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(context, 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 = mInstance.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();

*/









    }



}

活动:

public class Main extends AppCompatActivity{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getSupportFragmentManager().beginTransaction().

                replace(R.id.fragmentContainer, new MyListFragment()).commit();




    }




}

3 个答案:

答案 0 :(得分:1)

getActivity().getSupportFragmentManager().beginTransaction();

更改为

context.getSupportFragmentManager().beginTransaction();

使用您在onCreateView()方法

中初始化的动态上下文

答案 1 :(得分:1)

您可以像@sasikumar建议的那样更改方法,或者如果您不想修改方法,则可以执行此操作:

创建活动的静态实例:例如,如果您的活动的名称是MainActivity,则实例将为private static MainActivity mInstance 然后在片段的onCreateView()中添加初始化:

mInstance=getActivity()

然后改变

android.support.v4.app.FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();

android.support.v4.app.FragmentTransaction transaction2 = mInstance.getSupportFragmentManager().beginTransaction();

答案 2 :(得分:0)

更改您的方法,如下所示

public static void showResult(View v,Context context) 
{
 //your code
}