2016-09-18 4 views
0

저는 완전히 Android 개발에 익숙하며 두 클래스 만 사용했습니다. 교수님은 숫자를 입력하고 식사 비용을 계산하는 앱을 만드는 미니 프로젝트를 제공했습니다. 이것은 내가 지금까지 가지고있는 것입니다 :ava.lang.ClassCastException : android.support.v7.widget.AppCompatTextView를 android.widget.EditText로 캐스팅 할 수 없습니다.

public class MainActivity extends AppCompatActivity { 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

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

     Button cButton = (Button) findViewById(R.id.calculateButton); 
     cButton.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View view) 
       { 
        //getting cost 
        EditText cost = (EditText)findViewById(R.id.mealCost); 
        String mealCost = cost.getText().toString(); 
        int mealCostInt = Integer.parseInt(mealCost); 

        //getting tax percentage 
        EditText tax = (EditText)findViewById(R.id.tax); 
        String taxAmount = cost.getText().toString(); 
        int taxAmountInt = Integer.parseInt(mealCost); 

        //getting tip percentage 
        EditText tip = (EditText)findViewById(R.id.tip); 
        String tipAmount = cost.getText().toString(); 
        int tipAmountInt = Integer.parseInt(mealCost); 

        //calculating total amount 
        int totalAmount = (mealCostInt)+((taxAmountInt/100)*mealCostInt)+((tipAmountInt/100)*mealCostInt); 

        //displaying total amount 
        EditText total = (EditText)findViewById(R.id.total); 
        total.setText(totalAmount); 
       } 
      } 

     ); 

    } 


} 

코드를 실행할 때마다 앱이 성공적으로로드됩니다. 그러나 때마다 내가 계산 버튼을 클릭, 나는 오류가 아래와 얻을 :

--------- beginning of crash 
09-18 20:25:40.169 2752-2752/com.example.shameemah.mealcalculator E/AndroidRuntime: FATAL EXCEPTION: main 
                        Process: com.example.shameemah.mealcalculator, PID: 2752 
                        java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText 
                         at com.example.shameemah.mealcalculator.MainActivity$1.onClick(MainActivity.java:35) 
                         at android.view.View.performClick(View.java:5198) 
                         at android.view.View$PerformClick.run(View.java:21147) 
                         at android.os.Handler.handleCallback(Handler.java:739) 
                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                         at android.os.Looper.loop(Looper.java:148) 
                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

나는 비슷한 오류를 검색하고 내가 본 몇 가지 솔루션을 시도했지만 때문에 내 제한된 지식 나는 확실하지 않다 정확히 내가해야 하고있어. 나는 몇 가지 상세한 설명과 함께 도움을 진심으로 감사 할 것입니다. 나는 배우고 이해하기 위해 열심히 노력하고 있습니다. 안드로이드 개발자와 경험이없는 사람과 이야기하는 것처럼 대답하십시오. 정말 고맙습니다!

답변

1

XML 레이아웃의보기 (mealCost, 세금 또는 팁) 중 하나가 TextView 위젯이라고 생각합니다. 그래서 암묵적으로 변환하면 오류가 발생합니다. EditText에서 TextView를 xml로 변경하면 오류가 사라집니다.

+0

그걸 고쳤습니다. 고맙습니다! –

+0

도움이 된 것을 기쁘게 생각합니다. 정답으로 표시하십시오. – Dmitriy