-1

Past Few days,Facing this problem .I know about NullPointerException . Fragment launch to crashing .

Java

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_tab_home,container,false);


        final Dialog alertDialog = new Dialog(getActivity());
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alertDialog.setContentView(R.layout.dialog_viewpager);
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        alertDialog.show();
        viewPager = (ViewPager)alertDialog.findViewById(R.id.view_pager);

        // layouts of all welcome sliders
        // add few more layouts if you want
              layouts = new int[]{
                R.layout.welcome_slide1,
                R.layout.welcome_slide2,
                R.layout.welcome_slide3,

        }

        myViewPagerAdapter = new ViewPagerAdapter();
        viewPager.setAdapter(myViewPagerAdapter);

        RelativeLayout rl_header_thirdObj=(RelativeLayout)alertDialog.findViewById(R.id.rl_header_third);
    //Here//    rl_header_thirdObj.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                alertDialog.dismiss();
            }

    return view;
}

Crash

at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.fragment.FragmentHome.onCreateView

Indicate Line rl_header_thirdObj.setOnClickListener .

FYI

RelativeLayout rl_header_thirdObj presents in welcome_slide3.xml .

May I know what is the correct way to achieve my objective? Does any guys meet this issue?Any help would be greatly appreciated.

Community
  • 1
  • 1
A B
  • 33
  • 9

1 Answers1

1

Here Relativelayout is not a part of your alertDialog view.

so instead of:

 RelativeLayout rl_header_thirdObj=(RelativeLayout)alertDialog.findViewById(R.id.rl_header_third);

use:

 RelativeLayout rl_header_thirdObj=(RelativeLayout)view.findViewById(R.id.rl_header_third);
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • where are you using `layouts[]` ? – rafsanahmad007 Apr 01 '17 at 09:00
  • i would suggest you to add `rl_header_thirdObj` in alertdialog layout `dialog_viewpager`..apply the above code – rafsanahmad007 Apr 01 '17 at 09:04
  • `dialog_viewpager` xml is parent of viewpager which hold 3 layouts .If onclick events not sets then it works – A B Apr 01 '17 at 09:07
  • For Viewpager If you want click events on each page then you'll have to build your listener into the page content within your Adapter.: see this for [Help](http://stackoverflow.com/questions/16350987/viewpager-onitemclicklistener) – rafsanahmad007 Apr 01 '17 at 09:14