0

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

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

    But this textView is in another xml-file.

    the compiler outputs

  2. 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) 
    
Nivi dimka
  • 85
  • 8

2 Answers2

0

There's no textview with id R.id.textView3, so findViewById returns null. Are you sure you've called setContentView, or the layout is attached to your activity?

UtsavShah
  • 857
  • 1
  • 9
  • 20
0

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");
Blue_Alien
  • 2,148
  • 2
  • 25
  • 29