我是Android的初學者,也是Stackoverflow的新手。所以請原諒我這個愚蠢的問題。但是,我發現這裏沒有解決的解決方案,因此將其作爲新問題發佈。webview javascript界面更新Autocompletetextview
我有一個Autocompletetextview和webview的活動。我想單擊webview中的超鏈接來填充Autocompletetextview中的一些文本。所以我在調用Javascript AppInterface的網頁中調用了Javascript。在界面功能中,我想更新Autocompletetextview文本。
我可以從javascript收集文本。但是,在設置Autocompletetextview的文本時,我的應用程序崩潰了。
WebAppInterface.java
public class JsInterface {
@JavascriptInterface
public void NewText(final String text)
{
Log.d("New_Text", "" + text); //Works fine till here.
((Activity)mContext).findViewById(R.id.autoCompleteTextView);
Handler mHandler = new Handler();
mHandler.post(new Runnable() {
@Override
public void run() {
AutoCompleteTextView SearchTextbox = (AutoCompleteTextView) ((Activity)mContext).findViewById(R.id.autoCompleteTextView);
SearchTextbox.setText(text);
}
});
}
}
錯誤的應用程序崩潰
E/AndroidRuntime: FATAL EXCEPTION: JavaBridge
Process: com.test.xxx, PID: 22102
java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
at com.test.xxx.WebAppInterface$1.run(WebAppInterface.java:102)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
我想知道如果我的方法來更新UI組件是正確的? 如果是,哪些更改將解決錯誤,並會正常工作。