简体   繁体   English

无法解析符号“setText”和“toString”

[英]Cannot resolve symbol 'setText' and 'toString'

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.它一直显示我无法解析符号 'setText' 和 'toString' 我不知道为什么它会显示这些错误。 I tried File --> Invalidate Caches/Restart... but it didn't work.我试过 File --> Invalidate Caches/Restart... 但它没有用。 Here is my MainActivity.java.这是我的 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));只需移动您的textValue.setText(Integer.toString(newValue)); code inside onClick() method it will work onClick()方法中的代码它将起作用

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();
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM