我想獲取DialogFragment中的值:在EditText中輸入一個值並在Fragment中更改TextView中的值。Android DialogFragment從getActivity()中拋出ClasscastExeption
我的片段
public class MyPageActivity extends Fragment implements MyPageNicknameDialog.onNicknameListener{
...
@Override
public void setNicknameListener(String arg){
mypage_nickname_text.setText(arg);
}
}
對話框
public class MyPageNicknameDialog extends DialogFragment{
...
public interface onNicknameListener{
void setNicknameListener(String arg);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
dialogView = inflater.inflate(R.layout.dialog_nickname_mypage, container, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
nickname_dialog_edittext = (EditText)dialogView.findViewById(R.id.nickname_dialog_edittext);
nickname_dialog_edittext.requestFocus();
nickname_dialog_edittext.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// Here thorws ClasscastExeption
onNicknameListener activity = (onNicknameListener) getActivity();
activity.setNicknameListener(nickname_dialog_edittext.getText().toString());
getDialog().dismiss();
return false;
}
});
//init();
return dialogView;
}
}
日誌
05-21 22:48:42.725: E/AndroidRuntime(17131): java.lang.ClassCastException: com.hhh.kiznic.MainFragmentActivity cannot be cast to com.hhh.kiznic.MyPageNicknameDialog$onNicknameListener
05-21 22:48:42.725: E/AndroidRuntime(17131): at com.hhh.kiznic.MyPageNicknameDialog$1.onEditorAction(MyPageNicknameDialog.java:54)
05-21 22:48:42.725: E/AndroidRuntime(17131): at android.widget.TextView.onEditorAction(TextView.java:4973)
05-21 22:48:42.725: E/AndroidRuntime(17131): at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
05-21 22:48:42.725: E/AndroidRuntime(17131): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:297)
05-21 22:48:42.725: E/AndroidRuntime(17131): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
05-21 22:48:42.725: E/AndroidRuntime(17131): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 22:48:42.725: E/AndroidRuntime(17131): at android.os.Looper.loop(Looper.java:137)
05-21 22:48:42.725: E/AndroidRuntime(17131): at android.app.ActivityThread.main(ActivityThread.java:5493)
05-21 22:48:42.725: E/AndroidRuntime(17131): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 22:48:42.725: E/AndroidRuntime(17131): at java.lang.reflect.Method.invoke(Method.java:525)
05-21 22:48:42.725: E/AndroidRuntime(17131): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
05-21 22:48:42.725: E/AndroidRuntime(17131): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
05-21 22:48:42.725: E/AndroidRuntime(17131): at dalvik.system.NativeStart.main(Native Method)
我找到很多例子,但未能得到解決。如果你知道原因和解決方案,請幫助我。
謝謝這麼多先生! :) – user3552715