2016-12-14 35 views
0

我已經創建了一個片段,並希望在點擊按鈕時開始對話,其中我將使用線性佈局垂直方向顯示垂直分佈的n個文本。開始我創建了一個textview,我動態填充。但我得到一個空指針異常,說這裏引用的任何佈局指向NULL。動態填充片段內的對話

View view,tempView; 
    TextView myName, myPhNo, myEmail, myDob; 
    ImageButton selectRingTone; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     view = inflater.inflate(R.layout.fragment_profile, container, false); 
     tempView = inflater.inflate(R.layout.fragment_selectsong, container, false); 
     myName = (TextView)view.findViewById(R.id.username); 
     myPhNo = (TextView)view.findViewById(R.id.userPhNo); 
     myEmail = (TextView)view.findViewById(R.id.useremailId); 
     myDob = (TextView)view.findViewById(R.id.userdob); 
     selectRingTone = (ImageButton)view.findViewById(R.id.selectRingTone); 
     setUserProfileData(); 
     //setUserTextStatus(); 
     //setUserAudioStatus(); 
     selectRingTone.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       chooseAndSetRingTone(); 
      } 
     }); 


     return view; 
    } 

    private void chooseAndSetRingTone(){ 
     final Dialog fbDialogue = new Dialog(view.getContext(), android.R.style.Theme_Black); 
     fbDialogue.getWindow().setTitle("Select your audio status song"); 
     fbDialogue.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(100, 0, 0, 0))); 
     fbDialogue.setContentView(R.layout.fragment_selectsong); 
     getSongsAndFillDialogue(); 
     fbDialogue.setCancelable(true); 
     fbDialogue.show(); 
    } 
    private void getSongsAndFillDialogue(){ 
     LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     LinearLayout container = (LinearLayout) tempView.findViewById(R.id.eachRingToneSong); 
     TextView tv = new TextView(tempView.getContext()); 
     tv.setText("Hi hello"); 
     Button b = new Button(tempView.getContext()); 
     b.setText("abcde"); 
     container.addView(tv); 
     container.addView(b); 
    } 

XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLyt" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ScrollView 
     android:id="@+id/scrollCotainerForRingToneSongs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:id="@+id/eachRingToneSong" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <!--<TextView 
       android:id="@+id/song1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="abcde" 
       />--> 
     </LinearLayout> 
    </ScrollView> 

</RelativeLayout> 
+0

顯示異常堆棧跟蹤 – snachmsm

+0

哦對不起!實際上沒有錯誤。它給了我一個窗口,其中標題的設置就像我在代碼中設置的那樣。而文本框或任一按鈕,窗口內都沒有東西。它基本上只有標題集而清空。 – Santanu

回答

1

對創建ViewDialogonCreateView方法:

tempView = inflater.inflate(R.layout.fragment_selectsong, container, false); 

,並使用getSongsAndFillDialogue方法實現它,但你正在爲你的Dialog另一僅傳遞資源ID的新實例:

fbDialogue.setContentView(R.layout.fragment_selectsong); 

當你傳遞只是id然後Dialog正在膨脹這個佈局本身(就像你一樣)。因此,有創造了兩個佈局,一個在Activity - 應驗了,一個setContentView方法創建的「自動」,而不是由你的方法(因此它保持爲空)感動在所有

,而不是通過資源ID通已經準備tempView ,如下所示:

fbDialogue.setContentView(tempView); 
+0

可愛!非常感謝你!!有效。 :) – Santanu

+0

你能回答我的android的另一個問題http://stackoverflow.com/questions/40571579/replace-one-fragment-with-an-another-fragment – Santanu