2014-11-08 17 views
2

我有一個很簡單的用例:價值的EditText的片段中不刷新

  • 我已經定義了一個按鈕和其片段的內側一個EditText。
  • 我已經將該按鈕操作綁定到片段活動中的方法。
  • 我想從方法

我的操作方法(在片段)更新的EditText的文字如下:

public void notifyLaterAction(View view) { 
    EditText editText = (EditText)findViewById(R.id.nextNotificationTime); 
    editText.setText("Gotcha"); 
    String theText = editText.getText().toString(); 
    Log.d(LOG_PATH, theText); 
} 

運行,theText是後「疑難雜症」,但用戶界面不會更新。誰能告訴我這裏發生了什麼?

編輯:額外的代碼

這裏更新是在片段中定義的按鈕:

<Button 
    android:id="@+id/notifyLaterButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal|top" 
    android:onClick="notifyLaterAction" 
    android:text="Ask Me Later" /> 

下面是完整的堆棧跟蹤:

Caused by: java.lang.NoSuchMethodException: notifyLaterAction [class android.view.View] 
    at java.lang.Class.getConstructorOrMethod(Class.java:472) 
    at java.lang.Class.getMethod(Class.java:857) 
    at android.view.View$1.onClick(View.java:3963) 
    at android.view.View.performClick(View.java:4654) 
    at android.view.View$PerformClick.run(View.java:19438) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5487) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NoSuchMethodException: notifyLaterAction [class android.view.View] 
    at java.lang.Class.getConstructorOrMethod(Class.java:472) 
    at java.lang.Class.getMethod(Class.java:857) 
    at android.view.View$1.onClick(View.java:3963) 
    at android.view.View.performClick(View.java:4654) 
    at android.view.View$PerformClick.run(View.java:19438) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5487) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
    at dalvik.system.NativeStart.main(Native Method) 
+0

嘗試以某種方式使視圖無效 – TheRedFox 2014-11-08 16:19:48

+0

請顯示更多代碼。你在哪裏調用該action()方法? – joao2fast4u 2014-11-08 17:24:28

+0

@ joao2fast4u我正在使用XML綁定技術。 – mhlandry 2014-11-08 17:43:49

回答

2

如果你從綁定方法xml,那麼你需要調用這個從活動類這是h在那個佈局。

編輯:以前,我是完全錯誤的,很抱歉指導你錯誤的方向。

1.)如果您已經在xml中定義了onClick,那麼它必須在Activity中。

2.)即使您決定在Activity中有notifyLaterAction(View mView)方法。你不能訪問

EditText editText = (EditText) mView.findViewById(R.id.nextNotificationTime); 

這將是始終爲空,因爲MVIEW僅供按鈕來查看,這樣就不能使用seText和gettext的方法。

3)我們這裏唯一的選擇是聲明和初始化的EditText和Button的片段中,並使用setOnClickListener方法按鈕單擊事件,使裏面的編輯文本的變化。

+1

我正在使用XML綁定方法。我在Android文檔中讀到,您不能使用XML綁定技術將操作綁定到Fragment。當我嘗試這種技術時,我得到:java.lang.NoSuchMethodException。 – mhlandry 2014-11-08 17:43:11

+1

你確定你的佈局和動作(視圖視圖)是在相同的片段?你能告訴我們xml的按鈕屬性嗎? – Chitrang 2014-11-08 17:51:41

+1

檢查我添加到問題的編輯。 – mhlandry 2014-11-11 17:30:46