gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:26:21 25 4
gpt4 key购买 nike

我正在制作一个简单的应用程序来计算锻炼时燃烧的卡路里。但是,当我尝试将 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" />

最佳答案

我不确定它是什么,因为您还没有显示 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);

关于java - Android 应用程序在运行时不会停止崩溃,ParseInt 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38832358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com