【问题标题】:calling dialog fragment from my fragment从我的片段中调用对话框片段
【发布时间】:2015-05-05 21:52:55
【问题描述】:

我正在尝试从我的 Fragment 类中调用一个 DialogFragment。我有一个 EditText,想在我设置的 EditText 的 onClickListener 中调用我的 DialogFragment 类。

我在尝试调用 DialogFragment 时设置的代码在 onClick 中出现错误。

我在“show”上收到一个错误,说明“DialogFragment 类型中的方法 show(FragmentManager, String) 不适用于参数 (FragmentManager, String)”,并且在“new Instance”上收到一个错误,说明“方法对于 MyDialogFragment 类型未定义 newInstance()"

这是我的代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View root = (View) inflater.inflate(R.layout.fragment_profile_fragment, container, false);

        setListenerOnWeight(root);
        button(root);


        return root;
    }

    public void setListenerOnWeight(View v) {
        EditText Weight = (EditText) v.findViewById(R.id.Weight_up);
        Weight.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                weight_frag dialog = new weight_frag.newInstance();
                dialog.show(getFragmentManager(), "fragmentDialog");

            }


        });
    }

对于我的 DialogFragment 类:

package com.the.healthescort;


import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class weight_frag extends DialogFragment {

    Context mContext;

    public weight_frag() {
        mContext = getActivity();
    }

    public static weight_frag newInstance() {
        weight_frag f = new weight_frag();
        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
        alertDialogBuilder.setTitle("Set Wallpaper?");
        alertDialogBuilder.setMessage("Are you sure?");
        //null should be your on click listener
        alertDialogBuilder.setPositiveButton("OK", null);
        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        return alertDialogBuilder.create();
    }




}

【问题讨论】:

标签: java android android-fragments


【解决方案1】:

我认为你应该更换

import android.app.Fragment;

import android.support.v4.app.Fragment;

在你的“我的片段”课程中

你还应该在你的 dialogfragment 类中提供一个名为 newInstance 的方法,如下所示:

   public static weight_frag newInstance(){
    weight_frag frag = new weight_frag();
    return frag;
    }

请遵循 java 命名约定,使用大写字母作为起始字母 (WeightFragment)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多