2017-04-01 57 views
-1

過去幾天,面臨這個問題。我知道關於NullPointerException。 片段啓動崩潰。在 com.fragment 顯示java.lang.NullPointerException:佈局setOnClickListener不工作

的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; 
} 

崩潰

在dalvik.system.NativeStart.main(本機方法)所造成的。 FragmentHome.onCreateView

指示線rl_header_thirdObj.setOnClickListener

FYI

RelativeLayout的rl_header_thirdObj呈現welcome_slide3.xml

我可以知道什麼是實現我目標的正確方法是什麼?有沒有人遇到這個問題?任何幫助將不勝感激。

+0

發表您的'fragment_tab_home'佈局 – rafsanahmad007

+0

''fragment_tab_home簡單.Parent是'相對Layout'並有'Button' – Kankana

+0

發表您的佈局xml文件。 –

回答

1

這裏Relativelayout是不是你alertDialog圖的一部分。

所以不是:

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

使用:

RelativeLayout rl_header_thirdObj=(RelativeLayout)view.findViewById(R.id.rl_header_third); 
+0

都能跟得上。我你在哪裏使用'佈局[]'嘗試已經.same問題 – Kankana

+0

? – rafsanahmad007

+0

我建議你在alertdialog佈局'dialog_viewpager'中添加'rl_header_thirdObj' ..申請上面的代碼 – rafsanahmad007