2017-09-18 2 views
-2

나는 프로그램을 실행할 때마다 멈추는 오류가 계속 발생합니다. 몇 가지 포럼을 읽은 후, 내 문제는 정수와 물건의 전환에 있다고 생각합니다. "parseInt"에서 "valueOf"로 변경하려고 시도했지만 도움이되지 않았습니다. 이 주요 활동java.lang.NumberFormatException : 입력 문자열의 경우 : ""?

package com.example.gutierrez.calculator; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

TextView total; 
EditText pewee_tray, pewee_price, sp_tray, sp_price, pullets_tray, pullets_price, small_price, small_tray, medium_price, medium_tray, large_price, large_tray, xlarge_tray, xlarge_price, jumbo_price, jumbo_tray; 
Button compute; 

float tprice, pewprice, spprice, pulprice, smallprice, medprice, largeprice, xlprice, jumboprice; 
int pewtray, sptray, pultray, smalltray, medtray, largetray, xltray, jumbotray; 



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

    total = (TextView) findViewById(R.id.total); 


    pewee_tray = (EditText) findViewById(R.id.pewee_tray); 
    pewee_price =(EditText) findViewById(R.id.pewee_price); 

    sp_tray = (EditText) findViewById(R.id.sp_tray); 
    sp_price =(EditText) findViewById(R.id.sp_price); 

    pullets_tray = (EditText) findViewById(R.id.pullets_tray); 
    pullets_price =(EditText) findViewById(R.id.pullets_price); 

    small_tray = (EditText) findViewById(R.id.small_tray); 
    small_price =(EditText) findViewById(R.id.small_price); 

    medium_tray = (EditText) findViewById(R.id.medium_tray); 
    medium_price =(EditText) findViewById(R.id.medium_price); 

    large_tray = (EditText) findViewById(R.id.large_tray); 
    large_price =(EditText) findViewById(R.id.large_price); 

    xlarge_tray= (EditText) findViewById(R.id.xlarge_tray); 
    xlarge_price =(EditText) findViewById(R.id.xlarge_price); 

    jumbo_tray = (EditText) findViewById(R.id.jumbo_tray); 
    jumbo_price =(EditText) findViewById(R.id.jumbo_price); 

    pewtray = Integer.valueOf(pewee_tray.getText().toString()); 
    pewprice = Integer.valueOf(pewee_price.getText().toString()); 

    sptray = Integer.valueOf(sp_tray.getText().toString()); 
    spprice = Integer.valueOf(sp_price.getText().toString()); 

    pultray = Integer.valueOf(pullets_tray.getText().toString()); 
    pulprice = Integer.valueOf(pullets_price.getText().toString()); 

    smalltray = Integer.valueOf(pullets_tray.getText().toString()); 
    smallprice = Integer.valueOf(small_price.getText().toString()); 

    medtray = Integer.valueOf(pullets_tray.getText().toString()); 
    medprice = Integer.valueOf(medium_price.getText().toString()); 

    largetray = Integer.valueOf(large_tray.getText().toString()); 
    largeprice = Integer.valueOf(large_price.getText().toString()); 

    xltray = Integer.valueOf(large_tray.getText().toString()); 
    xlprice = Integer.valueOf(xlarge_price.getText().toString()); 

    jumbotray = Integer.valueOf(jumbo_tray.getText().toString()); 
    jumboprice = Integer.valueOf(jumbo_price.getText().toString()); 

    compute = (Button) findViewById(R.id.compute); 

    compute.setOnClickListener(new View.OnClickListener() { 
     @Override 

       public void onClick(View v){ 
      tprice = pewprice * pewtray + spprice * sptray + pulprice * pultray + smallprice * smalltray + medprice * medtray + largeprice * largetray + xlprice * xltray + jumboprice * jumbotray; 
      total.setText(String.valueOf(tprice)); 
     } 
     }); 

} 
} 

이며,이 하나가 로그 캣 항목입니다 :

