Android改变xml [重复](Android Changing xml [duplicate])

编程入门 行业动态 更新时间:2024-10-27 23:24:01
Android改变xml [重复](Android Changing xml [duplicate])

这个问题在这里已有答案:

什么是NullPointerException,我该如何解决? 12个答案

您好我想在textView中更改文本,但不知道如何。 我试着像这样改变它

TextView textView3 = (TextView) findViewById(R.id.textView3); textView3.append("hi");

但是这个textView在另一个xml文件中。

编译器输出

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nividimka.shopapplication/com.example.nividimka.shopapplication.ShopActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403) at android.app.ActivityThread.access$600(ActivityThread.java:165) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5391) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.nividimka.shopapplication.ShopActivity.onCreate(ShopActivity.java:62) at android.app.Activity.performCreate(Activity.java:5122) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)  at android.app.ActivityThread.access$600(ActivityThread.java:165)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:194)  at android.app.ActivityThread.main(ActivityThread.java:5391)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:525)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)  at dalvik.system.NativeStart.main(Native Method) 

This question already has an answer here:

What is a NullPointerException, and how do I fix it? 12 answers

Hello I want to change text in my textView , but dont know how. I tryed to change it like this

TextView textView3 = (TextView) findViewById(R.id.textView3); textView3.append("hi");

But this textView is in another xml-file.

the compiler outputs

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nividimka.shopapplication/com.example.nividimka.shopapplication.ShopActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403) at android.app.ActivityThread.access$600(ActivityThread.java:165) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5391) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.nividimka.shopapplication.ShopActivity.onCreate(ShopActivity.java:62) at android.app.Activity.performCreate(Activity.java:5122) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)  at android.app.ActivityThread.access$600(ActivityThread.java:165)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)  at android.os.Handler.dispatchMessage(Handler.java:107)  at android.os.Looper.loop(Looper.java:194)  at android.app.ActivityThread.main(ActivityThread.java:5391)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:525)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)  at dalvik.system.NativeStart.main(Native Method) 

最满意答案

问题不明确,但我相信你正在尝试更新另一个活动/之前的TextView文本。 然后,您可以在实际布局文件(包含textview)膨胀的类中将TextView声明为公共静态

Class PreviousActivity extends Activity{ public static TextView textView3 ; // inside onCreate(), after setContentView() //initialize to avoid null-pointer. textView3 = (TextView) findViewById(R.id.textView3); }

然后你可以尝试从otherActivity。

PreviousActivity.textView3 .append("new txt");

the question is not clear, but i believe you are trying to update the text of a TextView which is on another activity/previous. Then you may declare the TextView as public static in the class where the actual layout file(containing textview) inflated.

Class PreviousActivity extends Activity{ public static TextView textView3 ; // inside onCreate(), after setContentView() //initialize to avoid null-pointer. textView3 = (TextView) findViewById(R.id.textView3); }

then you can try from otherActivity.

PreviousActivity.textView3 .append("new txt");

更多推荐

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

发布评论

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

>www.elefans.com

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