我试图在youtube上找到这个简单的计算器我在youtube上找到但是我一直得到“错误找不到符号变量id”,“错误找不到符号变量菜单”和“错误找不到符号布局”和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_height="match_parent" android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/Lnum1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Enter First Number" android:textAppearance="?
android:attr/textAppearanceMedium"/>
<EditText
android:id="@+id/Tfnum1"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<TextView
android:id="@+id/Lnum2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Enter Second Number"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<EditText
android:id="@+id/Tfnum2"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
<TextView
android:id="@+id/Lnum3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/emptystring" android:textAppearance="?
android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/Badd"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="+"
android:onClick="onButtonClick"
style="?android:attr/buttonStyleSmall"/>
<Button
android:id="@+id/Bsub"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="-"
android:onClick="onButtonClick"
style="?android:attr/buttonStyleSmall"/>
<Button
android:id="@+id/Bmult"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="*"
android:onClick="onButtonClick"
style="?android:attr/buttonStyleSmall"/>
<Button
android:id="@+id/Bdiv"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="/"
android:onClick="onButtonClick"
style="?android:attr/buttonStyleSmall"/>
</LinearLayout>
答案 0 :(得分:0)
你必须在&#34; onCreate&#34;中初始化所有的editTexts,Buttons等....方法。当一个活动开始时,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+"");
}
答案 1 :(得分:0)
如果您小心翼翼,您会发现他从onButtonClick()中删除了代码,并将其放入onCreate()
他为您重写了您的代码。您所要做的就是复制该代码,并从onButtonClick方法中删除大部分代码,您会很好。