2

Can someone help me? I don't know why have I javaNullExepction in LayoutInflater.Here is my code :

This code is in fragment :

ServiceManager.getInstance().getCampaign( new Callback <Campaign>() {
            @Override
            public void success(Campaign campaign, Response response) {
                campaigns = campaign;
                adapter = new RecyclerCampaignAdapter(getActivity(), campaign);
                recyclerView.setAdapter(adapter);
                recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).marginProvider(adapter).showLastDivider().build());
                adapter.notifyDataSetChanged();
                if (progressBar.isShown()) {
                    progressBar.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                Toast.makeText(getActivity(), "Failed" + retrofitError, Toast.LENGTH_SHORT).show();
                progressBar.setVisibility(View.GONE);
            }
        });

here is code in my adapter :

 public RecyclerCampaignAdapter (Activity activity, Campaign campaignList) {
        this.activity = activity;
        this.campaignList = campaignList;
        this.inflater= LayoutInflater.from(activity);
    }

I think getActivity() == null I don't know how can I fix it.

java.lang.NullPointerException at android.view.LayoutInflater.from(LayoutInflater.java:212) at tr.org.yyd.yeryuzudoktorlari.adapter.RecyclerCampaignAdapter.(RecyclerCampaignAdapter.java:38) at tr.org.yyd.yeryuzudoktorlari.fragment.CampaignFragment$1.success(CampaignFragment.java:69) at tr.org.yyd.yeryuzudoktorlari.fragment.CampaignFragment$1.success(CampaignFragment.java:65) at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5756) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method)

EDIT: I use replace method on frgment transaction when user clicks on an item from menu.So CampaignFragment works okey when the user clicks in first time but it crashes when user clicks again while it is still on. If I change into another fragment and come back to CampaginFragment there is no crash.

Olcay Sönmez
  • 602
  • 1
  • 7
  • 16
  • Are you using RecyclerView.Adapter ? – moud Sep 21 '15 at 07:56
  • try to use getApplicationContext() instead of getActivity and in adapter use Context instance instead of Activity –  Sep 21 '15 at 08:03
  • Yes I am using RecyclerView.Apadpter. I call Recyclervew.Adapter in Fragment.There is no command for getApplicationContext. – Olcay Sönmez Sep 21 '15 at 08:09
  • **1.** check `getActivity()` result, probably it's null **2.** where do you call `ServiceManager.getInstance().getCampaign` ? In `onCreateView` etc ? – Alexander Malakhov Sep 21 '15 at 08:16
  • The Service Manager is for retrofit. It is singelton class. Service Manager was in onCreateView but there was the same error.Then I thing if I call the service manager in onActivityCreated , maybe it fixed but that was useless too. :) – Olcay Sönmez Sep 21 '15 at 08:25
  • use this one RecyclerCampaignAdapter (Context context, Campaign campaignList) – Ajit Kumar Dubey Sep 21 '15 at 13:18
  • If you are using targetSdkVersion 23 and using the framework Fragment class (instead of the support lib Fragment class) then it may have to do with the deprecating of `onAttach(Activity)`. See http://stackoverflow.com/questions/32083053/android-fragment-onattach-deprecated. Use the support lib's Fragments to work around that. – Rob Meeuwisse Sep 21 '15 at 14:02
  • It didn't work. I don't know what make I a mistake. I use the same adapter, which is name difficult.I haven't an error i this adapter. :( – Olcay Sönmez Sep 21 '15 at 14:13
  • Okey I will be look right now.Thank u. – Olcay Sönmez Sep 21 '15 at 14:16
  • I am using targetSdk 22. Android studio kidding with me :D – Olcay Sönmez Sep 21 '15 at 14:35
  • The answer is : if(getActivity() != null) { adapter = new RecyclerCampaignAdapter(getActivity(), campaigns); recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).marginProvider(adapter).showLastDivider().build()); recyclerView.setAdapter(adapter); } – Olcay Sönmez Sep 22 '15 at 07:09

1 Answers1

0

The answer just that.I don't know how that fix it but it worked.It's just a control.

if (getActivity() != null) {
    adapter = ....
Olcay Sönmez
  • 602
  • 1
  • 7
  • 16