动态填充片段内的对话框

时间:2016-12-14 19:06:05

标签: android android-fragments fragment

我创建了一个片段,想要点击按钮开始对话,我将使用线性布局垂直方向显示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>

1 个答案:

答案 0 :(得分:1)

您正在使用View方法为Dialog创建onCreateView

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

并使用getSongsAndFillDialogue方法完成它,但您要为Dialog另一个仅传递资源ID的新实例进行设置:

fbDialogue.setContentView(R.layout.fragment_selectsong);

当你传递id时,Dialog会自动膨胀这个布局(就像你一样)。因此,创建了两个布局,一个在Activity - 已完成,一个已创建&#34;自动&#34;通过setContentView方法,但你的方法根本没有触及(所以它保持为空)

而不是传递已准备好的tempView资源ID传递,如下所示:

fbDialogue.setContentView(tempView);