【发布时间】: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