2014-11-25 88 views
0

我想:如何获得textview的值?

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

,但在第二行落在错误:

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) 

而且,我试图

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

但在这种情况下, “findViewByID” 错误红色亮点”无法解析findViewById(int)方法“。我知道类必须从Activity类继承,但是我的类从另一个类继承而来。如何获得textview的价值?

+0

你把这段代码放在setContentView之前吗? – 2014-11-25 11:37:07

+0

显示您的xml代码布局 – Suvitruf 2014-11-25 11:37:10

+1

请发布您的完整代码。 – 2014-11-25 11:37:11

回答

0

如果您正在使用getActivity,则您在片段内部,并且该视图可能只是片段本身的一部分。

变化

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

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

,如果你使用的是行后onCreateView

0

我认为你正在使用的片段。在onCreateView写

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

希望这会工作。 :)

0

我想得到一个在TextView中的值。这个决定很简单:

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

谢谢大家。