混乱を避けるため、アラートとダイアログを別のクラスに保存します。私の活動では、WebViewがあり、HTMLドキュメントにはAndroidでカスタムダイアログを実行するボタンがあります。 JavaScriptInterfaceを使用して、HTMLドキュメントとAndroid JavaメソッドのJavaScript関数間で通信しています。これは、このカスタムダイアログを除いて、お互いのトーストメッセージや他の機能でうまくいきます。カスタムダイアログ実行NPE
UPDATE:
私はいくつかの変更を行なったし、今、少なくとも私は従うカントNPEを取得します。 DialogメソッドをJavaInterfaceに移動して、デバッグや他のいくつかの変更を容易にしました。コード内のコメントを参照してください。
私は何が間違っているのか分かりません。私はこれとLogCatにエラー情報を得ません。ちょうど強制的にアプリを閉じる...? Plzは下の私のコードを見てください。
Thnxさんのお手伝い!
LogCat:
ミスドラッグ我々はタッチダウンのためのWebCoreの応答を待っているよう。 スレッドID = 9:捕捉されない例外で終了スレッド(グループ= 0x4024ee20) 致命的な例外:WebViewCoreThread java.lang.NullPointerExceptionが com.andaero.JavaScriptInterfaceでandroid.app.Activity.findViewById(Activity.java:1745)で
。 android.app.Activity.showDialog(AT android.app.Activity.createDialog(Activity.java:936)でandroid.app.Activity.onCreateDialog(Activity.java:2758)でonCreateDialog(JavaScriptInterface.java:45) Activity.java:2851)at android.app.Activity.showDialog(Activity.java:2810)at com.andaero.JavaScriptInterface.showDashBoard(JavaScriptInterface.java:32) at android.webkit.WebViewCore.nativeTouchUp(ネイティブメソッド)で android.webkit.WebViewCore $ EventHub $ 1.handleMessage(WebViewCore.java:1340) での でandroid.webkit.WebViewCore.nativeTouchUp(ネイティブメソッド)android.webkit.WebViewCore.access $は3600(WebViewCore.java:52) android.webkit.WebViewCore $ WebCoreThread.run(WebViewCore.java:723)
で android.os.Looper.loop(Looper.java:132)でandroid.os.Handler.dispatchMessage(Handler.java:99) 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クラス:
. . .
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
. . .
HTML:WebViewのを含む主な活動に
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;
}
. . .
}
機能:
<script type="text/javascript">
function showDashBoardFunction() {
Android.showDashBoard();
}
</script>
のthnx。私は試してみましたが、NPEは次のように変更されました: 'android.app.Activity.getVolumeControlStream(Activity.java:4089) \t android.app.Dialog.setOwnerActivity(Dialog.java:206) \t android.app .Activity.onPrepareDialog(Activity.java:2767)android.app.Activity.onPrepareDialog(Activity.java:2796)で \t android.app.Activity.showDialog(Activity.java:2859)アンドロイドで \tで\t。 app.Activity.showDialog(Activity.java:2810) \t com.andaero.JavaScriptInterface.showDashBoard(JavaScriptInterface.java:31) ' – CelticParser
また、ヘルプのThnxでは、ダイアログをより簡単な形式に書き直しました。下の私の答えを参照してください - あなたが示唆したようにそれが動作するためにnullが必要でした。 – CelticParser