Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android fragments must be static

I've got this error, but I don't know how to resolve this error.

Error: Fragments should be static such that they can be re-instantiated by the system, and anonymous classes are not static [ValidFragment]

Please help me if you know how to solve

This is the 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);
        }


    }
}

and this is the fragment class 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();


}
like image 792
felangga Avatar asked Feb 11 '16 12:02

felangga


People also ask

What is static fragment?

A static Fragment is included in an XML file using a fragment tag within another XML layout. A dynamic Fragment isn't associated with a fragment tag and it is created in association with the FragmentManager.

How do Android fragments work?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.

Can we use fragments without activity?

Android app must have an Activity or FragmentActivity that handles the fragment. Fragment can't be initiated without Activity or FragmentActivity.


1 Answers

You are declaring your Fragment as abstract class. Abstract class can't be instantiated. It can be used only as a base class. The way you are trying to use it (anonymous class) is not possible in Android framework. A Fragment class must be a subclass of Fragment (or an existing subclass of it) and concrete.

Remove abstract keyword

public class FoodListFragment extends Fragment {

Things your are trying to achieve with

 public abstract void doRefresh();

should be done using an interface.

like image 59
Rohit5k2 Avatar answered Sep 21 '22 13:09

Rohit5k2