2013-11-26 38 views
1

我試圖在android片段中添加警報對話框。這就像片段加載時應該檢查一個字符串的值,並根據它需要顯示一個對話框..但它的進來如果條件,但警告框不工作..添加提醒對話框中的碎片?

這是我的代碼

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_planet, container, false); 



    if(heart.equals("No connection")) 
    { 
     Toast.makeText(getActivity(), "in alert", Toast.LENGTH_SHORT).show(); 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext()); 
     builder.setCancelable(true); 
     builder.setTitle("Connection Problem"); 
     builder.setMessage("No device detected Do you want to restart the application?"); 
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
     { 
      @Override 
      public void onClick(DialogInterface arg0, int arg1) 
      { 

       Intent restrat=new Intent(getActivity(),LandingPage.class); 
       startActivity(restrat); 


      } 
     }); 

     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
     { 
      @Override 
      public void onClick(DialogInterface arg0, int arg1) 
      { 




      } 
     }); 

    } 
} 

如何解決這個問題呢?

+0

我沒有看到你的任何警報show()方法,你叫的嗎? – saiful103a

+0

雅在那裏.. builder.create()。show(); ..但控制進入循環,然後崩潰.. – Anandhu

+0

你可以發佈你的logcat崩潰細節? – CodeMonkey

回答

1

試試這個,

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

     if (heart.equals("No connection")) { 
      Toast.makeText(getActivity(), "in alert", Toast.LENGTH_SHORT) 
        .show(); 
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity() 
        .getApplicationContext()); 
      builder.setCancelable(true); 
      builder.setTitle("Connection Problem"); 
      builder.setMessage("No device detected Do you want to restart the application?"); 
      builder.setPositiveButton("OK", 
        new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface arg0, int arg1) { 

          Intent restrat = new Intent(getActivity(), 
            LandingPage.class); 
          startActivity(restrat); 

         } 
        }); 

      builder.setNegativeButton("Cancel", 
        new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface arg0, int arg1) { 

         } 
        }); 
      AlertDialog dialog=builder.create(); 
      dialog.show(); 
     } 
     return rootView; 
    } 
+0

它的工作..謝謝.. – Anandhu