错误找不到符号变量 id
Posted
技术标签:
【中文标题】错误找不到符号变量 id【英文标题】:error cannot find symbol variable id 【发布时间】:2015-08-30 00:30:32 【问题描述】:我正在尝试根据我在 youtube 上找到的本教程构建这个简单的计算器,但我不断收到“”、“错误找不到符号变量菜单”和“错误找不到符号布局”和 id、菜单和布局显示为红色这里是代码,请帮助,谢谢:
package com.example.khalil.basiccalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
public void onButtonClick(View v)
//2 text fields - Edit Text
EditText a1 = (EditText)findViewById(R.id.Tfnum1);
EditText a2 = (EditText)findViewById(R.id.Tfnum2);
//1 result label - TextView
TextView tv = (TextView) findViewById(R.id.Lresult);
boolean flag = false; //no division by 0 error
double num1,num2,ans;
num1 = Double.parseDouble(a1.getText().toString());
num2 = Double.parseDouble(a2.getText().toString());
if (v.getId() == R.id.Badd)
ans = num1 + num2;
if(v.getId() == R.id.Bsub)
ans = num1 - num2;
if(v.getId() == R.id.Bmult)
ans = num1 * num2;
if(v.getId() == R.id.Bdiv)
//division by 0
if (num2 == 0)
flag = true; //division by 0 error
else
ans = num1 / num2;
tv.setText(ans+"");
@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);
private class R
这是 xml:
<?xml version="1.0"?>
<LinearLayout
android:id="@+id/activity_main"
tools:context=".MainActivity"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:layout_ android:layout_
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/Lnum1"
android:layout_
android:layout_
android:text="Enter First Number" android:textAppearance="?
android:attr/textAppearanceMedium"/>
<EditText
android:id="@+id/Tfnum1"
android:layout_
android:layout_/>
<TextView
android:id="@+id/Lnum2"
android:layout_
android:layout_
android:text="Enter Second Number"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<EditText
android:id="@+id/Tfnum2"
android:layout_
android:layout_/>
<TextView
android:id="@+id/Lnum3"
android:layout_
android:layout_
android:text="@string/emptystring" android:textAppearance="?
android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/Badd"
android:layout_
android:layout_
android:text="+"
android:onClick="onButtonClick"
style="?android:attr/buttonStyleSmall"/>
<Button
android:id="@+id/Bsub"
android:layout_
android:layout_
android:text="-"
android:onClick="onButtonClick"
style="?android:attr/buttonStyleSmall"/>
<Button
android:id="@+id/Bmult"
android:layout_
android:layout_
android:text="*"
android:onClick="onButtonClick"
style="?android:attr/buttonStyleSmall"/>
<Button
android:id="@+id/Bdiv"
android:layout_
android:layout_
android:text="/"
android:onClick="onButtonClick"
style="?android:attr/buttonStyleSmall"/>
</LinearLayout>
【问题讨论】:
【参考方案1】:您必须在“onCreate”方法中初始化所有的editText、按钮等。当一个活动开始时,onCreate首先被调用,所以每当访问UI元素时,在你的onCreate方法上初始化它
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//2 text fields - Edit Text
EditText a1 = (EditText)findViewById(R.id.Tfnum1);
EditText a2 = (EditText)findViewById(R.id.Tfnum2);
//1 result label - TextView
TextView tv = (TextView) findViewById(R.id.Lresult);
boolean flag = false; //no division by 0 error
double num1,num2,ans;
num1 = Double.parseDouble(a1.getText().toString());
num2 = Double.parseDouble(a2.getText().toString());
if (v.getId() == R.id.Badd)
ans = num1 + num2;
if(v.getId() == R.id.Bsub)
ans = num1 - num2;
if(v.getId() == R.id.Bmult)
ans = num1 * num2;
if(v.getId() == R.id.Bdiv)
//division by 0
if (num2 == 0)
flag = true; //division by 0 error
else
ans = num1 / num2;
tv.setText(ans+"");
【讨论】:
谢谢,这可能是一个愚蠢的问题,但我该怎么做呢?【参考方案2】:如果你仔细你会看到他从 onButtonClick() 中删除了代码并将其放入 onCreate()
他为你重写了你的代码。您所要做的就是复制该代码并从 onButtonClick 方法中删除大部分代码,这样就可以了。
【讨论】:
这是对另一个答案的回应吗?目前尚不清楚“他重写了你的代码”指的是谁,但请参阅How to Answer 上的指导以上是关于错误找不到符号变量 id的主要内容,如果未能解决你的问题,请参考以下文章
错误:找不到符号类 FirebaseInstanceIdService