我想從我的片段類調用DialogFragment。我有一個ImageView,並且想要在我設置的ImageView的onClickListener中調用我的DialogFragment類。從片段調用DialogFragment
我在onClick中遇到了一個錯誤,我用設置的代碼嘗試調用DialogFragment。
我收到「show」錯誤,指出「DialogFragment類型中的方法show(FragmentManager,String)不適用於參數(FragmentManager,String)」和「新實例」中的錯誤,聲明「The方法的newInstance()是未定義的類型MyDialogFragment」
這裏是我的代碼:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
final View v = inflater.inflate(R.layout.image_detail_fragment,
container, false);
mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Here
MyDialogFragment dialog = MyDialogFragment.newInstance();
dialog.show(getFragmentManager(), "fragmentDialog");
}
});
return v;
}
DialogFragment類:
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
class MyDialogFragment extends DialogFragment {
Context mContext;
public MyDialogFragment() {
mContext = getActivity();
}
@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();
}
public static MyDialogFragment newInstance() {
MyDialogFragment = new MyDialogFragment;
return f;
}
}
請添加logcat。 – Aleksandr
我不能看到你的靜態方法MyDialogFragment.newInstance() – Aleksandr