我正在尝试创建一个DialogFragment,它在解除片段之前按下OK按钮时会运行一些自定义检查。
这是我的代码:
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
public class Contact extends DialogFragment implements LoaderCallbacks<Cursor> {
public Bundle koteg;
/** A constant identifier for add operation */
public static final int CONTACT_ADD = 1;
/** A constant identifier for edit operation */
public static final int CONTACT_EDIT = 2;
/** A constant identifier for delete operation */
public static final int CONTACT_DEL = 3;
/** An adapter which holds contacts for the listview */
SimpleCursorAdapter mAdapter;
/** A reference for Contact name */
EditText mTxtNev;
EditText mTxtTelefon;
EditText mTxtEmail;
DatePicker dpSzulDatum;
/** A variable to inflate the layout file contact.xml */
View mContactView;
/**
* A variable to hold contact id, which is required for updation and
* deletion of a contact
*/
int mContactId = 0;
/** Current operation done by this class, default is CONTACT_ADD */
int mContactAction = CONTACT_ADD;
/** A callback method */
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
/** Getting a reference to Name */
mTxtNev = (EditText) mContactView.findViewById(R.id.txt_nev);
/** Getting a reference to Phone */
mTxtTelefon = (EditText) mContactView.findViewById(R.id.txt_telefon);
mTxtEmail = (EditText) mContactView.findViewById(R.id.txt_email);
dpSzulDatum = (DatePicker) mContactView.findViewById(R.id.dpszuldatum);
/** Creating the loader, if it does not exists */
getLoaderManager().initLoader(0, null, this);
};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.contact, container, false);
Button button = (Button) view.findViewById(R.id.btnrendben);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity activity = getActivity();
if (activity != null) {
Toast.makeText(activity, "You clicked OK!", Toast.LENGTH_LONG).show();
}
}
});
return view;
}
/** A callback method */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/**
* Initializes mContactAction and mContactID using the arguments passed
* to this fragment by the function setArguments()
*/
Bundle b = getArguments();
if (b != null) {
if (b.containsKey("contact_action"))
mContactAction = b.getInt("contact_action");
if (b.containsKey("contact_id"))
mContactId = b.getInt("contact_id");
}
/** Creates a dialog window */
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(
getActivity());
/** Inflating the layout file for this dialog window */
mContactView = getActivity().getLayoutInflater().inflate(
R.layout.contact, null);
/** Changing the appearance of the dialog window based on the operation */
switch (mContactAction) {
case CONTACT_ADD:
dialogBuilder.setView(mContactView);
dialogBuilder.setTitle("Add Contact");
String rogzNev = b.getString("rogznev");
// ha az átadott név nem üres...
if (rogzNev.length() > 0) {
Cursor c = FoKepernyo.cDb.getContactByName(rogzNev);
// és ha még nincs ilyen az adatbázisban...
if (c.getCount() == 0) {
mTxtNev = (EditText) mContactView
.findViewById(R.id.txt_nev);
mTxtNev.setText(nevNagybetusre(rogzNev));
}
}
String szuldatum = "1970.06.10";
StringTokenizer tokens = new StringTokenizer(szuldatum, ".");
int year = Integer.parseInt(tokens.nextToken());
int month = Integer.parseInt(tokens.nextToken()) - 1;
int day = Integer.parseInt(tokens.nextToken());
dpSzulDatum = (DatePicker) mContactView
.findViewById(R.id.dpszuldatum);
dpSzulDatum.updateDate(year, month, day);
break;
case CONTACT_EDIT:
dialogBuilder.setView(mContactView);
dialogBuilder.setTitle("Személy szerkesztése");
break;
case CONTACT_DEL:
dialogBuilder.setTitle("Személy törlése");
dialogBuilder.setMessage("Are you sure to delete?");
break;
}
return dialogBuilder.create();
}
/** A callback method invoked by the loader when initLoader() is called */
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
Bundle b = getArguments();
/** Requesting for the contact name and phone to be edited */
if (mContactAction == CONTACT_EDIT) {
Uri uri = ContactsProvider.CONTENT_URI;
String pathSegment = Integer.toString(mContactId);
uri = Uri.withAppendedPath(uri, pathSegment);
return new CursorLoader(getActivity(), uri, null, null, null, null);
} else {
return null;
}
}
/**
* A callback method, invoked after the requested content provider returned
* all the data
*/
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor data) {
/** populates Contact Name and Phone on edit operation */
if (data.moveToFirst()) {
mTxtNev = (EditText) mContactView.findViewById(R.id.txt_nev);
/** Getting a reference to Phone */
mTxtTelefon = (EditText) mContactView.findViewById(R.id.txt_telefon);
mTxtEmail = (EditText) mContactView.findViewById(R.id.txt_email);
dpSzulDatum = (DatePicker) mContactView.findViewById(R.id.dpszuldatum);
mTxtNev.setText(data.getString(1));
mTxtTelefon.setText(data.getString(2));
mTxtEmail.setText(data.getString(3));
String szuldatum = data.getString(4);
StringTokenizer tokens = new StringTokenizer(szuldatum, ".");
int year = Integer.parseInt(tokens.nextToken());
int month = Integer.parseInt(tokens.nextToken()) - 1;
int day = Integer.parseInt(tokens.nextToken());
dpSzulDatum.updateDate(year, month, day);
}
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
}
public static String nevNagybetusre(String nev) {
boolean cap = true;
char[] out = nev.toCharArray();
int i, len = nev.length();
for (i = 0; i < len; i++) {
if (Character.isWhitespace(out[i])) {
cap = true;
continue;
}
if (cap) {
out[i] = Character.toUpperCase(out[i]);
cap = false;
}
}
return new String(out);
}
public static boolean checkNev(String nev) {
boolean eredmeny = false;
if (nev.length() > 2) {
eredmeny = true;
}
return eredmeny;
}
public static boolean checkEmail(String email) {
String regExpn = "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
+ "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+ "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(regExpn, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches())
return true;
else
return false;
}
public static boolean checkTelefon(String telefon) {
final String phoneNumPattern = "^(?=.*[0-9])$";
Pattern pattern = Pattern.compile(phoneNumPattern);
Matcher matcher = pattern.matcher(telefon);
if (matcher.matches())
return true;
else
return false;
}
public static boolean checkSzulDatum(String szuldatum) {
StringTokenizer tokens = new StringTokenizer(szuldatum, ".");
int year = Integer.parseInt(tokens.nextToken());
int month = Integer.parseInt(tokens.nextToken()) - 1;
int day = Integer.parseInt(tokens.nextToken());
Calendar most = Calendar.getInstance();
Calendar datum = most;
datum.set(Calendar.YEAR, year);
datum.set(Calendar.MONTH, month);
datum.set(Calendar.DAY_OF_MONTH, day);
if (datum.compareTo(most) == -1)
return true;
else
return false;
}
}
现在我得到的是以下内容(在FragmentTansaction.commit()之后):
01-22 12:34:42.572: W/dalvikvm(1247): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-22 12:34:42.592: E/AndroidRuntime(1247): FATAL EXCEPTION: main
01-22 12:34:42.592: E/AndroidRuntime(1247): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
01-22 12:34:42.592: E/AndroidRuntime(1247): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
01-22 12:34:42.592: E/AndroidRuntime(1247): at com.android.internal.app.AlertController.installContent(AlertController.java:199)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.app.AlertDialog.onCreate(AlertDialog.java:251)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.app.Dialog.show(Dialog.java:225)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:385)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.os.Handler.handleCallback(Handler.java:587)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.os.Handler.dispatchMessage(Handler.java:92)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.os.Looper.loop(Looper.java:123)
01-22 12:34:42.592: E/AndroidRuntime(1247): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-22 12:34:42.592: E/AndroidRuntime(1247): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 12:34:42.592: E/AndroidRuntime(1247): at java.lang.reflect.Method.invoke(Method.java:507)
01-22 12:34:42.592: E/AndroidRuntime(1247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-22 12:34:42.592: E/AndroidRuntime(1247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-22 12:34:42.592: E/AndroidRuntime(1247): at dalvik.system.NativeStart.main(Native Method)
我甚至没有在我的代码中使用requestFeature()。我哪里出错了?
抱歉我的英文!
提前致谢!