我有一个已设置的 Main Fragment 类。在那个主片段中,我有一个 ImageView 并设置了一个 onClick 侦听器。一旦调用了 onClick,它就会打开一个对话框片段。
在我的对话框片段中,我在其中设置了一个简单的“确定”或“取消”选项。单击“确定”按钮后,将其设置为将父活动转换为主片段,并执行在主片段中设置的名为“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();
}
}
日志猫:
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)