混乱を防ぐために、アラートとダイアログを別のクラスに保持しています。私のアクティビティでは、webView があり、HTML ドキュメントには、Android でカスタム ダイアログを実行するボタンがあります。JavaScriptInterface を使用して、HTML ドキュメント内の JavaScript 関数と Android Java メソッドの間で通信しています。これは、このカスタム ダイアログを除いて、トースト メッセージと他の機能との間ですべて正常に動作します。
アップデート:
私はいくつかの変更を行いましたが、今では少なくとも従うことができない NPE を取得しています。Dialog メソッドを JavaInterface に移動して、デバッグを容易にし、その他いくつかの変更を行いました。コード内のコメントを参照してください。
何が間違っているのかわかりません。これを使用すると、LogCat にエラー情報が表示されません。アプリを強制終了するだけです...?? 以下のコードをご覧ください。
お世話になりました!!
LogCat:
タッチダウンに対する WebCore の応答を待っているため、ドラッグを逃します。threadid=9: キャッチされない例外 (group=0x4024ee20) で終了するスレッド 致命的な例外: WebViewCoreThread java.lang.NullPointerException
android.app.Activity.findViewById(Activity.java:1745) で com.andaero.JavaScriptInterface.onCreateDialog(JavaScriptInterface.java:45) で android.app.Activity.onCreateDialog(Activity.java:2758) で android.app. Activity.createDialog(Activity.java:936) で android.app.Activity.showDialog(Activity.java:2851) で android.app.Activity.showDialog(Activity.java:2810) で com.andaero.JavaScriptInterface.showDashBoard(JavaScriptInterface) .java:32) で android.webkit.WebViewCore.nativeTouchUp(ネイティブ メソッド) で android.webkit.WebViewCore.nativeTouchUp(ネイティブ メソッド) で android.webkit.WebViewCore.access$3600(WebViewCore.java:52) で android.webkit. android.os.Looper の android.os.Handler.dispatchMessage(Handler.java:99) の WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1340)loop(Looper.java:132) at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:723)
java.lang.Thread.run(Thread.java:1020) で
アラートとダイアログ クラス:
public class AlertsAndDialogs extends Activity
{
public final int CATEGORY_ID = 0;
. . .
//Moved to the JavaScriptInterface Class ----->
/* protected Dialog onCreateDialog(int id)
{
Dialog dialog;
switch (id)
{
case CATEGORY_ID:
AlertDialog.Builder builder;
Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dashboard_dialog, (ViewGroup) findViewById(R.id.webView));
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}*/
}
JavaScriptInterface クラス:
public class JavaScriptInterface extends AlertsAndDialogs
{
public final int CATEGORY_ID = 0;
Context mContext;
/** Instantiate the interface and set the context */
JavaScriptInterface(Context c)
{
mContext = c;
}
. . .
/** Show the DashBoard from the web page */
public void showDashBoard()
{
showDialog(CATEGORY_ID);
}
protected Dialog onCreateDialog(int id)
{
Dialog dialog;
switch (id)
{
case CATEGORY_ID:
AlertDialog.Builder builder;
//Context mContext = this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dashboard_dialog, (ViewGroup) findViewById(R.id.layout_root));
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
. . .
}
WebView を含むメイン アクティビティで:
. . .
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
. . .
HTML 関数:
<script type="text/javascript">
function showDashBoardFunction() {
Android.showDashBoard();
}
</script>