活動主:基本計算器在Android的錯誤
<TextView
android:text="Enter your first number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/textView2"
android:textSize="20sp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="12dp"
android:id="@+id/txtfno"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Enter your second number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:textSize="20sp"
android:layout_below="@+id/txtfno"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="@+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="17dp"
android:id="@+id/txtsno" />
<TextView
android:text="Answer:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtsno"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="13dp"
android:id="@+id/result"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/result"
android:layout_toRightOf="@+id/result"
android:layout_toEndOf="@+id/result"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:id="@+id/txtresult"
android:textSize="20sp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/result"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp">
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button6"
android:layout_weight="1"
android:onClick="calc"/>
<Button
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button7"
android:layout_weight="1"
android:onClick="calc"/>
<Button
android:text="*"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button5"
android:layout_weight="1"
android:onClick="calc"/>
<Button
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button8"
android:layout_weight="1"
android:onClick="calc"/>
</LinearLayout>
</RelativeLayout>
主要活動的Java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void calc(View view){
EditText E1=(EditText)findViewById(R.id.txtfno);
EditText E2=(EditText)findViewById(R.id.txtsno);
//reads 1st value
int val1 = Integer.parseInt(E1.getText().toString());
int val2 = Integer.parseInt(E2.getText().toString());
int result = 0;
//checking which button was click
if(view.getId()==R.id.button6)
{
result=val1+val2;
TextView tt = (TextView)findViewById(R.id.txtresult);
tt.setText(String.valueOf(result));
}
if(view.getId()==R.id.button7)
{
result=val1-val2;
TextView tt = (TextView)findViewById(R.id.txtresult);
tt.setText(String.valueOf(result));
}
if(view.getId()==R.id.button5)
{
result=val1val2;
TextView tt = (TextView)findViewById(R.id.txtresult);
tt.setText(String.valueOf(result));
}
if(view.getId()==R.id.button8)
{
result=val1/val2;
TextView tt = (TextView)findViewById(R.id.txtresult);
tt.setText(String.valueOf(result));
}
}
}
下面是錯誤:
Error:(15, 22) error: cannot find symbol class View
Error:(18, 9) error: cannot find symbol class EditText
Error:(18, 22) error: cannot find symbol class EditText
Error:(19, 9) error: cannot find symbol class EditText
Error:(19, 22) error: cannot find symbol class EditText
Error:(31, 13) error: cannot find symbol class TextView
Error:(31, 28) error: cannot find symbol class TextView
Error:(37, 13) error: cannot find symbol class TextView
Error:(37, 28) error: cannot find symbol class TextView
Error:(42, 20) error: cannot find symbol variable val1val2
Error:(43, 13) error: cannot find symbol class TextView
Error:(43, 28) error: cannot find symbol class TextView
Error:(49, 13) error: cannot find symbol class TextView
Error:(49, 28) error: cannot find symbol class TextView
你需要輸入你的widget (View,EditText和TextView)! –