在编辑文本中更新 URL

updating URL in edit text

我想在 OnPageStarted() 页面开始加载时更新我的​​ url 但是当我 运行 这些代码时

public class web_client extends WebViewClient {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        EditText editText=view.findViewById(R.id.edit);
        editText.setText(url);
    }
}

我收到以下错误

03-10 12:05:35.022 3385-3385/com.example.mmalishant.mvd E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.mmalishant.mvd, PID: 3385
      java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
      at com.example.mmalishant.mvd.web_client.onPageStarted(web_client.java:35)
      at com.android.webview.chromium.WebViewContentsClientAdapter.onPageStarted(WebViewContentsClientAdapter.java:517)
      at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:144)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6119)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
      03-10 12:05:35.585 1269-1269/? E/EGL_emulation: tid 1269: eglCreateSyncKHR(1669): error 0x3004 (EGL_BAD_ATTRIBUTE)

目前您在

中使用过 view

EditText editText=view.findViewById(R.id.edit);

是方法中的WebView。我确定你的 WebView.

中不能有 EditText

解决方案是您应该 findViewById 在您的 Activity 或 Fragment 的根视图中。

更新:

因为 WebViewClient 是在另一个 Class 和文件中找到的,所以这将起作用:

EditText editText=((YourActivityClassName)view.getContext()).findViewById(R.id.edit);