setOnItemLongClick() doesn't work in Fragment

94 views Asked by At

I've a ListView from Json API. But, when I want to click or choose one of them, my App crash a.ka force closed. Can someone please help me with this? I've spent much time in trying to fix this. Even I read some discussions here for several days which is can relate or similar with this issue, it doesn't help. Thank you! :)

This is the source code:

public class KategoriFragment extends Fragment {

@BindView(R.id.kategori_lv)
ListView kategoriLv;
Unbinder unbinder;

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_kategori, container, false);
    unbinder = ButterKnife.bind(this, v);

    getData();

    //String value = getArguments().getString("title");

    return v;
}

private void getData() {

    RetrofitRequest.UserAPIService apiService = RetrofitRequest.getRetrofit().create(RetrofitRequest.UserAPIService.class);

    final ProgressDialog pg = new ProgressDialog(getContext());
    pg.show();

    apiService.getKategori(app_api,"user_name","pass").enqueue(new Callback<ResponKategori>() {
        @Override
        public void onResponse(Call<ResponKategori> call, Response<ResponKategori> response) {

            pg.dismiss();

            try {

                if (response.body().getError() == false) {
                    final String[] itemListKategori = new String[15];
                    itemListKategori[0] = response.body().getData().get1().toString();
                    itemListKategori[1] = response.body().getData().get2().toString();
                    itemListKategori[2] = response.body().getData().get3().toString();
                    itemListKategori[3] = response.body().getData().get4().toString();
                    itemListKategori[4] = response.body().getData().get5().toString();
                    itemListKategori[5] = response.body().getData().get6().toString();
                    itemListKategori[6] = response.body().getData().get7().toString();
                    itemListKategori[7] = response.body().getData().get8().toString();
                    itemListKategori[8] = response.body().getData().get9().toString();
                    itemListKategori[9] = response.body().getData().get10().toString();
                    itemListKategori[10] = response.body().getData().get11().toString();
                    itemListKategori[11] = response.body().getData().get12().toString();
                    itemListKategori[12] = response.body().getData().get13().toString();
                    itemListKategori[13] = response.body().getData().get14().toString();
                    itemListKategori[14] = response.body().getData().get15().toString();

                    /*AdapterKategori adapterKategori = new AdapterKategori();
                    LinearLayoutManager llm = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
                    kategoriRv.setLayoutManager(llm);
                    kategoriRv.setAdapter(adapterKategori);*/

                    kategoriLv.setAdapter(new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item,  itemListKategori));

                    kategoriLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                        @Override
                        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                            startActivity(new Intent(getContext(),AdminPanelFragment.class).putExtra("Title",itemListKategori[position]));
                            return true;
                        }
                    });

                        } else {
                            Toast.makeText(getContext(), "Failed", Toast.LENGTH_SHORT).show();
                        }

                    } catch (Exception e) {
                        pg.dismiss();
                        Toast.makeText(getContext(), "Failed: " + e, Toast.LENGTH_SHORT).show();
                        Log.e("errorRead", "" + e.toString());
                    }
                        }

        @Override
        public void onFailure(Call<ResponKategori> call, Throwable t) {
            pg.dismiss();
            Toast.makeText(getContext(), "Failed " + t, Toast.LENGTH_SHORT).show();
            Log.e("errorRead", "" + t.toString());
        }
    });
}
        @Override
        public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
     }
}

Here's I put logcat too:

08-04 13:41:15.935 2531-2531/dummy.project.sobud E/AndroidRuntime: FATAL EXCEPTION: main
Process: dummy.project.sobud, PID: 2531
android.content.ActivityNotFoundException: Unable to find explicit activity class {dummy.project.sobud/dummy.project.sobud.admin_panel.AdminPanelFragment}; have you declared this activity in your AndroidManifest.xml?
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1820)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1531)
    at android.app.Activity.startActivityForResult(Activity.java:4404)
    at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
    at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:230)
    at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:806)
    at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:920)
    at android.support.v4.app.Fragment.startActivity(Fragment.java:908)
    at android.support.v4.app.Fragment.startActivity(Fragment.java:897)
    at dummy.project.sobud.kategori.KategoriFragment$1$1.onItemLongClick(KategoriFragment.java:105)
    at android.widget.AbsListView.performLongPress(AbsListView.java:4223)
    at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:4122)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
1

There are 1 answers

4
Mehul Kabaria On

You can't not directly open Open AdminPanelFragment Fragment in stand of activity while ListView setOnItemLongClickListener.