0

My app contains navigation drawer. And within that there is one item fragment. In that fragment I tried to implement three button click events to open 3 activities. But, whenever I click on the fragment from the navigation drawer the app crashes with nullPointerAcception. After debug it shows issues with the below code containing onClickListener:

itemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
                startActivity(itemIntent);
            }
        });

Here's my fragment code.

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


/**
 * A simple {@link Fragment} subclass.
 */
public class ItemsFragment extends Fragment {

    Button itemButton;
    Button categoryButton;
    Button discountButton;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        itemButton=(Button) container.findViewById(R.id.items_button);
        categoryButton=(Button) container.findViewById(R.id.category_button);
        discountButton=(Button) container.findViewById(R.id.discount_button);

        itemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
                startActivity(itemIntent);
            }
        });

        setHasOptionsMenu(false);
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_items, container, false);
    }

}

// The Logcat error

E/AndroidRuntime: FATAL EXCEPTION: main
                  java.lang.NullPointerException
                      at com.soumya.possystem.ItemsFragment.onCreateView(ItemsFragment.java:36)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
                      at android.os.Handler.handleCallback(Handler.java:725)
                      at android.os.Handler.dispatchMessage(Handler.java:92)
                      at android.os.Looper.loop(Looper.java:137)
                      at android.app.ActivityThread.main(ActivityThread.java:5041)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:511)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                      at dalvik.system.NativeStart.main(Native Method)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Soumya Rauth
  • 1,163
  • 5
  • 16
  • 32

5 Answers5

4

Before inflating view and created view you are trying to find instance of the controls.and return inflated view in onCreateView

Another way is to take the instance(findviewById) in onViewCreated or onActivityCreated

Second issue is you are trying to get control's object from container instead of that you need to should take from inflated view.

View v=inflater.inflate(R.layout.fragment_items, container, false);

Below i have kept code with changes.

import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;


    /**
     * A simple {@link Fragment} subclass.
     */
    public class ItemsFragment extends Fragment {

        Button itemButton;
        Button categoryButton;
        Button discountButton;

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


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    View v=inflater.inflate(R.layout.fragment_items, container, false);
    //check in place of container here you need to use inflated view (`v`) instance
            itemButton=(Button) v.findViewById(R.id.items_button);
            categoryButton=(Button) v.findViewById(R.id.category_button);
            discountButton=(Button) v.findViewById(R.id.discount_button);

            itemButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
                    startActivity(itemIntent);
                }
            });

            setHasOptionsMenu(false);
            // Inflate the layout for this fragment
            return v;
        }

    }
user1140237
  • 5,015
  • 1
  • 28
  • 56
1

You have to initialize view as per requirement:
Create a Fragment Class

So you have to do
View view = inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
and then
itemButton=(Button) view.findViewById(R.id.items_button);

Bhavana Vadodariya
  • 2,287
  • 1
  • 19
  • 26
1
    public class ItemsFragment extends Fragment {

    Button itemButton;
    Button categoryButton;
    Button discountButton;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    /** Inflate rootView of fragment **/
    View rootView = inflater.inflate(R.layout.fragment_items, container, false);

      //replace "container" with "rootView" **/
        itemButton=(Button) rootView.findViewById(R.id.items_button);
        categoryButton=(Button) rootView.findViewById(R.id.category_button);
        discountButton=(Button) rootView.findViewById(R.id.discount_button);

        itemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
                startActivity(itemIntent);
            }
        });

        setHasOptionsMenu(false);
        // Inflate the layout for this fragment
        return rootView; // Return the view object you inflated
    }

}
Mohammed Junaid
  • 1,362
  • 14
  • 18
0

Try this

    View rootView = inflater.inflate(R.layout.fragment_id, container, false);
    itemButton=(Button) container.findViewById(R.id.items_button);
    categoryButton=(Button) container.findViewById(R.id.category_button);
    discountButton=(Button) container.findViewById(R.id.discount_button);
.
.
.

    return rootView;
Amit Bhandari
  • 3,014
  • 6
  • 15
  • 33
0

Try this:

public class ItemsFragment extends Fragment {

Button itemButton;
Button categoryButton;
Button discountButton;

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v=inflater.inflate(R.layout.fragment_items, container, false);
    setHasOptionsMenu(false);
    return v;
}

 @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
   itemButton=(Button) view.findViewById(R.id.items_button);
    categoryButton=(Button) view.findViewById(R.id.category_button);
    discountButton=(Button) view.findViewById(R.id.discount_button);

    itemButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent itemIntent=new Intent(getActivity(),ItemsActivity.class);
            startActivity(itemIntent);
        }
    });

}
}
Vishal Jadav
  • 924
  • 9
  • 11