Reputation: 5
I want to obtain the value in DialogFragment: enter a value into an EditText and change the TextView in Fragment regarding it.
My Fragment
public class MyPageActivity extends Fragment implements MyPageNicknameDialog.onNicknameListener{
...
@Override
public void setNicknameListener(String arg){
mypage_nickname_text.setText(arg);
}
}
Dialog
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;
}
}
The Log
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)
I was find many examples but could not be resolved. If you know the cause and solution please help me.
Upvotes: 0
Views: 294
Reputation: 1888
When you're opening a DialogFragment inside another Fragment
dialog.show(getChildFragmentManager(), "dialog");
you can get the parent fragment by getParentFragment()
So your code needs to be like this:
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// Here thorws ClasscastExeption
onNicknameListener parentFrag = (onNicknameListener) getParentFragment();
parentFrag.setNicknameListener(nickname_dialog_edittext.getText().toString());
getDialog().dismiss();
return false;
}
Upvotes: 0
Reputation: 404
Your class "MyPageActivity
" is not defined as an Activity but as a Fragment. When you use the method getActivity()
, you get the Activity that is hosting the Fragment "MyPageActivity
" which is not implementing the interface. You must implement the interface in the Activity that is hosting the "MyPageActivity
" or change "MyPageActivity
" to extend an Activity.
Upvotes: 0
Reputation: 157437
MyPageActivity
, which implements MyPageNicknameDialog.onNicknameListener
, is a Fragment not an Activity. getActivity
returns the Activity that hosts the Fragment. If it does not implements that interface, the ClassCastException
is thrown
Upvotes: 2