How to get the value of a textview? - java

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.

Related

Cannot resolve method setText(java.lang.String)

I've looked at other questions similar to the error I'm getting, but I can't seem to figure out the issue here. All I'm trying to do is click a button and change a TextView's text.
My code is as follows:
public class FindBeerActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_beer);
}
//call when the user clicks the button
public void onClickFindBeer(View view) {
//Get a reference to the TextView
View brands = (TextView) findViewById(R.id.brands);
//Get a reference to the Spinner
Spinner color = (Spinner) findViewById(R.id.color);
//Get the selected item in the Spinner
String beerType = String.valueOf(color.getSelectedItem());
//Display the beers. This is where the error is
brands.setText(beerType);
}
}
and the XML
<TextView
android:id="#+id/brands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/find_beer"
android:layout_alignStart="#id/find_beer"
android:layout_below="#id/find_beer"
android:layout_marginTop="18dp"
android:text="#string/brands" />
When I build I get: "error: cannot find symbol method setText(String)
I'm following a tutorial to the letter and I can't see where I've made a mistake, so I'd appreciate it if you guys could help me out! Thanks!
You can either change
View brands = (TextView) findViewById(R.id.brands);
to
TextView brands = (TextView) findViewById(R.id.brands);
OR change
brands.setText(beerType);
to
((TextView)brands).setText(beerType);
In either ways, you need to call the setText method on a TextView reference.
You are trying setText() method on a view object. You need to call it on a TextView object.so either change the declaration of brands to TextView or just wrap ur brands object to TextView before calling setText() over it.
Change
View brands = (TextView) findViewById(R.id.brands);
To
TextView brands = (TextView) findViewById(R.id.brands);

Android: how to pass multiple values to setContentView() method from an activity class?

I have some problems passing and displaying values from the MainActivityClass to another MyActivityClass. So I learned here how to pass values from one class to another. Here's the method of MainActivity class which contains the intent to the other activtiy:
Intent intent = new Intent(this, AddAptActivity.class);
Bundle extras = new Bundle();
EditText editText = (EditText) findViewById(R.id.edit_address);
String message = editText.getText().toString();
EditText editText2 = (EditText) findViewById(R.id.edit_name);
String message2 = editText2.getText().toString();
extras.putString(EXTRA_MESSAGE, message);
extras.putString(EXTRA_MESSAGE1, message2);
intent.putExtras(extras);
startActivity(intent);
and then in MyActivityClass receiving the values:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String message = extras.getString(MainActivity.EXTRA_MESSAGE);
String message2 = extras.getString(MainActivity.EXTRA_MESSAGE2);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
TextView textView2 = new TextView(this);
textView2.setTextSize(40);
textView2.setText(message2);
setContentView(textView, textView2);
}
but the setContentView() method doesn't want to accept more than one value. How can I display the other value in another textview and then display it???
I think I don't understand how to display values with setContentView... Please help me, as I am new in Android programming (just finished the 1st tutorial one week ago).
You can use a ViewGroup such as a LinearLayout or RelativeLayout then add TextViews to the same and then setContenView(linearlayout);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(this);
TextView textView2 = new TextView(this);
ll.addView(textView);
ll.addView(textView2);
setContenView(ll);
setContentView() takes one Layout, either a View or a layout resource ID. You can pass in a LinearLayout, RelativeLayout, etc. to setContentView() which can contain any number of views. Probably a better approach would be to build a layout XML file that contains your view higherarchy then set it using setContentView(R.layout.someLayout);
see more info here:
http://developer.android.com/guide/topics/ui/declaring-layout.html
You can only add a view to your setContentView(...). Best create a new View that contains both TextView and then setContentView to this new View.
RelativeLayout layout= new RelativeLayout(this);
layout.addView(textView1);
layout.addView(textView1);
setContentView(layout);
Several things wrong here so let's start from the beginning...
but the setContentView() method doesn't want to accept more than one value. There is a method of it which takes a second param but that is for layout params.
Nope, if you look at the docs that's all it takes and you can't force it to take more (without rewriting the activity class).
I think I don't understand how to display values with setContentView.
I don't think so either but you will.
You just set a layout (typically) with this method. You are creating Views dynamically which is ok but you don't need to. If you do then you need to add them to your inflated layout (the one you set in setContentView()). You can just add them to your layout (someLayout.xml) then get a reference to them
So it may look something like
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* call this first and it will save you some grief
*/
where someLayoutFile is the xml file without the .xml extension
setContentView(R.layout.someLayoutFile);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String message = extras.getString(MainActivity.EXTRA_MESSAGE);
String message2 = extras.getString(MainActivity.EXTRA_MESSAGE2);
TextView textView = (TextView) findViewById(R.id.someID); // where someId is the id you give a TextVIew in your xml file
textView.setTextSize(40);
textView.setText(message);
// create another TextView in your xml and do the same with that using the appropriate layout
See this page in the docs for more information

