eclipse MainActivity.java action_settings 无法解析或不是字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28816725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MainActivity.java action_settings cannot be resolved or is not a field
提问by Helia Haghighi
This is the only error I have now in my MainActivity.java. The line "if (id == R.id.action_settings)" which is created by eclipse gives the error :"action_settings cannot be resolved or is not a field" I appreciate your help to solve it
这是我现在在 MainActivity.java 中遇到的唯一错误。由 eclipse 创建的“if (id == R.id.action_settings)”行给出了错误:“action_settings 无法解析或不是字段”我感谢您帮助解决它
package chapter.two.hello_world;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
WorldGen earth = new WorldGen("Earth", 5973, 9.78);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setStartUpWorldValues();
}
protected void setStartUpWorldValues(){
earth.setPlanetColonies(1);
earth.setPlanetMilitary(1);
earth.setColonyImmigration(1000);
earth.setBaseProtection(100);
earth.turnForceFieldOn();
}
protected void setStartUpScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView)findViewById(R.id.dataView2);
planetMassValue.setText(String.valueOf(earth.planetMass));
TextView planetGravityValue = (TextView)findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView)findViewById(R.id.dataView4);
planetColoniesValue.setText(String.valueOf(earth.planetColonies));
TextView planetPopulationValue = (TextView)findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView)findViewById(R.id.dataView6);
planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
TextView planetBaseValue = (TextView)findViewById(R.id.dataView7);
planetBaseValue.setText(String.valueOf(earth.planetBases));
TextView planetForceFieldValue = (TextView)findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) { // the error is here
return true;
}
return super.onOptionsItemSelected(item);
}
}
回答by Quanturium
Most likely your R.menu.main xml file does not contain an item having @+id/action_settings as its id. Check your R.menu.main xml file ans make sure the action_settings id is set.
很可能您的 R.menu.main xml 文件不包含以 @+id/action_settings 作为其 ID 的项目。检查您的 R.menu.main xml 文件并确保设置了 action_settings id。
回答by Peter
As say Quanturium this mean you don't have an item with this name. But as you are talking about "action_setting" you're in fact talking about special item which are not commonly in the XML main file.
正如 Quanturium 所说,这意味着您没有具有此名称的项目。但是当您谈论“action_setting”时,您实际上是在谈论 XML 主文件中不常见的特殊项目。
Note: I'm not a "pro" in Android, so I just hope this will help.
注意:我不是 Android 的“专家”,所以我只是希望这会有所帮助。
Have a look at the folder on the left of Android Studio. You have one named "res". Inside you have drawable, values etc... Do you have one named "menu"? If not create one: clic right on "res", select "New / Android ressource directory" and give "menu" as name. Now we have a folder which will contain the data for our menu, like action_settings etc...
查看 Android Studio 左侧的文件夹。你有一个名为“res”的。里面有drawable,values等等……你有一个名为“menu”的吗?如果没有创建:在“res”上单击右键,选择“New / Android ressource directory”并指定“menu”作为名称。现在我们有一个文件夹,其中将包含我们菜单的数据,例如 action_settings 等...
Now, clic right on this new folder and select "New / Menu ressource file". The name you'll give must be the name of the activity. Eg you have a Java piece of code named "Creation", the activity (in the layout folder) is named "activity_creation.xml", so in the menu folder you have to give "creation" as name of the file, which will be created as "creation.xml".
现在,右键单击这个新文件夹并选择“新建/菜单资源文件”。您提供的名称必须是活动的名称。例如,您有一段名为“Creation”的 Java 代码,活动(在布局文件夹中)被命名为“activity_creation.xml”,因此在菜单文件夹中,您必须将“creation”作为文件名,这将是创建为“creation.xml”。
Now, in this file you have to write:
现在,在这个文件中你必须写:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="net.XXX.YYY.ZZZZ" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
where XXXX and YYYY are the directory of your app (same as yu write in top of yur Java code), and ZZZZ is the name of the activity (so in my example "creation")
其中 XXXX 和 YYYY 是您的应用程序的目录(与 yu 在您的 Java 代码顶部写的相同),而 ZZZZ 是活动的名称(因此在我的示例中为“创建”)
As know you have a "menu", and an action_settings, in your code the following lines will be OK:
众所周知,您有一个“菜单”和一个 action_settings,在您的代码中,以下几行就可以了:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Understanding the goal of each folder in Android is a bit hard but necessary as a lot of "smalls problems" came from that.
了解 Android 中每个文件夹的目标有点困难但很有必要,因为很多“小问题”都来自于此。