gpt4 book ai didi

java - 自定义对话框执行 NPE

转载 作者:行者123 更新时间:2023-12-01 15:29:40 24 4
gpt4 key购买 nike

我将警报和对话框放在单独的类中以防止困惑。在我的 Activity 中,我有一个 webView,HTML 文档有一个在 Android 中执行自定义对话框的按钮。我使用 JavaScriptInterface 在 HTML 文档中的 JavaScript 函数和 Android Java 方法之间进行通信。除了这个自定义对话框之外,这一切都可以很好地处理我的 toast 消息和彼此之间的其他功能。

更新:

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

我不知道我做错了什么。我在 LogCat 中没有收到此错误信息;只是强制关闭应用程序...?请查看下面我的代码。

感谢您的帮助!!

日志猫:

Miss a drag as we are waiting for WebCore's response for touch down. threadid=9: thread exiting with uncaught exception (group=0x4024ee20) FATAL EXCEPTION: WebViewCoreThread java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1745) at com.andaero.JavaScriptInterface.onCreateDialog(JavaScriptInterface.java:45) at android.app.Activity.onCreateDialog(Activity.java:2758) at 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) at android.webkit.WebViewCore.nativeTouchUp(Native Method) at 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) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:132) at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:723)
at 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>

最佳答案

尝试设置

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

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

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

关于java - 自定义对话框执行 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9711914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com