While following a tutorial I did the same thing he did which he didn't get any errors. it keeps showing me cannot resolve symbol 'setText' and 'toString' i don't why it shows me these errors. I tried File --> Invalidate Caches/Restart... but it didn't work. Here is my MainActivity.java.
package com.example.firstapk;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
TextView textValue = findViewById(R.id.text_value);
String stringValue = textValue.getText().toString();
int originalValue = Integer.parseInt(stringValue);
int newValue = MyWorker.doubleTheValue(originalValue);
textValue.setText(Integer.toString(newValue));
@Override
public void onClick(View view) {
Snackbar.make(view, "Changed Value " + originalValue + " to " + newValue, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Just move your textValue.setText(Integer.toString(newValue));
code inside onClick()
method it will work
Try this
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView textValue = findViewById(R.id.text_value);
String stringValue = textValue.getText().toString();
int originalValue = Integer.parseInt(stringValue);
int newValue = MyWorker.doubleTheValue(originalValue);
textValue.setText(Integer.toString(newValue));
Snackbar.make(view, "Changed Value " + originalValue + " to " + newValue, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.