Can't set text to the TextView

I am learning to develop apps for android from a book and after following the instructions I end up with the following. When I run the app the text from the setStartUpScreenText() object is not displayed.
MainActivity.java:
protected void setStartupScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.DataView1);
planetNameValue.setText(earth.planetName);
}
MainActivity.xml
<TextView
android:id="#+id/DataView1"
android:layout_toRightOf="#+id/TextView1"
android:layout_marginLeft="36dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Make sure you call setContentView method before updating any view in the screen.
Make sure you call setStartupScreenText() method after setContentView in oncreate of the activity
Fist of all call the setStartUpScreenText() function from inside of setStartUpWorldValues() function (either in the beginning or in the end). You can also call this function inside the onCreate() methods after the setStartUpWorldValues() function.
Secondly replace the entire code of the setStartUpScreenText() function with the following code: (page 90, Learn Android App Development, publisher: Apress)
TextView planetNameValue = (TextView) findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView) findViewById(R.id.dataView2);
planetMassValue.setText(String.valueOf(earth.planetMass));
TextView planetGravityValue = (TextView) findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView) findViewById(R.id.dataView4);
planetColoniesValue.setText(String.valueOf(earth.planetColonies));
TextView planetPopulationValue = (TextView) findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView) findViewById(R.id.dataView6);
planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
TextView planetBaseValue = (TextView) findViewById(R.id.dataView7);
planetBaseValue.setText(String.valueOf(earth.planetBases));
TextView planetForceFieldValue = (TextView) findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
protected void setStartUpWorldValues() {
earth.setPlanetColonies(1); //Set planet colonies to 1
earth.setPlanetMilitary(1); // set planet military to 1
earth.setColonyImmigration(1000); // set planet population to 1000
earth.setBaseProtection(100); // set Planet armed force to 100
earth.turnForceFieldOn(); // Turn on the planet force field
setStartUpScreenText() ;
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView)findViewById(R.id.dataView2);
planetMassValue.setText(String.valueOf(earth.planetMass));
TextView planetGravityValue = (TextView)findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView)findViewById(R.id.dataView4);
planetColoniesValue.setText(String.valueOf(earth.planetColonies));
TextView planetPopulationValue = (TextView)findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView)findViewById(R.id.dataView6);
planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
TextView planetBasesValue = (TextView)findViewById(R.id.dataView7);
planetBasesValue.setText(String.valueOf(earth.planetBases));
TextView planetForceFieldValue = (TextView)findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
}
private void setStartUpScreenText() {
// TODO Auto-generated method stub
}

Error setting text on TextView in Android

I keep getting a null pointer error, but I can't figure out why. Is there something very obvious I'm missing?
final Dialog d = new Dialog(Start.this);
// dialog.requestWindowFeature((int) Window.FEATURE_NO_TITLE);
d.requestWindowFeature((int) android.view.Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.popuptwo);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
d.show();
d.getWindow().setAttributes(lp);
TextView totaltxt = (TextView) findViewById(R.id.totaltxt);
totaltxt.setText("Test text");
If I remove totaltxt.setText("Test text"); then the program doesn't crash. Ideas?
The text view belongs to the dialog..so you should use the dialog's view..
TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt);
Just do..TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt);
totaltxt.setText("Test text");
Because it unable to get the view for dialog so,
Replace this line--
TextView totaltxt = (TextView) findViewById(R.id.totaltxt);
by this--
TextView totaltxt = (TextView) d.findViewById(R.id.totaltxt);
here d is your dialog object.

android : getting an item from a textview to another textview?

i'm having two layout files. The first layout file containing some data in textview and the another layout containing one empty textview. Now, i want to pass the first layout's textview data to new layout textview when i'm using button in first layout. How can i use this? Thanks in advance.
Basically you can do like this, in button click,get the view of second layout
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.secondlayout, parent, false);
and use below statement to get id of second textview
TextView text2=(TextView)row.findViewById(R.id.SecondTextView)
then get the text in the first textview using id of first one into String:
String s=text1.getText().toString();
then set this String to 2nd textview.
text2.setText(s);
If, textview2 is the textview from second layout. and the
//to get data from text view of first layout use the following code
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.layout1, null);
TextView tv1 = (TextView) vi.findViewById(R.id.textview1);
// to set of first textview in second textview
textview2.setText(tv1.getText().toString

Categories