2016-12-14 12 views
0

フラグメントを作成し、リニアレイアウトの垂直方向を使用して垂直方向にn個のテキストを表示するボタンをクリックしてダイアログを開始します。まず、テキストビューを作成しました。このテキストビューは動的に生成されています。しかし、私はここでNULLを指すここで​​参照されたレイアウトのいずれかを言う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

あなたがViewを作成しているあなたのDialogonCreateView方法:

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

getSongsAndFillDialogue方法を使用して、それを果たしていますが、あなたのDialog別の設定されています新しいインスタンスのみを渡すリソースID:

fbDialogue.setContentView(R.layout.fragment_selectsong); 

あなたがちょうどidを渡すとき、Dialogはこのレイアウトを(あなたのように)膨らませています。

はすでにtempViewを準備代わりにリソースIDのパスを渡すので満たし、かつsetContentView方法で「自動的に」作成したものが、あなたの方法では全く触れていない(ので、それは空のまま) - だから、2作成したレイアウト、Activityの1があります以下のようになります。

fbDialogue.setContentView(tempView); 
+0

素敵!どうもありがとうございました!!出来た。 :) – Santanu

+0

あなたは私の他の質問にアンドロイドで答えることができますか?http://stackoverflow.com/questions/40571579/replace-one-fragment-with-an-another-fragment – Santanu

関連する問題