2017-09-18 17 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)); 
     } 
     }); 

} 
} 

であり、この1はlogcatエントリです:

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

ユーザーが何を入力する前であってもEditTextに値を変換しようとしているためです。 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"); 
    } 
関連する問題