0

我想獲取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) 

我找到很多例子,但未能得到解決。如果你知道原因和解決方案,請幫助我。

回答

0

當你打開另一個片段內DialogFragment

dialog.show(getChildFragmentManager(), "dialog"); 

您可以通過獲取父片段getParentFragment()

所以,你的代碼需要是這樣的:

@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; 
    } 
+0

謝謝這麼多先生! :) – user3552715

2

MyPageActivity,其中implements MyPageNicknameDialog.onNicknameListener,是一個片段不是活動。 getActivity返回承載片段的活動。如果它不實現該接口,則引發ClassCastException

0

您的類「MyPageActivity」未定義爲活動,而是定義爲片段。當您使用方法getActivity()時,您會看到承載片段「MyPageActivity」的Activity未執行該界面。您必須在承載「MyPageActivity」的活動中實施界面或更改「MyPageActivity」以擴展活動。

相關問題