我正在尝试一个带有真正可重用代码的新对话框。 但是我在dialogOnClick& Click上得到一个Nullpointer异常错误 这是logCat:
package at.bomsbg;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import at.bomsbg.MindX.MindXconstants;
/**
* @author asteidl
*
*/
public final class YNNfragmentDialog extends DialogFragment implements DialogConstants, MindXconstants{
private final String LOG = this.getClass().getSimpleName();
private static final String EXTRA_CHOOSE = "CHOOSE";
static private int mRequest_tag;
private static String mTitle;
private static String mMessage;
private Drawable mImage;
private static boolean mCancelable;
private static YNNinterface mTarget;
public static final String TAG = YNNfragmentDialog.class.getSimpleName();
public static YNNfragmentDialog newInstance(Context tobjContext,
int tintrequest_tag,
String tstrTitle,
String tstrMessage,
Boolean tbolCancelable,
YNNinterface tobjTarget) {
mRequest_tag = tintrequest_tag;
mTitle = tstrTitle;
mMessage = tstrMessage;
mCancelable = tbolCancelable;
mTarget = tobjTarget;
Bundle args = new Bundle();
// args.putSerializable(EXTRA_CHOOSE, "YesNo");
// args.putSerializable(TAG, "hallo");
YNNfragmentDialog fragment = new YNNfragmentDialog();
fragment.setArguments(args);
return fragment;
}
public YNNfragmentDialog(Context tobjContext,
final int tintrequest_tag,
String tstrTitle,
String tstrMessage,
boolean tbolCancelable,
final YNNinterface target) {
if(DEF_DEBUG) Log.d(LOG, "YNNFragmentDialog with parameters");
}
@Override
// ################################################
public Dialog onCreateDialog(Bundle savedInstanceState){
// ################################################
if (DEF_DEBUG) Log.d(LOG, "onCreateDialog");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
int imageResource = android.R.drawable.ic_dialog_alert;
Drawable image = getResources().getDrawable(imageResource);
builder.setTitle(mTitle)
.setMessage(mMessage)
.setIcon(mImage)
.setCancelable(mCancelable)
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
FragmentManager fm = getActivity().getSupportFragmentManager();
mTarget.onDialogClick(dialog, id, 0, RESULT_POSITIVE); // generate a Nullpointer Exception
dialog.dismiss();
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
FragmentManager fm = getActivity().getSupportFragmentManager();
mTarget.onDialogClick(dialog, id, 0, RESULT_NEGATIVE);
dialog.dismiss();
}
});
builder.setCancelable(mCancelable);
if (mCancelable) {
builder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
mTarget.onDialogClick(null, 0, mRequest_tag, RESULT_NEUTRAL);
arg0.dismiss();
}
});
}
return builder.create();
}
// ################################################
public YNNfragmentDialog() {
// ################################################
if (DEF_DEBUG) Log.d(LOG, "YNNFragmentDiaog with no parameter");
// TODO NO Auto-generated constructor stub
}
}
import android.content.DialogInterface;
public interface YNNinterface {
public void onDialogClick(DialogInterface dialog, int id, int request_tag, int retValue);
}
package at.bomsbg.MindX;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import at.bomsbg.DialogConstants;
import at.bomsbg.YNNfragmentDialog;
import at.bomsbg.YNNinterface;
public class testCodeFragment extends Fragment implements DialogConstants, MindXconstants {
private final String LOG = this.getClass().getSimpleName();
public static final String EXTRA_CARD_ID = "at.bomsbg.card_id";
private static final String DIALOG_CHOOSE = "Choose";
private static final int REQUEST_CHANGE = 0;
private Button startBtn;
public static testCodeFragment newInstance(int myID){
Bundle args = new Bundle();
args.putSerializable(EXTRA_CARD_ID, myID);
testCodeFragment fragment = new testCodeFragment();
fragment.setArguments(args);
return fragment;
}
// ################################################
public void onCreate(Bundle savedInstanceState) {
// ################################################
if(DEF_DEBUG) Log.d(LOG, "onCreate");
super.onCreate(savedInstanceState);
}
// ################################################
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedStateInstance) {
// ################################################
if(DEF_DEBUG) Log.d("testCodeFragment", "onCreateView called");
View v = inflater.inflate(R.layout.testcode, parent, false);
startBtn = (Button) v.findViewById(R.id.button1);
startBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
doMethode();
}
});
return v;
}
// ################################################
private void doMethode() {
// ################################################
if(DEF_DEBUG) Log.d(LOG, "doMethode");
callDialog(PICK_YN);
}
// ################################################
public void callDialog(int pintRequestCode) {
// ################################################
if(DEF_DEBUG) Log.d(LOG, "callDialog");
int pintDialogType = PICK_YN;
String pstrTitle = "Title of Dialogbox";
String pstrMsg = "show a MessageDetail";
boolean pbolCancelable = true;
YNNinterface pbolInterface = null;
// call Dialog with returnValue
FragmentManager fm = getActivity().getSupportFragmentManager();
YNNfragmentDialog dialog = YNNfragmentDialog.newInstance(getContext(), pintDialogType, pstrTitle, pstrMsg, pbolCancelable, pbolInterface);
dialog.setTargetFragment(testCodeFragment.this, REQUEST_CHANGE);
dialog.show(fm, DIALOG_CHOOSE);
// dialog.dismiss(); not here !!!
}
@Override
// ################################################
public void onActivityResult(int requestCode, int resultCode, Intent data){
// ################################################
if(DEF_DEBUG) Log.d(LOG, "onActivityResult");
if (resultCode != Activity.RESULT_OK) return;
if (requestCode == REQUEST_CHANGE) {
// Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
// if (date == null) {
// date = (Date) data.getSerializableExtra(TimePickerFragment.EXTRA_TIME);
}
// mCrime.setDate(date);
// updateDate();
}
// ################################################
public void onDialogClick(DialogInterface tobjialog, int tintID, int tint_request, int tint_Value) {
// ################################################
// TODO NO Auto-generated method stub
// public void onDialogClick(DialogInterface dialog, int id, int request_tag, int retValue);
// YNNfragmentDialog(Context tobjContext, final int tintrequest_tag, String tstrTitle,
// String tstrMessage, boolean tbolCancelable, final YNNinterface target) {
// YNNfragmentDialog myDialog = new YNNfragmentDialog();
Log.d(LOG, "onDialogClick from testcode wtih return VAlue" + Integer.toString(tint_Value));
}
}
public interface DialogConstants {
public static final String DIALOG_WARN = "WARNING";
public static final String DIALOG_YN = "YESNO";
public static final String SIALOG_INFO = "INFO";
public static final String DIALOG_YNC= "YESNOCANCEL";
public static final String DIALOG_ANSWER = "extra_answer";
public static final String DIALOG_TITLE = "extra_title";
public static final String DIALOG_MESS = "extra_mess";
public static final String DIALOG_DATE = "DATE";
public static final String DIALOG_TIME = "TIME";
public static final String DIALOG_DATETIME = "DATETIME";
public static final int RESULT_OK = 0;
public static final int RESULT_POSITIVE = 2;
public static final int RESULT_NEGATIVE = 3;
public static final int RESULT_NEUTRAL = 4;
public static final String STATE_RECNO = "recno";
// Requests
public static final int TAG_DELETE = 11;
public static final int TAG_DELETE_REC = 12;
public static final int TAG_RESET = 13;
public static final int TAG_RESET_TEST = 14;
public static final int TAG_EXIT = 19;
public static final int PICK_OK = 20;
public static final int PICK_YN = 21;
public static final int PICK_DATE = 22;
public static final int PICK_TIME = 23;
public static final int PICK_DATEWTIME = 24;
public static final int PICK_QUESTION_IMAGE = 91;
public static final int PICK_ANSWER_IMAGE = 92;
}
成功后,我将发布dialogBox的整个解决方案。