我运行时Android应用程序不会停止崩溃,ParseInt出现问题

时间:2016-08-08 14:51:24

标签: java android

我正在制作一个简单的应用来计算运动时燃烧的卡路里。但是,当我尝试将edittext转换为int值时,它会崩溃。我也是java和Android Studio的初学者,所以请不要犹豫,使用详细的解释。提前谢谢。

import android.support.v7.widget.AppCompatTextView;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RelativeLayout;
 import android.widget.Spinner;
 import android.widget.TextView;
 import android.widget.Toast;




public class FirstFragment extends Fragment implements AdapterView.OnItemSelectedListener{

    // Store instance variables
    private String title;
    private int page;
    EditText countCal;
    EditText weight;
    int cal;

    Spinner spinner;

    // newInstance constructor for creating fragment with arguments
    public static FirstFragment newInstance(int page, String title) {
        FirstFragment fragmentFirst = new FirstFragment();
        Bundle args = new Bundle();
        args.putInt("someInt", page);
        args.putString("someTitle", title);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }

    // Store instance variables based on arguments passed
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page = getArguments().getInt("someInt", 0);
        title = getArguments().getString("someTitle");


    }

    // Inflate the view for the fragment based on layout XML
    @Override
    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<?> 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) {
            if (weight.getText().toString().trim().length() == 0) {

            }
        } else {
           if (parent.getItemAtPosition(i).toString() == "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() == "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() == "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();
           }

        Toast.makeText(parent.getContext(), "Enter the Values!", Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}
             }

这是错误日志

08-08 10:53:46.211 31221-31221/stefdude1999.fitness E/AndroidRuntime: FATAL EXCEPTION: main
Process: stefdude1999.fitness, PID: 31221
java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
  at stefdude1999.fitness.FirstFragment.onCreateView(FirstFragment.java:56)
  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
  at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
  at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
  at android.support.v4.view.ViewPager.populate(ViewPager.java:1237)
  at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)
  at android.view.View.measure(View.java:18794)
  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
  at android.view.View.measure(View.java:18794)
  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
  at android.view.View.measure(View.java:18794)
  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
  at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
  at android.view.View.measure(View.java:18794)
  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
  at android.view.View.measure(View.java:18794)
  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
  at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
  at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
  at android.view.View.measure(View.java:18794)
  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
  at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
  at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
  at android.view.View.measure(View.java:18794)
  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
  at android.view.Choreographer.doCallbacks(Choreographer.java:670)
  at android.view.Choreographer.doFrame(Choreographer.java:606)
  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
  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)

这是我的XML布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="stefdude1999.fitness.FirstFragment"
android:id="@+id/content">

<!-- TODO: Update blank fragment layout -->

<Spinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|top"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<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" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter Minutes"
    android:id="@+id/textView"
    android:layout_gravity="left|top"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter Weight (Kg)"
    android:id="@+id/weight"
    android:layout_below="@+id/editText2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

1 个答案:

答案 0 :(得分:2)

我不确定它是什么,因为您还没有显示您的XML布局,但countCalweight不是editText。其中一个或两个实际上是AppCompatTextView。这基本上是支持旧版Android的TextView

首先导入android.widget.TextView,然后尝试一次更改一个。所以试试:

weight = (TextView) view.findViewById(R.id.weight);

如果这不起作用,请将其切换回原始版本并尝试:

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

如果这不起作用,请尝试将它们都投放到TextView

如果仍然不起作用,请尝试使用相同的方法,但导入android.support.v7.widget.AppCompatTextView并将其转换为AppCompatTextView。希望这有帮助!

编辑:

知道了!在您的XML布局中,它表示窗口小部件R.id.weight是TextView!

如果您仅将权重投放到TextView并将另一个权重保留为EditText,那么它应该有效!

weight = (TextView) view.findViewById(R.id.weight);