*Android Error*-Syntax error on token(s), misplaced construct(s) - android

I'm new at this and is the first time I try to do an android app. I'm trying to do a rock, paper & scissors I know I haven't finish the code but I'm having some error....

I see a few places that could be the problem, but one that could be causing you trouble is where you have
else (computerplay.equals("5"))
you are missing the if, you need something like
else if(computerplay.equals("5"))
in a few places.
Also, you need to reference the TextView that's in your layout, so instead of
TextView tv = new TextView(this);
you need something like
TextView tv = (TextView) findViewById(R.id.textview);
and dont call setContentView(tv).

Related

how is an object created?

EditText text1;
text1 = (EditText) findViewById(R.id.editText1);
text1.getText().toString();
Hi im new to android programming an need a little help. :) I just want to clarify if text1 is an object? Because it can call a method. But if text1 is an object how come that there is no "new" keyword. Thanks in advance for any response. :)
It is not necessary that all variables do initialization with new keyword.
Like if you write
String s = "";
Your String has been initialized without new keyword.
Same like this EditText is provided initialization in findViewById(). Here findViewById returned Edittext instance.
I suggest you complete Java Tutorial before continue work on Android. Because Android is based on Java language.
EditText is derived from the Super class View. Here findViewById method is returning an an object of the View class. You are explicitly typecasting it to EditText and assigning it to text1. So new is not required. It is being managed in findViewById method. Alternatively you can do this as:
EditText text1;
text1 = new EditText(An instance of Context); //Create an object of Edittext class
Now do whatever with this object text1.
It's me 2 years after, Now I want to answer your question bud. I know you just started and had a dream of creating apps that will be used by others, and guess what? You already achieved that and you can now also create apps not just for android but for IOS since you're using flutter now, you also have your own account on google play store now. Listen Bud, on the first line you declare what type is the text1. on the second line that's where you declare the text1 as an object now, then on the third line you are then using that object's capabilities like getting text. You've learned so much in this journey, and still learning not just in programming but in life.

Android: android textview recognition

This code is within a method in a class (not main):
TextView moodouttw = (TextView) findViewById(R.id.moodouttw);
The textview seemes not to be recognized in this part: R.id.moodouttw
The textview moodouttw is located within a layout xml.
Can someone please helpout with code to correctly format the code above?
(I've tried to minimize code at first if it could be solved with any further info)
LOGCAT: 940Kb cant post
is not iR.d is just R.id
the proper way will be
findViewById(R.id.moodouttw);
this is the complete correct sentence
TextView moodouttw = (TextView) findViewById(R.id.moodouttw);
ok as you stated at your xml file, the problem is that your TextView id is moodtw and you are doing R.id.moodouttw;
so you need to do this
TextView moodouttw = (TextView) findViewById(R.id.moodtw);

setVisibility(View.Gone) with SwipeyTabs

I'm a beginner android programmer, and I'm trying to figure out how to work with SwipeyTabs. First of all, is this a good way to find the view:
rb2 = (RadioButton) getView().findViewById(R.id.rb2);
rb2.setOnClickListener(this);
My second question is about how to set the visibility of a TextView or an EditText with SwipeyTabs. I've made a simple application without SwipeyTabs, and there I use the code:
tvPlayer1.setVisibility(View.GONE);
Once I use this same line of code within SwipeyTabs, it crashes. Can someone explain me why I can't get this to work?
Thank you in advance!
Turned out I had a grammar mistake in my line of code.
etPlayer1 = (TextView) getView().findViewById(R.id.etPlayer1);
had to be:
etPlayer1 = (EditText) getView().findViewById(R.id.etPlayer1);

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

how to load parsed data into an existing textview(existing layout) in android

hii every buddy ,
am not getting how to load parsed data into an existing textview(existing layout) in android,
insted of loading data to the new textview like how they r doing in the following tutorial(check the following link)
check this link for tutorial
In the tutorial they do:
TextView something = new TextView(context);
As you don't want to do so (good decision), you get the reference to the existent TextView:
TextView something = (TextView)findViewById(R.id.the_id_you_gave_it_in_the_xml);
// then:
something.setText(theParsedString);
Try this code using existing Layout
Textview name=(TextView)findViewById(R.id.textView1);
String txt="";
for(int i=0;i<sitesList.getName().size();i++)
{
txt=txt+sitesList.getName().get(i).toString();
}
name.setText(txt);
Sowmya It seems that you are creating multiple textviews & trying to update those textviews .In that case you need to use setTag() and getTag() .
There is an example you can find here
Regarding your answer:
we r declaring textview array so am
unable to load to existing textview
I have never implemented such a textview array instead the best practice would be to
for (String s : stringarray) {
TextView tv = new TextView (this);
tv.setText(s);
tv.setTag("1");
linearlayout.addView(tv);
}
*EDIT:*For those enthusiast out there I found 2 more methods:
One is using setId() to manually set ID of that textview & then find it by findViewById()
Another method(is though not implemented by me , but is a suggestion by my colleague so dont kill me if it doesn't works) is to store textview objects in an arraylist & then access them & do whatever you want whoa!

Categories

Resources