package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Why did it say hello world?");
setContentView(R.layout.main);
}
}
I'm following the Hello, World tutorial on the android SDK site. When I set the text for tv, it never shows. Instead, the output is:
Hello World, HelloAndroid!
Where in the world is that coming from? I never wrote that text, ever, anywhere...freaky.
TextView tv = new TextView(this); << create TextView object
tv.setText("Why did it say hello world?"); << set text to it
setContentView(R.layout.main); << display the view defined in main.xml
as scriptocalypse said you can simply set the content view to tv, alternatively, and I think a better approach is to use the main.xml:
in main.xml there is a TextView and it has a property called id, it looks something like this: <TextView android:id="#+id/text" you can use this id to get the TextView object of this xml like this:
setContentView(R.id.xml);
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("Your text");
There are 3 things to remember:
R.id.text is corresponding to android:id="#+id/text".
R.id.text is an integer, generated during the build of application, it's not a string, and cannot be manipulated as a string.
setContentView has to be called before calling findViewById, otherwise, you will get null as return value of findViewById.
Check your main.xml file. It's likely being printed from there.
To get your own test there, add your new TextView to the main.xml
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Your text here" />
(Note: the "Your text here" should really be brought out to the strings.xml file.)
To put in an editable text field, use an EditText field.
<EditText android:id="#+id/TextId" android:hint="background text"
android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText>
You should use setContentView before creating a new TextView object. Also, did you remember to add the TextView to the display?
setContentView(tv)
I see you adding the xml layout, but you never add the newly created TextView
You are calling setContentView() and passing in a reference to a layout, specifically main.xml as noted by Haphazard. If you want to see your TextView that you just created, you should be able to pass the variable tv into the setContentView() method.
The default main.xml has a reference to a String in strings.xml in your res/ folder. In there is the definition of a String "Hello World", which is why you are seeing "Hello World".
Related
I am totally new at Android Studio.
I am trying to write simple application with 2 screens and passing info from screen 1 to screen 2.
This is the code of the Second activity.
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
if(getIntent().hasExtra("com.gmail.zionrevi.SOMETHING")) {
String text = getIntent().getExtras().get("com.gmail.zionrevi.SOMETHING").toString();
TextView tv = (TextView)findViewById(R.id.textview);
tv.setText(text);
}
}
}
when i am running it i am getting the following Run Time exception:
E/AndroidRuntime: FATAL EXCEPTION: main
android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView
on the following line
TextView tv = (TextView)findViewById(R.id.textview);
What i am doing wrong?
can someone help me please?
You are trying to cast ConstraintLayout into TextView which cannot succeed. They are two completly different view.
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/textView"
android:layout_height="match_parent">
You need to set TextView in you xml layout or change ConstraintLayout id to some other id and set TextView id to textView
Part of learning Android is understanding the errors. When you're told that typeX cannot be cast to typeY it almost always means you're attempting to cast something to the wrong type.
In this case, it appears to be occurring here TextView tv = (TextView)findViewById(R.id.textview);. When you do something such as (TextView)findViewById(R.id.textview), you're casting whatever is returned by findViewById(...) to a TextView. If that ID you pass to the function is not a TextView, you will get a ClassCastException
In your case, very likely, you have a ConstrainLayout in your .xml file for this activity that has an ID of textview.
welcome at android programming ..
i think that your R.id.textview refers to [ConstraintLayout]
you should set your [textview] id to be [#+id/textview] and of course clear it from yourConstraintLayout .. and it will be
solved :)
happy programming
I'm trying to change my TextView text from the code.
This is what my xml looks like:
XML:
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal" />
And the code:
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
setContentView(tv1);
I'm getting an error on my device and the application stops.
I tried to show a TextView (not connected to an XML TextView) and it worked.
Your approach is incorrect. I think it will be Null Pointer Exception (next time post log cat)
You need to specify the layout you are using first, before finding views.
Java:
// Specify the layout you are using.
setContentView(R.layout.yourlayout);
// Load and use views afterwards
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
Kotlin:
// Specify the layout you are using.
setContentView(R.layout.yourlayout)
// Load and use views afterwards
val tv1: TextView = findViewById(R.id.textView1)
tv1.text = "Hello"
Click Here to study exactly you want to know
remove this.. setContentView(tv1);
I got the same problem. My app was stopping too.
Actually, I was writing the code outside of the function/method. So to fix this problem, these lines
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
must be inside a function/method. (can be user defined)
(I am new to android studio so I don't know the reason behind the problem but I only know how to fix this. Maybe this helps new ones despite this question being 8 years old.)
Why can't I use setContentView(R.layout.main) in last line without setContentView(tv)? Please explain this to me.
package com.mue.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorldActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android i am suvankar");
setContentView(tv);
}
}
You have to define the textview in the R.layout.main ( a xml file) this file contains the information of the objects in the activity.
if you are using eclipse you can simple drag and drop a textview, just open the main file.
(folder res -> layout -> main.xml)
then yo have to call it in your program:
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.tv); //<-- yo have to use the same ID that is in the main.xml file
and then you can set the text. all of this in the oncreate function.
tv.setText("Hello, Android i am suvankar");
Well, I hope I've helped. is my first response here
You shouldn't call setContentView() twice in your onCreate. Either call setContentView(R.layout.main) or call setContentView(tv) but not both. I would prefer the first of the two... but you need to make sure the TextView is declared in your layouts XML.
Lets first try to understand what setContentView() method does. Basically setContentView() places your UI on your Activity. Now to create a UI component for your Activity, you can either use xml resource (e.g. R.layout.main) or you can obtain an instance of the UI component in your code and dynamically add it to your Activity. For example
TextView tv = new TextView(this);
tv.setText("Hello, Android i am suvankar");
setContentView(tv);
In your case you created an instance of the TextView tv, set some text to it and added that to your Activity. Here you dont need to use setContentView(R.layout.main). However if you have a xml layout (main.xml) in your layouts folder like the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World"
/>
</LinearLayout>
and at the bottom of your application you write setContentView(R.layout.main), your should see a black screen with Hello World written on it. This is because here you created a TextView instance, set some text to it, but did not place it to your activity by calling
setContentView(tv), rather you added a completely different layout resource. If you use setContentView(tv) and at the end of your onCreate() you add setContentView(R.layout.main) then again you will see Hello World instead of "Hello, Android i am suvankar" because at the end you replaced your UI resource. However, if you forget to add your xml resource and call 'setContentView(R.layout.main)' compiler will issue an error for not finding the xml resource specified.
The setText() method returns null in my application why?
public class GetValue extends Activity {
char letter = 'g';
int ascii = letter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView = (TextView)findViewById(R.id.txt_1);
textView.setText(ascii);
}
}
It doesn't matter what text i put in, it crashes anyway. Why does setText() keep returning null?
Thank you, in advance
Solution: My error was in the xml file. I wrote:
android:text="#+id/txt_1"
When it should say:
android:id="#+id/txt_1"
Thanks a lot for all the answers and comments!
You tried to pass an integer as parameter to setText, which assumes it is a resource ID. To display computed text, pass it as a string: textView.setText("g");
Edited: Check your XML file, I have test with something very basic and it works
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/txt_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="myTextView"/>
</LinearLayout>
Maybe try to clean your project (Project->Clean in Eclipse), I recently have some trouble with R generation on the last ADT version.
Try this:
textView.setText(Integer.toString(ascii));
Also make sure that your layout xml has TextView txt_1
I've tried :
Integer.toString(char) and String.valueOf(char)
both didnt work.
The only solution is :
txt.setText(""+char);
This is not very efficient from optimization point of view but it does the trick :)
Is there anywhere that I can find documentation on the scope of the XML files? I have an app I am currently working on and have been struggling with getting a feature to work and it seems that the problem I am having is that I am trying to access an element in an XML file that must be out of scope. To simplify the layout, my project has main.xml, sub.xml, main.java, and sub.java files in it. As you can probably guess, main.java works with main.xml and sub.java is working with the elements in sub.xml. Here's where the issue comes in, I have a TextView element that is created in main.xml that I would like to modify the text in, but the action that would trigger it will occur in sub.java. I can't figure out how to change it from sub.java, and I can't figure out how to move the element into sub.xml. The code I am using is pretty simple:
TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText(filePath);
I get a FC every time I run the app, but if I move the code into main.java, it runs flawlessly. If anyone can offer any ideas, or point me in the direction of some documentation that would explain what java files can access what elements in which xml files, that would be awesome! Sorry for the novel, but I'm just struggling to get the point across. Thanks.
try like this Bryan in Main.xml file it works with no issue...........Declare first & then Initialize it...
public class Main extends Activity {
static TextView tv;
static Button submit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
tv = (TextView) findViewById(R.id.header_text1);
}
}
Activity.findViewById(int) only works if that view is in the activity's layout. So no, you can't refer to a view in main.xml because that layout doesn't apply to sub.
Do you have any TextViews in sub.xml called myTitle?
You can access the the textview of main.java(main.xml) in submain.java as follows
In main.java write the following code
static TextView titleText = (TextView) findViewById(R.id.myTitle);
titleText.setText(filePath);
and u can access this submain.java as
Main.titleText.setText(filePath);