我有一个我已经设置的Main Fragment类。在那个Main Fragment中,我有一个带有onClick监听器的ImageView。一旦调用onClick,它就会打开一个Dialog Fragment。
在我的Dialog Fragment中,我在其中设置了一个简单的“Ok”或“Cancel”选项。一旦“好” 单击按钮,将其设置为将父活动强制转换为主片段,并执行在主片段内设置的名为“onDialogOKPressed”的方法,以便它仅响应该方法内部的代码。
我的代码中没有错误,一旦点击“确定”,我就无法看到“NullpointerException”的位置
非常感谢任何帮助,谢谢。
点击主要片段:
@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) {
MyDialogFragment dialog = new MyDialogFragment();
dialog.show(getActivity().getSupportFragmentManager(),
"MyDialogFragment");
}
});
return v;
}
对话片段:
class MyDialogFragment extends DialogFragment {
private Button ButtonOk;
private Button ButtonCancel;
public MyDialogFragment() {
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.prompt, container);
ButtonOk = (Button) view.findViewById(R.id.button1);
ButtonCancel = (Button) view.findViewById(R.id.button2);
getDialog().setTitle("Set Wallpaper?");
ButtonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
((ImageDetailFragment) (MyDialogFragment.this
.getParentFragment())).onDialogOKPressed();
}
});
ButtonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MyDialogFragment.this.dismiss();
}
});
return view;
}
}
OnDialogOKPressed:
public void onDialogOKPressed() {
BitmapDrawable drawable = (BitmapDrawable) mImageView
.getDrawable();
Bitmap bitmap = drawable.getBitmap();
bitmap = Bitmap.createScaledBitmap(bitmap, 950, 1000, true);
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getActivity());
try {
myWallpaperManager.setBitmap(bitmap);
;
Toast.makeText(getActivity(),
"Wallpaper Successfully Set!", Toast.LENGTH_LONG)
.show();
} catch (IOException e) {
Toast.makeText(getActivity(), "Error Setting Wallpaper",
Toast.LENGTH_LONG).show();
}
}
logcat的:
10-12 09:25:06.099: E/AndroidRuntime(9362): FATAL EXCEPTION: main
10-12 09:25:06.099: E/AndroidRuntime(9362): java.lang.NullPointerException
10-12 09:25:06.099: E/AndroidRuntime(9362): at com.Question.ui.MyDialogFragment$1.onClick(MyDialogFragment.java:35)
10-12 09:25:06.099: E/AndroidRuntime(9362): at android.view.View.performClick(View.java:4128)
10-12 09:25:06.099: E/AndroidRuntime(9362): at android.view.View$PerformClick.run(View.java:17142)
10-12 09:25:06.099: E/AndroidRuntime(9362): at android.os.Handler.handleCallback(Handler.java:615)
10-12 09:25:06.099: E/AndroidRuntime(9362): at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 09:25:06.099: E/AndroidRuntime(9362): at android.os.Looper.loop(Looper.java:213)
10-12 09:25:06.099: E/AndroidRuntime(9362): at android.app.ActivityThread.main(ActivityThread.java:4787)
10-12 09:25:06.099: E/AndroidRuntime(9362): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 09:25:06.099: E/AndroidRuntime(9362): at java.lang.reflect.Method.invoke(Method.java:511)
10-12 09:25:06.099: E/AndroidRuntime(9362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
10-12 09:25:06.099: E/AndroidRuntime(9362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
10-12 09:25:06.099: E/AndroidRuntime(9362): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
getParentFragment()
可能会返回null
。
根据ParentFragment文档,如果DialogFragment
附加到Activity
而不是Fragment
,就会发生这种情况。
答案 1 :(得分:0)
看起来你要在弹出窗口上执行onClick事件后进行回调,我明白了吗?如果是这样,你可以在主片段中添加一个处理程序,并在Dialog Fragment的onClick()方法中向处理程序发送一条消息。
答案 2 :(得分:0)
public static MyHandler myHandler = new MyHandler();
public class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
onDialogOKPressed()
}
}
MyDialogFragment.myHandler.sendEmptyMessage();
如果静态处理程序无法为非静态引用调用onDialogOkPressed()。你可以删除静态并尝试
new MyDialog().sendEmptyMessage();