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 answersHello 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");更多推荐
发布评论