Android 使用带有接口侦听器的DialogFragment将结果返回给父活动时发生IllegalStateException
我正在尝试使用support Library中定义的DialogFragment添加登录对话框(我必须使用它来支持旧设备)。我可以使用Google页面上的例子:创建这样一个带有自定义布局的登录对话框。当我将所有内容放在一起时,Eclipse中的所有内容看起来都太匹配了,但是当我尝试运行它时,我立即得到一个非法状态异常。它甚至没有给我一个要看的行号。有人能帮我找到什么原因吗?谢谢 从源文件夹:Android 使用带有接口侦听器的DialogFragment将结果返回给父活动时发生IllegalStateException,android,android-support-library,illegalstateexception,dialogfragment,Android,Android Support Library,Illegalstateexception,Dialogfragment,我正在尝试使用support Library中定义的DialogFragment添加登录对话框(我必须使用它来支持旧设备)。我可以使用Google页面上的例子:创建这样一个带有自定义布局的登录对话框。当我将所有内容放在一起时,Eclipse中的所有内容看起来都太匹配了,但是当我尝试运行它时,我立即得到一个非法状态异常。它甚至没有给我一个要看的行号。有人能帮我找到什么原因吗?谢谢 从源文件夹: public class LoginDialogFragment extends DialogFragm
public class LoginDialogFragment extends DialogFragment {
public interface LoginDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
LoginDialogListener mListener;
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (LoginDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.login_dialog, null))
// Add action buttons
.setPositiveButton("Login", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(LoginDialogFragment.this);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// LoginDialogFragment.this.getDialog().cancel();
// Send the negative button event back to the host activity
mListener.onDialogNegativeClick(LoginDialogFragment.this);
}
});
return builder.create();
}
}
public class UsingPreferencesActivity
extends FragmentActivity
implements LoginDialogFragment.LoginDialogListener{
protected void onCreate(Bundle savedInstanceState){
this.showLoginDialog();
}
public void showLoginDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new LoginDialogFragment();
dialog.show(getSupportFragmentManager(), "LoginDialogFragment");
}
public void onDialogPositiveClick(DialogFragment dialog) {
Toast toast = Toast.makeText(getBaseContext(), "OK", Toast.LENGTH_LONG);
toast.show();
}
public void onDialogNegativeClick(DialogFragment dialog) {
Toast toast = Toast.makeText(getBaseContext(), "Cancel",
Toast.LENGTH_LONG);
toast.show();
}
public void onClickLoad(View view){
Intent i = new Intent("com.example.homework09.AppPreferenceActivity");
startActivity(i);
}
public void onClickDisplay(View view){
SharedPreferences appPrefs =
getSharedPreferences("com.example.homework09_preferences",MODE_PRIVATE);
DisplayText(appPrefs.getString("editTextPref", ""));
}
public void onClickModify(View view){
SharedPreferences appPrefs =
getSharedPreferences("com.example.homework09_preferences", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = appPrefs.edit();
prefsEditor.putString("editTextPref",
((EditText)findViewById(R.id.etNewUsername)).getText().toString());
prefsEditor.commit();
DisplayText(appPrefs.getString("editTextPref", ""));
}
public void onClickReset(View view){
SharedPreferences appPrefs =
getSharedPreferences("com.example.homework09_preferences", MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = appPrefs.edit();
prefsEditor.putString("editTextPref", " ");
prefsEditor.commit();
DisplayText(appPrefs.getString("editTextPref", ""));
}
private void DisplayText(String str){
Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
TextView textView = (TextView)findViewById(R.id.tvUsername);
textView.setText(str);
}
}
public class AppPreferenceActivity extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.myapppreferences);
}
}
从res/layout/login_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="[Enter username here]"/>
<CheckBox
android:id="@+id/input_chkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save Username?"
android:defaultValue="false"
/>
</LinearLayout>
从res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnPreferences"
android:text="Show Preference Screen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickLoad"
/>
<Button
android:id="@+id/btnDisplayValues"
android:text="Display Username Preference"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickDisplay"
/>
<Button
android:id="@+id/btnResetValues"
android:text="Reset Username Preference"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickReset"
/>
<EditText
android:id="@+id/etNewUsername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter New Username Here."
/>
<CheckBox
android:id="@+id/chkBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save Username as Preference?"
></CheckBox>
<Button
android:id="@+id/btnModifyValues"
android:text="Update Username Preference"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickModify"
/>
<TextView
android:id="@+id/tvUsername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text=" "
android:hint="[See Username Preference Here]"
/>
</LinearLayout>
从xml/myapppreferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:title="Save Username?"
android:defaultValue="false"
android:summary="Yes or No"
android:key="checkboxPref"
/>
<EditTextPreference
android:summary="Enter Username"
android:defaultValue="[Enter Username]"
android:title="Username"
android:key="editTextPref"
/>
</PreferenceScreen>
我发现了问题。在清理一些旧代码的过程中,我意外地从活动的onCreate方法中删除了setContentView语句,因此我的应用程序的主活动没有指定布局/视图。这就是导致错误的原因。因此,它与对话框片段等无关。一旦我将语句添加回onCreate(),如下所示,它就可以正常工作了:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState); // added - fixed problem
this.setContentView(R.layout.main); // added - fixed problem
this.showLoginDialog();
}