在PreferenceScreen Android中的NullPointerException(NullPointerException In PreferenceScreen Android)

编程入门 行业动态 更新时间:2024-10-25 06:25:42
PreferenceScreen Android中的NullPointerException(NullPointerException In PreferenceScreen Android)

我有一个PreferenceScreen,然后动态创建一个PreferenceCategory,在这个类别中我设置了布局newCategory.setLayoutResource(<PackageName>.R.Layout.myLayout); 。 布局是几个TextView和一个按钮。 当我尝试更改这些TextViews TextView tvTextView = (TextView) findViewById(<PackageName>.R.id.tvTextView);的文本时, TextView tvTextView = (TextView) findViewById(<PackageName>.R.id.tvTextView); tvTextView.setText("Test"); 我得到java.lang.NullPointerException 。 我不知道到底发生了什么,我正在把头发拉出来,这一定是非常简单的我不会得到的。

Log Cat:希望这是我想复制的正确的东西= S.

12-16 23:25:23.450: E/AndroidRuntime(5412): FATAL EXCEPTION: main 12-16 23:25:23.450: E/AndroidRuntime(5412): java.lang.NullPointerException 12-16 23:25:23.450: E/AndroidRuntime(5412): at <PackageName>.GpsLocation$2.onClick(GpsLocation.java:190) 12-16 23:25:23.450: E/AndroidRuntime(5412): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 12-16 23:25:23.450: E/AndroidRuntime(5412): at android.os.Handler.dispatchMessage(Handler.java:99) 12-16 23:25:23.450: E/AndroidRuntime(5412): at android.os.Looper.loop(Looper.java:144) 12-16 23:25:23.450: E/AndroidRuntime(5412): at android.app.ActivityThread.main(ActivityThread.java:4937) 12-16 23:25:23.450: E/AndroidRuntime(5412): at java.lang.reflect.Method.invokeNative(Native Method) 12-16 23:25:23.450: E/AndroidRuntime(5412): at java.lang.reflect.Method.invoke(Method.java:521) 12-16 23:25:23.450: E/AndroidRuntime(5412): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 12-16 23:25:23.450: E/AndroidRuntime(5412): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 12-16 23:25:23.450: E/AndroidRuntime(5412): at dalvik.system.NativeStart.main(Native Method)

第190行有tvTextView.setText("Test"); ,这就是错误发生的地方。

I have a PreferenceScreen and then dynamically create a PreferenceCategory, and on this category I set the layout newCategory.setLayoutResource(<PackageName>.R.Layout.myLayout);. The layout is a couple of TextView's and a button. When I try to change the text of these TextViews TextView tvTextView = (TextView) findViewById(<PackageName>.R.id.tvTextView); tvTextView.setText("Test"); I get java.lang.NullPointerException. I have no idea what the hell is going on, and I am pulling my hair out, it must be something very simple I am not getting.

Log Cat: Hope this is the right stuff I am meant to copy =S

12-16 23:25:23.450: E/AndroidRuntime(5412): FATAL EXCEPTION: main 12-16 23:25:23.450: E/AndroidRuntime(5412): java.lang.NullPointerException 12-16 23:25:23.450: E/AndroidRuntime(5412): at <PackageName>.GpsLocation$2.onClick(GpsLocation.java:190) 12-16 23:25:23.450: E/AndroidRuntime(5412): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 12-16 23:25:23.450: E/AndroidRuntime(5412): at android.os.Handler.dispatchMessage(Handler.java:99) 12-16 23:25:23.450: E/AndroidRuntime(5412): at android.os.Looper.loop(Looper.java:144) 12-16 23:25:23.450: E/AndroidRuntime(5412): at android.app.ActivityThread.main(ActivityThread.java:4937) 12-16 23:25:23.450: E/AndroidRuntime(5412): at java.lang.reflect.Method.invokeNative(Native Method) 12-16 23:25:23.450: E/AndroidRuntime(5412): at java.lang.reflect.Method.invoke(Method.java:521) 12-16 23:25:23.450: E/AndroidRuntime(5412): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 12-16 23:25:23.450: E/AndroidRuntime(5412): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 12-16 23:25:23.450: E/AndroidRuntime(5412): at dalvik.system.NativeStart.main(Native Method)

The line 190 has tvTextView.setText("Test");, that's where the error is happening apparently.

最满意答案

谢谢大家帮忙快速回复,但我想出了一个不同的方法。 谢谢你的时间。

Thanks guys for helping out with speedy replies, but I figured out a different approach. Thanks for your time.

更多推荐

java,AndroidRuntime,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-08-07 05:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459571.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PreferenceScreen   Android   NullPointerException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!