당신이 사용자가 아무것도 입력에도 전에 EditText의 값을 변환하려는 때문입니다
09-18 16:55:15.347 2491-2491/com.example.gutierrez.calculator I/art: Not 
late-enabling -Xcheck:jni (already on) 
09-18 16:55:15.347 2491-2491/com.example.gutierrez.calculator W/art: 
Unexpected CPU variant for X86 using defaults: x86 
09-18 16:55:15.577 2491-2491/com.example.gutierrez.calculator W/System: 
ClassLoader referenced unknown path: 
/data/app/com.example.gutierrez.calculator-1/lib/x86 
09-18 16:55:15.582 2491-2491/com.example.gutierrez.calculator I/InstantRun: 
starting instant run server: is main process 
09-18 16:55:15.649 2491-2491/com.example.gutierrez.calculator W/art: Before 
Android 4.1, method android.graphics.PorterDuffColorFilter 

     android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) 
     would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
    09-18 16:55:15.746 2491-2491/com.example.gutierrez.calculator D/AndroidRuntime: Shutting down VM 
    09-18 16:55:15.747 2491-2491/com.example.gutierrez.calculator E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.gutierrez.calculator, PID: 2491 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gutierrez.calculator/com.example.gutierrez.calculator.MainActivity}: java.lang.NumberFormatException: For input string: "" 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:154) 
     at android.app.ActivityThread.main(ActivityThread.java:6119) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
    Caused by: java.lang.NumberFormatException: For input string: "" 
     at java.lang.Integer.parseInt(Integer.java:533) 
     at java.lang.Integer.parseInt(Integer.java:556) 
     at com.example.gutierrez.calculator.MainActivity.onCreate(MainActivity.java:55) 
     at android.app.Activity.performCreate(Activity.java:6679) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
     at android.app.ActivityThread.-wrap12(ActivityThread.java)  
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  
     at android.os.Handler.dispatchMessage(Handler.java:102)  
     at android.os.Looper.loop(Looper.java:154)  
     at android.app.ActivityThread.main(ActivityThread.java:6119)  
     at java.lang.reflect.Method.invoke(Native Method)  
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) ' 
+0

빈 문자열 그래서 interger하지 않습니다 .. ... – tyczj

+0

textview가 비어 있는지 확인 (사용자가 값을 입력하지 않은 경우) 또는 NumberFormatException과 함께 try catch 블록을 사용하는 것이 좋습니다. 2 가지 옵션이 있지만 빈 값을 확인하는 것은 당신이해야 할 일입니다. – Thracian

답변

0

. onClick 방법이 라인을 이동 :

pewtray = Integer.valueOf(pewee_tray.getText().toString()); 
pewprice = Integer.valueOf(pewee_price.getText().toString()); 

sptray = Integer.valueOf(sp_tray.getText().toString()); 
spprice = Integer.valueOf(sp_price.getText().toString()); 

pultray = Integer.valueOf(pullets_tray.getText().toString()); 
pulprice = Integer.valueOf(pullets_price.getText().toString()); 

smalltray = Integer.valueOf(pullets_tray.getText().toString()); 
smallprice = Integer.valueOf(small_price.getText().toString()); 

medtray = Integer.valueOf(pullets_tray.getText().toString()); 
medprice = Integer.valueOf(medium_price.getText().toString()); 

largetray = Integer.valueOf(large_tray.getText().toString()); 
largeprice = Integer.valueOf(large_price.getText().toString()); 

xltray = Integer.valueOf(large_tray.getText().toString()); 
xlprice = Integer.valueOf(xlarge_price.getText().toString()); 

jumbotray = Integer.valueOf(jumbo_tray.getText().toString()); 
jumboprice = Integer.valueOf(jumbo_price.getText().toString()); 
0

시도 다음 코드

compute.setOnClickListener(new View.OnClickListener() { 
      @Override 

        public void onClick(View v){ 

    pewtray = Integer.valueOf(pewee_tray.getText().toString()); 
    pewprice = Integer.valueOf(pewee_price.getText().toString()); 

    sptray = Integer.valueOf(sp_tray.getText().toString()); 
    spprice = Integer.valueOf(sp_price.getText().toString()); 

    pultray = Integer.valueOf(pullets_tray.getText().toString()); 
    pulprice = Integer.valueOf(pullets_price.getText().toString()); 

    smalltray = Integer.valueOf(pullets_tray.getText().toString()); 
    smallprice = Integer.valueOf(small_price.getText().toString()); 

    medtray = Integer.valueOf(pullets_tray.getText().toString()); 
    medprice = Integer.valueOf(medium_price.getText().toString()); 

    largetray = Integer.valueOf(large_tray.getText().toString()); 
    largeprice = Integer.valueOf(large_price.getText().toString()); 

    xltray = Integer.valueOf(large_tray.getText().toString()); 
    xlprice = Integer.valueOf(xlarge_price.getText().toString()); 

    jumbotray = Integer.valueOf(jumbo_tray.getText().toString()); 
    jumboprice = Integer.valueOf(jumbo_price.getText().toString()); 


       tprice = pewprice * pewtray + spprice * sptray + pulprice * pultray + smallprice * smalltray + medprice * medtray + largeprice * largetray + xlprice * xltray + jumboprice * jumbotray; 
       total.setText(String.valueOf(tprice)); 
      } 
      }); 

사용이 "문자열이 숫자가 포함 된 경우 확인"

Pattern p = Pattern.compile("([0-9])"); 
    Matcher m = p.matcher("Here is ur string"); 

    if(m.find()){ 
     System.out.println("Hello "+m.find()); 
    }else 
    { 
System.out.println("Please Enter Integer"); 
    } 
관련 문제