Android 不幸的是,FindWorkOutActivity已停止工作。如何解决这个错误?
我是Android学习新手,尝试完成我的任务,在任务中我需要创建一个简单的训练应用程序,包括下拉列表、按钮和文本。在应用程序预览中,应用程序看起来不错,但当我在设备上运行时,它会给我一个错误“很遗憾,FindWorkOutActivity已停止工作。” 我在互联网上搜索了这个错误,无法理解Logcat信息,因为我没有了解到这一点。请帮我解决这个错误 以下是我的应用程序的文件: 活动\u find\u work\u out.xmlAndroid 不幸的是,FindWorkOutActivity已停止工作。如何解决这个错误?,android,android-layout,Android,Android Layout,我是Android学习新手,尝试完成我的任务,在任务中我需要创建一个简单的训练应用程序,包括下拉列表、按钮和文本。在应用程序预览中,应用程序看起来不错,但当我在设备上运行时,它会给我一个错误“很遗憾,FindWorkOutActivity已停止工作。” 我在互联网上搜索了这个错误,无法理解Logcat信息,因为我没有了解到这一点。请帮我解决这个错误 以下是我的应用程序的文件: 活动\u find\u work\u out.xml <?xml version="1.0" encodi
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rj.findworkout.FindWorkOutActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/your_workout_here"
android:id="@+id/text_hello"
android:layout_centerHorizontal="true"
android:layout_below="@+id/button_id"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_id"
android:text="New Button"
android:layout_centerHorizontal="true"
android:layout_below="@+id/spinner_id"
android:layout_marginTop="20dp" />
<Spinner
android:id="@+id/spinner_id"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="35dp"
android:layout_centerHorizontal="true"
android:entries="@array/work_out"
android:onClick="onClickWorkOut"
>
</Spinner>
</RelativeLayout>
您的问题是微调器中的onClick属性。正确的方法是在其上添加setOnItemSelectedListener。因此,首先需要删除
android:onClick="onClickWorkOut"
在活动代码中,添加一个监听器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner workouttype = (Spinner) findViewById(R.id.spinner_id);
workouttype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
微调器工作类型=(微调器)findViewById(R.id.Spinner\u id);
workouttype.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
}
使用LogCat检查与崩溃相关的Java堆栈跟踪:请注意,workouttype
没有数据,因此它没有选定项。@Commonware感谢您的建议,我是Android新手,不知道LogCat,因为我没有了解它。@Commonware请指导我如何解决这个错误。我建议你读一本关于Android应用程序开发的书。只有使用SpinnerAdapter
对其进行配置,才能使用微调器。请参阅使用微调器的示例应用程序。感谢您的回答,但我不知道adapterview,请您帮助我用简单的方式解决。微调器是adapterview的子类。AdapterView是由适配器确定其子视图的视图。对于微调器,一次只能看到一个子视图。为了能够处理微调器中的更改,需要设置OnItemSelectedListener。这将有两个回调函数onItemSelected和onNothingSelected。他们的目的不言而喻。adapterView参数告诉您是什么微调器触发了事件。由于您只有一个微调器,因此无需担心。你可能感兴趣的是这个职位。这个职位告诉你选择了列表中的哪个项目。该职位从0开始,对应于列表中的第一项。我不了解这个问题,请帮助理解,因为我是android的新手。我已经尝试了相同的代码,因为我从视频中了解到他们的代码运行良好,但我得到了这个错误。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner workouttype = (Spinner) findViewById(R.id.spinner_id);
workouttype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}