我尝试运行我的第一个 Java 代码,但收到"symbol"错误



我对Android世界和Java很陌生。

当我尝试运行此代码时:

package com.example.firstapp;
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;
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() {
@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, "Change 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) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

我收到此错误:

/Users/pro/AndroidStudioProjects/FirstApp/app/src/main/java/com/example/firstapp/MainActivity.java:29: error: cannot find symbol
TextView textValue = findViewById(R.id.text_value);
^
symbol: class TextView
/Users/pro/AndroidStudioProjects/FirstApp/app/src/main/java/com/example/firstapp/MainActivity.java:31: error: cannot find symbol
int originalValue = integer.parseInt(stringValue);
^
symbol: variable integer
/Users/pro/AndroidStudioProjects/FirstApp/app/src/main/java/com/example/firstapp/MainActivity.java:33: error: cannot find symbol
textValue.setText(integer.toString(newValue));
^
symbol: variable integer

这不是"符号"错误。让我们看一下其中之一(切出一条很长的路径(:

<somepath>/MainActivity.java:29: error: cannot find symbol
TextView textValue = findViewById(R.id.text_value);
^
symbol: class TextView

请参阅它显示"错误:找不到符号"的位置。这是重要的部分。编译器基本上告诉你它不知道你在说什么。

找不到两个符号(第二个符号出现两次(:TextViewinteger。原因不同:

  • 您缺少导入TextView,将import android.widget.TextView添加到文件顶部。
  • 你拼错了Integer:它需要在开头有一个大写的 I。

相关内容

最新更新