如何解决您好,每次调用存在于名为“Firstfragment”的 Fragment 中的 Spinner 时,我的应用程序都会崩溃
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//This Spinner is not located in the MainActivity but in A fragment
Spinner s = (Spinner) findViewById(R.id.spinner33);
s.setonItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0,View arg1,int arg2,long arg3) {
String msupplier=s.getSelectedItem().toString();
Log.e("Selected item : ",msupplier);
}
@Override
public void onnothingSelected(AdapterView<?> arg0) {
}
});
//Main MainActivity
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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=".MainActivity"
android:id="@+id/drawer">
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="104dp"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
//第一个片段
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/Fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2196F3"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="773dp"
android:src="@mipmap/background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_700"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/watBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:drawabletop="@drawable/ic_baseline_looks_one_24"
android:editable="true"
android:enabled="true"
android:focusable="auto"
android:gravity="center"
android:includeFontPadding="true"
android:linksClickable="true"
android:onClick="WattMthd"
android:text="@string/WAT"
app:backgroundTint="@android:color/holo_blue_dark"
tools:layout_editor_absoluteX="35dp"
tools:layout_editor_absoluteY="129dp" />
<Button
android:id="@+id/ampBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawabletop="@drawable/ic_baseline_looks_two_24"
android:editable="true"
android:gravity="center"
android:onClick="ampMthd"
android:text="@string/AMP"
android:textColorLink="#FFFFFF"
app:backgroundTint="@android:color/darker_gray" />
<Button
android:id="@+id/voltBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawabletop="@drawable/ic_baseline_looks_3_24"
android:editable="true"
android:gravity="center"
android:onClick="voltMthd"
android:text="فولت"
app:backgroundTint="@android:color/holo_blue_dark" />
<Button
android:id="@+id/kWbtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawabletop="@drawable/ic_baseline_looks_4_24"
android:editable="true"
android:gravity="center"
android:onClick="kWMthd"
android:text="كيلو"
app:backgroundTint="@android:color/holo_blue_dark" />
</LinearLayout>
<Spinner
android:id="@+id/spinner33"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:dropDownWidth="match_parent"
android:entries="@array/SpinerConvertFrom"
android:foreground="@drawable/ic_baseline_arrow_drop_down_circle_24"
android:foregroundGravity="left|center"
android:gravity="center"
android:textAlignment="center"
android:visibility="visible"
app:flow_verticalAlign="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner33">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/Text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/AMP"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:textAlignment="center" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/Volt"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:textAlignment="center" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
解决方法
如果您是从片段中调用微调器初始化,但它存在于主活动中它会崩溃,您需要在活动中调用它,或将其移动到 framgnet 的 xml。
,activity_main 属于 MainActivity。所以有一个fragment_first 属于FirstFragment。您尝试初始化的微调器属于fragment_first,因此您必须在FirstFragment 中使用它或将其(微调器)移动到activity_main
,你好,你的activity_main.xml里面你没有定义spinner,所以你不能得到你没有在xml文件中创建的视图,你可以从你的片段中获取它,或者你必须将Spinner移动到activity_main .xml。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。