2016-10-22 76 views
-3

活動主:基本計算器在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 
+1

你需要輸入你的widget (View,EditText和TextView)! –

回答

1

您需要添加進口:

import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

另外,我不確定您目前的方法是否可行。如果沒有,請使用findViewById分別獲取每個按鈕,併爲它們設置onClickListener。

要檢查哪個按鈕被點擊,使用:

Button btn1 = (Button) findViewById(R.id.button5); 
Button btn2 = (Button) findViewById(R.id.button6); 
Button btn3 = (Button) findViewById(R.id.button7); 
Button btn4 = (Button) findViewById(R.id.button8); 
TextView tt = (TextView)findViewById(R.id.txtresult); 
int result; 

btn1.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     result = val1*val2 
    } 
}); 
btn2.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     result = val1+val2 
    } 
}); 
btn3.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     result = val1-val2 
    } 
}); 
btn4.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     result = val1/val2 
    } 
}); 
tt.setText(result+""); 

希望我幫助:d

+0

耶仍然收到錯誤。錯誤:無法找到符號類EditText 錯誤:(21,22)錯誤:找不到符號類EditText 錯誤:(22,9)錯誤:無法找到符號類EditText 錯誤:(22,22)錯誤:找不到符號類EditText 錯誤:(45,20)錯誤:找不到符號變量val1val2 –

+0

好的,我將添加一些可以使用的替代代碼。如果您認爲答案令人滿意,請將其標記爲已接受:D – Rippr

0

你應該調用calc方法內onCreate方法

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    calc(yourview); 
}