Android 支持库 v4 对话框片段

Android support library v4 dialog fragment

我想做什么? 我有这个片段,它加载了 xml。在那个 xml 我有一个图像按钮,它应该显示一条对话框消息。

这是片段的代码;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class LoadingupFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.example, container, false);

 return v;
}


public void dialogboxalert (View view){

    ExampleAlert dialog = new ExampleAlert();
   // dialog.show(getSupportFragmentManager(),""); ERRRORRR

}

这是对话框的代码;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class ExampleAlert extends android.support.v4.app.DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("This is my dialog..").setPositiveButton("OK",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        AlertDialog dialog = builder.create();

        return dialog;
    }
}

有人可以帮我解决支持库问题吗??

出现此问题是因为您的 DialogFragment 扩展了 android.app.DialogFragement,它应该扩展 android.support.v4.app.DialogFragment

由于 Google 发布了新的支持库,您应该使用此 class AppCompatDialog

扩展您的对话框片段

https://developer.android.com/reference/android/support/v7/app/AppCompatDialog.html