尽管在Android Studio中有文本,但对话框仍显示空白

时间:2015-08-11 16:39:35

标签: java android android-studio

我的对话框显示没有信息看到图片:我的代码在下面..我真的不知道为什么。任何帮助将不胜感激

这是我的代码,为什么会这样显示?

public class myDialogFragment extends DialogFragment{

   @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());

        theDialog.setTitle("Settings");
        theDialog.setMessage("Use the Levels Button To choose Level");
        theDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(getActivity(), "Clicked OK", Toast.LENGTH_SHORT).show();
                    }
                }
        );

        theDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getActivity(), "Clicked CANCEL", Toast.LENGTH_SHORT).show();

            }
        });

        return super.onCreateDialog(savedInstanceState);

    }


}

在我的主要部分,这就是我如何通过它。

public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.action_settings) {

            DialogFragment myFragment = new DialogFragment();
            myFragment.show(getFragmentManager(),"theDialog");



            return true;

1 个答案:

答案 0 :(得分:0)

首先应该从FragmentActivity或Fragment调用DialogFragment类。

课程应该更像这样:

public class DFragment extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.dialogfragment, container,
            false);
    getDialog().setTitle("DialogFragment Tutorial");        
    // Do something else
    return rootView;
}
 }

这是您从

调用的活动
public class MainActivity extends FragmentActivity {

Button dfragbutton;
Button alertdfragbutton;
FragmentManager fm = getSupportFragmentManager();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

    // Locate the button in activity_main.xml
    dfragbutton = (Button) findViewById(R.id.dfragbutton);
    alertdfragbutton = (Button) findViewById(R.id.alertdfragbutton);

    // Capture button clicks
    dfragbutton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            DFragment dFragment = new DFragment();
            // Show DialogFragment
            dFragment.show(fm, "Dialog Fragment");
        }
    });

    // Capture button clicks
    alertdfragbutton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            AlertDFragment alertdFragment = new AlertDFragment();
            // Show Alert DialogFragment
            alertdFragment.show(fm, "Alert Dialog Fragment");
        }
    });
}
 }

有关详细信息,请参阅:http://www.androidbegin.com/tutorial/android-dialogfragment-tutorial/

如果您需要更多帮助,我愿意提供帮助