0

I have created a fragment and want to start a dialogue on a button click on which i would display n numbers of texts distributed vertically using Linear layout vertical orientation. To begin with i have created a textview which i am populating dynamically. But i get a null pointer exception saying the any of the layout i referenced here points to 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 Code:

<?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>
Santanu
  • 893
  • 3
  • 10
  • 24
  • show stacktrace of exception – snachmsm Dec 14 '16 at 19:07
  • Oh Sorry !! There is no error actually. It gives me a window in which the title is set as i have set it in my code. And the text box or either button, nothing comes inside the window. It basically empty with only the title set. – Santanu Dec 15 '16 at 04:17

1 Answers1

1

you are creating View for your Dialog in onCreateView method:

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

and fulfilling it using getSongsAndFillDialogue method, but you are setting for your Dialog another new instance passing only resource id:

fbDialogue.setContentView(R.layout.fragment_selectsong);

when you are passing just id then Dialog is inflating this layout by itself (just like you). So there are two layouts created, one in Activity - fulfilled, and one created "automatically" by setContentView method, but not touched at all by your methods (so it stays empty)

instead of passing resource id pass already prepared tempView, like below:

fbDialogue.setContentView(tempView);
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Can you answer another question of mine on android http://stackoverflow.com/questions/40571579/replace-one-fragment-with-an-another-fragment – Santanu Dec 16 '16 at 04:13