I want to set the code .tostring but it isn't available - java

In the line of code marked by comment I want to put townHallLvl but it needs to be string so i tried to use .toString but it wasn't available.
Is there another solution or I made something wrong.
package com.example.android.planmyclash;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//set buildings' values
int townHallLvl = 1;
//declare ids
TextView townHallText = (TextView) findViewById(R.id.town_hall_lvl);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setValues();
}
public void setValues(){
townHallText.setText(); //this line <----
}
}

Use
townHallText.setText(String.valueOf(townHallLvl));
in order to convert the integer to a String.

package com.example.android.planmyclash;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//set buildings' values
int townHallLvl = 1;
TextView townHallText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//declare ids
townHallText = (TextView) findViewById(R.id.town_hall_lvl);
setValues();
}
public void setValues(){
townHallText.setText(townHallLvl + ""); //this line <----
}
}
Happy Coding

Try this:
townHallText.setText(townHallLvl+"");
I hope it will work perfectly.

Related

My code keep crashing with the same error (Cannot find symbol variable!)

package com.example.q;
import androidx.appcompat.app.AppCompatActivity;
import android.app.WallpaperManager;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
int images[] = new int[] { R.drawable.i1, R.drawable.i2, R.drawable.i3, }
Button btn;
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
new Timer().schedule(new ChangeWallpaper(),0, 30000);
}
});
}
class ChangeWallpaper extends TimerTask{
#Override public void run() {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
try {
wallpaperManager.setBitmap( BitmapFactory.decodeResource(getResources(),images[i]));
i++;
if(i==3){
i=0;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I'm having this error as mentioned in the title, originally i was facing the "R" package problem, but now it is this perhaps it could be connected ? I've already tried uninstalling and reinstalling it doesn't seem to work any suggestions ?
Cannot find symbol variable i1:17 Cannot find symbol variable i2:18
Cannot find symbol variable i3:19
public class MainActivity extends AppCompatActivity {
int images[] = new int[]{R.drawable.i1,R.drawable.i2, R.drawable.i3};
You just need to remove the COMMA in the end of the last item in the array.
But the question is too broad without a proper code to verify if the only answer would be this.

How to get a text from Edittext into a Toast?

My task is to get input text in EditText into a toast when I press the button and if there is no text, do nothing, but app crashes whenever I try to open it. Here is my code:
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
EditText name=(EditText)findViewById(R.id.EditText1);
public void cLickFuntion(View view){
String nameString=name.getText().toString();
Toast.makeText(getApplicationContext(),nameString,Toast.LENGTH_SHORT).show();
}
}
Declare EditText as global and change onCreate like below
public class MainActivity extends AppCompatActivity {
EditText name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.EditText1);
}
public void cLickFuntion(View view){
String nameString=name.getText().toString();
Toast.makeText(getApplicationContext(),nameString,Toast.LENGTH_SHORT).show();
}
}

How to solve the '.MainActivity.<init>(MainActivity.java:16)' error?

EditText object is not created, why?
EditText editText=(EditText)findViewById(R.id.edittext);
This is for an App that extends AsyncTask class. fetchingData is a class that extens AsyncTask class.But it gives error on creating object of EditText.
package com.example.zafar.omdb_7;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String string;
Button button;
public static TextView data;
//
private EditText editText = findViewById ( R.id.edittext );
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=findViewById(R.id.fetched_data);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
//
#Override
public void onClick(View v) {
// string = editText.getText ().toString ();
fetchingData process= new fetchingData();
process.execute();
}
});
}
}
Your code should be something like this:
package com.example.zafar.omdb_7;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String string;
Button button;
public static TextView data;
//
private EditText editText;
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data=findViewById(R.id.fetched_data);
editText = findViewById ( R.id.edittext );
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
//
#Override
public void onClick(View v) {
// string = editText.getText ().toString ();
fetchingData process= new fetchingData();
process.execute();
}
});
}
}
You won't find the view before you set it on the activity. I hope this helps.
You can't use the following:
public class MainActivity extends AppCompatActivity {
// This won't work!
private EditText editText = findViewById ( R.id.edittext );
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
}
Because you can't call the findViewById before you set the Activity layout with setContentView(). It means that you can't find a view if you didn't have the layout yet.
So, edit your code to something like this:
public class MainActivity extends AppCompatActivity {
...
private EditText mEdtText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Now you have the layout, you can find the view
mEdtText = findViewById(R.id.edittext);
...
}
}

Cannot Resolve Symbol 'setOnClickListener'

I am fairly new to Android/Java developing and I have run into this error. I cannot figure out what is wrong after doing extensive research and playing with my methods. Here is my code:
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener()
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
}
If anyone knows what I could've done wrong, please let me know! Thanks!
Put the code inSide onCreate()
public class mainactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Button Clicked
}
});
}
}
put your code inside onCreate() method
see the android lifeCycle to understand why :https://developer.android.com/guide/components/activities/activity-lifecycle.html
Your code is out of your method, Change it to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener()
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
}
Do Button initialization and adding listener to Button inside onCreate() method.
Try this:
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
Button SuSe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
});
}
}

Button and click display

I have tried Everything to my knowledge and nothing seems to work. Any help would be appreciated.
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class activity_main extends Activity {
TextView txtCount;
Button btnCount;
int count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount= (TextView) findViewById(R.id.textView);
txtCount.setText(String.valueOf(count));
btnCount= (Button)findViewById(R.id.button);
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
count++;
txtCount.setText(String.valueOf(count));
}
});
}
}
}
No errors appear at any point and the program works without errors; the button clicks and does not crash, but no click is counted numerically in the textView.
you onClick() listener is wrong.
try this one
btnCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
txtCount.setText(String.valueOf(count));
}
});

Categories

Resources