gpt4 book ai didi

java - 是否可以在Alertdialog中创建GridView?

转载 作者:行者123 更新时间:2023-12-01 23:52:00 25 4
gpt4 key购买 nike

是否可以在警报对话框中创建 GridView?

目前我有一个单词搜索,我想在弹出对话框中显示解决方案(完成的单词搜索),以便用户可以快速在他们的难题和答案之间切换。

我尝试过这样实现

AlertDialog.Builder aBuilder = new AlertDialog.Builder(this);

// set title
aBuilder.setTitle("The solution");
GridView g = null ;
final ArrayAdapter<String> Wordadapter = new ArrayAdapter<String>(getApplicationContext(),
R.layout.wordsstyle, WordsToFind);
g.setNumColumns(Length);
g.setAdapter(Wordadapter);
aBuilder.setView(g);
aBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
}
});

但是它只是崩溃了

我在我的日志猫中得到了这个

04-24 21:17:48.472: E/AndroidRuntime(826): FATAL EXCEPTION: main
04-24 21:17:48.472: E/AndroidRuntime(826): java.lang.IllegalStateException: Could not execute method of the activity
04-24 21:17:48.472: E/AndroidRuntime(826): at android.view.View$1.onClick(View.java:3591)
04-24 21:17:48.472: E/AndroidRuntime(826): at android.view.View.performClick(View.java:4084)
04-24 21:17:48.472: E/AndroidRuntime(826): at android.view.View$PerformClick.run(View.java:16966)
04-24 21:17:48.472: E/AndroidRuntime(826): at android.os.Handler.handleCallback(Handler.java:615)
04-24 21:17:48.472: E/AndroidRuntime(826): at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 21:17:48.472: E/AndroidRuntime(826): at android.os.Looper.loop(Looper.java:137)
04-24 21:17:48.472: E/AndroidRuntime(826): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-24 21:17:48.472: E/AndroidRuntime(826): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:17:48.472: E/AndroidRuntime(826): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:17:48.472: E/AndroidRuntime(826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-24 21:17:48.472: E/AndroidRuntime(826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-24 21:17:48.472: E/AndroidRuntime(826): at dalvik.system.NativeStart.main(Native Method)
04-24 21:17:48.472: E/AndroidRuntime(826): Caused by: java.lang.reflect.InvocationTargetException
04-24 21:17:48.472: E/AndroidRuntime(826): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 21:17:48.472: E/AndroidRuntime(826): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 21:17:48.472: E/AndroidRuntime(826): at android.view.View$1.onClick(View.java:3586)
04-24 21:17:48.472: E/AndroidRuntime(826): ... 11 more
04-24 21:17:48.472: E/AndroidRuntime(826): Caused by: java.lang.NullPointerException
04-24 21:17:48.472: E/AndroidRuntime(826): at com.example.angrywordsearch.Puzzle_Activity.GridPopup(Puzzle_Activity.java:726)
04-24 21:17:48.472: E/AndroidRuntime(826): at com.example.angrywordsearch.Puzzle_Activity.ViewSolution_Click(Puzzle_Activity.java:686)
04-24 21:17:48.472: E/AndroidRuntime(826): ... 14 more

最佳答案

您正在调用 g.setNumColumns(Length),但此时 gnull。因此,LogCat 中会出现 NullPointerException。您需要以某种方式初始化 GridView,可能通过 GridView g = new GridView(this); 来初始化。

关于java - 是否可以在Alertdialog中创建GridView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16202393/

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