facing problem while displaying the value of the variable - android

I have just tried to display the value of the variable but is showing some kind of exception
that is FORCE CLOSE.
the code i have tried is
TextView myTextView = (TextView) findViewById(R.id.result9);
myTextView.setText("your score is " +count);
considered count=0 intially.
Can any one suggest me for this problem
thanks in advance

if myTexyView is in dialog you have to do this:
myTextView = (TextView) dialog.findViewById();
anyway do clean project...this happens sometimes:
in eclipse: project->clean->your project

Seems findViewById() returns null. It means that either there's no widget with id result9 in current layout or your forgot to setContentView().

can please check your xml which one you have set, setContainView() it mast have TextView with "result9" this id.

Related

TextView setText Cannot resolve type

Im using Android Studio, trying set text to Text View but every time i do this
TextView test = (TextView) findViewById(R.id.testView);
test.setText("fds");
on Android Studio i see Cannot resolve symbol 'setText'.
Can someone tell me what im doing wrong?
setText() will accept String which you have passed, so there is no issue in that.
One possible reason for this might be you have not written your code inside onCreate() or onCreateView().
If it is Activity you need to use these lines after setContentView() in onCreate().
If it is Fragment you need to use these lines after inflating your view in onCreateView().
Second reason, you might be having one more test variable of different type like String or something else

Change TextView text

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.)

Resource ID question

may I know is it possible I use the resource id but I didnt set the Content View in the activity?
for example: abc.xml have one TextView id = "R.id.Text". Can I use the use TextView name = (TextView)findViewById(R.id.Text); method in the activity which was setContentView(R.layout.def);?
P/S: sorry about my bad english, hope you guys understand what I'm talking about.
Thanks
Short answer: no. The code setContentView(R.layout.def); loads only the views from def.xml, not abc.xml. You have to use another method to create the TextView or include it in def.xml.
For another method to use for separate XML files, check out http://developerlife.com/tutorials/?p=303

Setting textview text from a string resource held in an array problem

This should be simple, but driving my crazy.
I have the following in my layout, not problems.
<TextView
android:id="#+id/birdinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00009c"
android:text="The Robin is a popular bird"
/>
Then I have these arrays which are set up with the list of string resources I have
private Integer[] DetailIds = {
R.string.barnaclegoose,
R.string.barnowl,
R.string.bewicksswan,
R.string.blackbird,
R.string.blackcap_male};
So I simply want to do this!
TextView detail = (TextView)findViewById(R.id.birdinfo);
detail.setText(DetailIds[0]);
setContentView(R.layout.main);
But this causes a force close error.
The string resource looks like this ( without header and footer info of course
<string name="barnaclegoose">What a wonderful goose!</string>
Added to this problem is if I use the resource directly to the resource
detail.setText(R.string.barnaclegoose);
For example, I still get a null exception! I'm sure I've done this before, but maybe I'm missing the obvious???
Any ideas appreciated.
( Eclipse, Android 1.5, Emulator with 1.5 )
I realize this is very old, but it came up in a search... Anyways, you need to call setContentView() before findViewById() etc:
setContentView(R.layout.main);
TextView detail = (TextView)findViewById(R.id.birdinfo);
detail.setText(DetailIds[0]);
your problem is for this line setContentView(R.layout.main);
at first you must define this this line then setText your textView
Thanks for the answer. but if you mean R.string.barnaclegoose for example, this is an integer value for the ID pointing to the string itself in the resource.
Anyway, I finally got it working by just creating the view inline instead of using an resource view.
For example
TextView t= new TextView(ctx);
t.setId(2);
t.setTextColor(Color.BLACK);
t.setText(DetailIds[bird]);
mLinearLayout.addView(t,params);
mLinearLayout.setBackgroundColor(Color.WHITE);
setContentView(mLinearLayout);
And that works perfectly.

Getting EditText field from XML

I have an activity in my app, when i call the line
EditText username = (EditText)findViewById(R.id.usernameText);
the app crashes, why is this?
The view has not been inflated, or you are missing
setContentView(R.layout.layout_file);
Yeah either setContentView hasn't been called in onCreate of the activity, or else the view you are trying to get isn't an EditText. What is the Exception you are getting? If its ClassCast then its not an EditText.
Things to check as no more information is provided:
R.id.usernameText is a valid identifier in your layout
Your layout has been set as a content view
Also, if you can edit the post and add the log of what's messing up, it will be more easy to reply.

Categories

Resources