我将警报和对话框放在一个单独的类中以防止混乱。在我的活动中,我有一个webView,HTML文档有一个按钮,可以在Android中执行自定义对话框。我正在使用JavaScriptInterface在HTML Documents和Android Java方法中的JavaScript函数之间进行通信。除了这个自定义对话框之外,这一切都适用于我的toast消息和彼此之间的其他功能。
更新:
我做了一些改变,现在至少我得到了一个我无法遵循的NPE。我将Dialog方法移动到JavaInterface,以便更容易调试和其他一些chages;请参阅代码中的注释。
我不知道我错了什么。我在LogCat中没有得到任何错误信息;只是强制关闭应用程序...... ?? Plz通过我的代码查看。
Thnx的帮助!!
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)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.nativeTouchUp(Native Method)at android.webkit.WebViewCore.access $ 3600(WebViewCore.java:52)at android.webkit.WebViewCore $ EventHub $ 1.handleMessage(WebViewCore.java:1340) 在android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.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>
答案 0 :(得分: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”与您的活动有关,则Dialog无论如何都无法找到此视图。
答案 1 :(得分: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);
}