2016-08-08 13 views
1

このコードを実行しようとするとエラーが発生します。その目的は、ユーザが編集テキストを通して重みと時間を入力し、スピンナを通してアクティビティを選択することである。 。しかし、私は私の値を入れた後の選択肢を選択するとエラーが出るIfが原因でクラッシュするコード

をここにコードがある:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_first, container, false); 
    countCal = (EditText) view.findViewById(R.id.editText); 
    weight = (EditText) view.findViewById(R.id.weight); 
    Spinner spinner = (Spinner) view.findViewById(R.id.spinner); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Exercises, android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter); 
    spinner.setOnItemSelectedListener(this); 
    return view; 
} 

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) { 

    if (countCal.getText().toString().trim().length() > 0 && weight.getText().toString().trim().length() > 0){ 
     if (parent.getItemAtPosition(i).toString().equals("Jogging")) { 
      Toast.makeText(parent.getContext(), (int) (0.0175 * 7 * Integer.parseInt(weight.getText().toString()) * Integer.parseInt(weight.getText().toString())), Toast.LENGTH_SHORT).show(); 
     } else if (parent.getItemAtPosition(i).toString().equals("Walking")) { 
      Toast.makeText(parent.getContext(), (int) (0.0175 * 3.5 * Integer.parseInt(weight.getText().toString()) * Integer.parseInt(weight.getText().toString())), Toast.LENGTH_SHORT).show(); 
     } else if (parent.getItemAtPosition(i).toString().equals("Free Weights")) { 
      Toast.makeText(parent.getContext(), (int) (0.0175 * 4.5 * Integer.parseInt(weight.getText().toString()) * Integer.parseInt(weight.getText().toString())), Toast.LENGTH_SHORT).show(); 
     } 

     } 
    } 

ここXML

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="number" 
    android:ems="10" 
    android:id="@+id/weight" 
    android:layout_below="@+id/textView" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignRight="@+id/editText" 
    android:layout_alignEnd="@+id/editText" /> 

<EditText 
    android:layout_width="113dp" 
    android:layout_height="wrap_content" 
    android:inputType="number" 
    android:ems="10" 
    android:id="@+id/editText" 
    android:layout_gravity="left|top" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

ここでは私のエラーログが

です
08-08 13:23:40.885 2353-2353/stefdude1999.fitness E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: stefdude1999.fitness, PID: 2353 
    android.content.res.Resources$NotFoundException: String resource ID #0xc0 
     at android.content.res.Resources.getText(Resources.java:312) 
     at android.widget.Toast.makeText(Toast.java:286) 
     at stefdude1999.fitness.FirstFragment.onItemSelected(FirstFragment.java:75) 
     at android.widget.AdapterView.fireOnSelected(AdapterView.java:924) 
     at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:913) 
     at android.widget.AdapterView.-wrap1(AdapterView.java) 
     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:883) 
     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) 

答えて

4

Toast.makeTextの2番目のパラメータは、文字列または整数のリソースIDです。計算の結果を渡すので、それをリソースIDとして扱います。そのリソースIDは無効です。計算全体をInteger.toString()内に置きます。

+0

countCal = (EditText) view.findViewById(R.id.editText); 

にonItemSelected機能を追加するには、ありがとうございました!完璧に動作します! –

関連する問題