0

我将警报和对话框放在单独的班级中,以防止混乱。在我的活动中,我有一个 webView 并且 HTML 文档有一个在 Android 中执行自定义对话框的按钮。我正在使用 JavaScriptInterface 在 HTML 文档中的 JavaScript 函数和 Android Java 方法之间进行通信。除了这个自定义对话框之外,这一切都适用于我的 toast 消息和其他功能。

更新:

我做了一些改变,现在至少我得到了一个我无法遵循的 NPE。我将 Dialog 方法移至 JavaInterface 以使其更易于调试和其他一些更改;请参阅代码中的注释。

我不知道我有什么问题。我在 LogCat 中没有得到这个错误信息;只是强制关闭应用程序...??请看下面我的代码。

谢谢你的帮助!!

日志猫:

当我们等待 WebCore 对着陆的响应时,错过了一个阻力。threadid=9:线程以未捕获的异常退出(组=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) at android.app.Activity.showDialog(Activity.java:2851) at android.app.Activity.showDialog(Activity.java:2810) at com.andaero.JavaScriptInterface.showDashBoard(JavaScriptInterface .java:32) 在 android.webkit.WebViewCore.nativeTouchUp(Native Method) 在 android.webkit.WebViewCore.access$3600(WebViewCore.java:52) 在 android.webkit 的 android.webkit.WebViewCore.nativeTouchUp(Native Method)。 WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1340) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper。在 android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:723) 处循环(Looper.java:132)
在 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 的主 Activity 中:

. . .
myWebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
. . .

HTML 函数:

<script type="text/javascript">
    function showDashBoardFunction() {
        Android.showDashBoard();
    }
</script>
4

2 回答 2

1

尝试设置

View layout = inflater.inflate(R.layout.dashboard_dialog, (ViewGroup) findViewById(R.id.layout_root));

View layout = inflater.inflate(R.layout.dashboard_dialog, null);

我发现将视图附加到容器很少(如果有的话)有效,并且假设“layout_root”与您的活动有关,对话框无论如何都无法找到此视图。

于 2012-03-15T16:29:20.303 回答
0

从 AlertsAndDialogs 类中删除:

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;
      }

并添加:

public static void dashBoard(Context mContext)
    {
    AlertDialog.Builder builder;
    Dialog dbDialog;

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.dashboard_dialog, null);

    builder = new AlertDialog.Builder(mContext);
    builder.setView(layout);
    dbDialog = builder.create();
    dbDialog.show();
    }

在 JavaScriptInterface 类中,将调用替换为:

public void showDashBoard()
    {
    AlertsAndDialogs.dashBoard(mContext);
    }
于 2012-03-15T17:42:37.960 回答