Android片段必须是静态的

时间:2016-02-11 12:35:07

标签: java android android-fragments fragment

我遇到此错误,但我不知道如何解决此错误。

  

错误:片段应该是静态的,以便系统可以重新实例化,并且匿名类不是静态的[ValidFragment]

如果您知道如何解决,请帮助我

这是MainActivity.java

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private String keyPref = "test";
    private SharedPreferences pref;
    private static SharedPreferences.Editor editor;
    private static ViewPager view;

    private FoodListFragment makanan = new FoodListFragment() {
        @Override
        public void doRefresh() {
            updateData();
        }
    };
    private FoodListFragment minuman = new FoodListFragment() {
        @Override
        public void doRefresh() {
            updateData();
        }
    };
    private FoodListFragment snack = new FoodListFragment() {
        @Override
        public void doRefresh() {
            updateData();
        }
    };
    private Adapter adapter = new Adapter(getSupportFragmentManager());

    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 4343;
    private BroadcastReceiver mRegistrationBroadcastReceiver;
    private static ProgressDialog loading;



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

        pref = getApplicationContext().getSharedPreferences(keyPref, MODE_PRIVATE);
        editor = pref.edit();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);
        }

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setBackgroundTintList(ColorStateList.valueOf(Color.rgb(183,28,28)));
        fab.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), CartActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);


        DBHelper db = new DBHelper(this);

        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
                SharedPreferences sharedPreferences =
                        PreferenceManager.getDefaultSharedPreferences(context);
                boolean sentToken = sharedPreferences
                        .getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);

            }
        };


        loading = new ProgressDialog(this);
        loading.setMessage("Loading");
        loading.setTitle("Menu");

        updateData();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }


    @Override
    protected void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();

        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
                new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }



    public void updateData() {

        ServerHelper server = new ServerHelper() {
            @Override
            public void onStart() {
                loading.show();
            }

            @Override
            public void onFinish() {
                loading.dismiss();
            }

            @Override
            public void onSuccess(int statusCode, String response) {

                try {
                    editor.putString("response", response);
                    editor.commit();

                    makanan.clearFood();
                    minuman.clearFood();
                    snack.clearFood();

                    JSONArray foods = new JSONArray(response);
                    for (int i = 0; i <= foods.length()-1; i++) {

                        JSONObject object = foods.getJSONObject(i);
                        JSONObject subs = object.getJSONObject("SubCategory");
                        FoodCategory addFood = new FoodCategory(subs.getString("name"), subs.getString("photo"));
                        if (subs.getString("categories_id").contains("1")) {
                            makanan.addFood(addFood);
                        } else if (subs.getString("categories_id").contains("2")) {
                            minuman.addFood(addFood);
                        } else if (subs.getString("categories_id").contains("3")) {
                            snack.addFood(addFood);
                        }
                    }

                    makanan.doneRefresh();
                    minuman.doneRefresh();
                    snack.doneRefresh();
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(view.getContext(),"Terjadi masalah koneksi, silahkan coba kembali", Toast.LENGTH_LONG).show();
                }
                loading.dismiss();
            }

            @Override
            public void onFailure(int statusCode, String response) {
                if (view != null) {
                    Snackbar.make(view, "Terjadi Masalah Koneksi", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                } else {
                    Toast.makeText(MainActivity.this,"Terjadi Masalah Koneksi", Toast.LENGTH_LONG).show();
                }
                makanan.doneRefresh();
                minuman.doneRefresh();
                snack.doneRefresh();
                loading.dismiss();
            }
        };


        server.getAllSubs();
    }

    private void setupViewPager(ViewPager viewPager) {
        makanan.setRetainInstance(true);
        minuman.setRetainInstance(true);
        snack.setRetainInstance(true);

        adapter.addFragment(makanan, "Makanan");
        adapter.addFragment(minuman, "Minuman");
        adapter.addFragment(snack, "Snack");
        viewPager.setAdapter(adapter);

        updateData();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    private void setupDrawerContent(final NavigationView navigationView) {
        View header = navigationView.getHeaderView(0);
        TextView username = (TextView) header.findViewById(R.id.main_username);

        DBHelper db = new DBHelper(this);
        username.setText(db.getUsers().getName());

        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {

                switch (menuItem.getItemId()) {
                    case R.id.nav_home: {
                        menuItem.setChecked(true);
                        break;
                    }
                    case R.id.nav_order: {
                        navigationView.getMenu().getItem(0).setChecked(true);
                        Intent order = new Intent(MainActivity.this, OrderActivity.class);
                        startActivity(order);
                        break;
                    }
                    case R.id.nav_cart: {
                        navigationView.getMenu().getItem(0).setChecked(true);
                        Intent cart = new Intent(MainActivity.this, CartActivity.class);
                        startActivity(cart);
                        break;
                    }

                    case R.id.nav_logout: {
                        AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                        dialog.setMessage("Anda yakin ingin logout akun anda ?");
                        dialog.setNegativeButton("Batal",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface arg0, int arg1) {

                                    }
                                });

                        dialog.setPositiveButton("Keluar",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface arg0, int arg1) {
                                        DBHelper logout = new DBHelper(MainActivity.this);
                                        logout.clearSPConfig();
                                        LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mRegistrationBroadcastReceiver);
                                        Intent goLogin = new Intent(MainActivity.this, LoginMainActivity.class);
                                        startActivity(goLogin);
                                    }
                                });

                        AlertDialog alertDialog = dialog.create();
                        alertDialog.show();

                    }
                }
                mDrawerLayout.closeDrawers();
                return true;
            }
        });

    }


    public static class Adapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();


        public Adapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {

            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }


    }
}

这是片段类FoodListFragment.java

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public abstract  class FoodListFragment extends Fragment {
    private ArrayList<FoodCategory> foodList = new ArrayList<FoodCategory>();
    private RecyclerView rv;
    private RecyclerViewAdapter adapter;
    private SwipeRefreshLayout swipeLayout;
    private View view;

    FoodListFragment() {

    }

    public void addFoods(ArrayList<FoodCategory> food) {
        foodList = food;
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void addFood(FoodCategory food) {
        foodList.add(food);
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void clearFood() {
        foodList.clear();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        final RecyclerView rv = (RecyclerView) inflater.inflate(R.layout.fragment_food_list, container, false);

        view = inflater.inflate(R.layout.fragment_food_list, null);
        swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                doRefresh();
            }
        });
        swipeLayout.setColorSchemeColors(Color.RED, Color.GRAY);

        RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview);
        setupRecyclerView(rv);

        return view;
    }

    private void setupRecyclerView(final RecyclerView recyclerView) {
        //recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 2));
        recyclerView.setHasFixedSize(true);

        setRetainInstance(true);
        adapter = new RecyclerViewAdapter(getActivity(),foodList);
        recyclerView.setAdapter(adapter);
    }

    public void doneRefresh(){
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);

        }
    }

    public abstract void doRefresh();


}

1 个答案:

答案 0 :(得分:5)

您宣布Fragmentabstract课程。 Abstract课程无法实例化。它只能用作基类。在Android框架中,您尝试使用它的方式(anonymous类)是不可能的。 Fragment类必须是Fragment(或其现有子类)的子类,并且具体。

删除abstract关键字

public class FoodListFragment extends Fragment {

你想要实现的目标

 public abstract void doRefresh();

应该使用界面完成。