简体   繁体   中英

How to get the value of a textview?

I tried:

    TextView textView1 = (TextView) getActivity().findViewById(R.id.date);
    String s1 = textView1.getText().toString();

but on the second line falls error:

java.lang.NullPointerException
at com.ancort.cryptophone.guitest.CAMyTest.testMail_000(CAMyTest.java:94)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:177)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1619)

Also, I tried

    TextView textView1 = (TextView)findViewByID(R.id.date);
    String s = textView1.getText().toString();

But in this case "findViewByID" red highlights with error"cannot resolve method findViewById(int)". I understandthat the class must be extends from Activity class, but my class extends from another class. How to get the value of a textview?

you are inside a Fragment if you are using getActivity, and probably, the view, is only part of the fragment itself.

change

 TextView textView1 = (TextView) getActivity().findViewById(R.id.date);

with

 TextView textView1 = (TextView) getView().findViewById(R.id.date);

if you are using that line after onCreateView

I think you are using Fragment. Inside onCreateView write

View view = inflater.inflate(R.layout.your_fragment_layout,container, false);                                                                         
TextView textView1 = (TextView) view.findViewById(R.id.date);  

Hope this will work. :)

I wanted get a value that was in TextView. The decision was simple:

TextView textView1 = (TextView) solo.getView(R.id.date); String s1 =
textView1.getText().toString();

Thank you all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM