0

メイン アクティビティで WebView の現在の URL を取得し、カスタム DialogPreference でユーザーの TextView として表示しようとしています。今の私の困難は、呼び出すたびに null が返されることです。

カスタム DialogPreference のコードは次のとおりです。

public class CustomAlertPreference extends DialogPreference {

String currentURL = "HARDCODED TEST";
public CustomAlertPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setDialogLayoutResource(R.layout.custom_alert_preference);
    setPositiveButtonText(android.R.string.ok);
    setNegativeButtonText(android.R.string.cancel);
}


@Override
protected void onBindDialogView(View view) {
    LayoutInflater layoutInflater = (LayoutInflater)  getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view2 = layoutInflater.inflate(R.layout.activity_main, null);
    WebView webview = (WebView)view2.findViewById(R.id.shopFloorView);
    currentURL = webview.getUrl();
    TextView txview = (TextView) view.findViewById(R.id.URLCurrent);
    txview.setText(currentURL);
}

@Override
public void onClick(DialogInterface dialog, int which) {
    if (which == DialogInterface.BUTTON_POSITIVE) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getContext());
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("prefWMl", currentURL);
        editor.commit();
        Toast toast = Toast.makeText(getContext(), "URL changed to " + currentURL, Toast.LENGTH_SHORT);
        toast.show();
    }
}

WebView が null を返すようにするために、私は彼女に何か間違ったことをしていますか? 私は間違いなくページを完全にロードさせたので、そうではありません.

前もって感謝します、-ジョン

4

1 に答える 1

0

まだ読み込まれていないため、リターンWebViewURL 。null

a を追加しWebViewClientてオーバーライドonPageFinished()し、その URL を正しくキャッチしてみてください。

private String currentURL;

// ...

webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        currentURL = url;
    }
});
webView.loadUrl("an URL");
于 2015-01-09T16:22:10.910 に答える