Android支持库v4对话框片段

时间:2015-03-03 11:58:33

标签: android android-support-library

我想做什么? 我有这个片段加载一个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;
    }
}

请有人帮助我解决支持库问题吗?

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:3)

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

扩展对话框片段

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