Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 您好,每次我调用Spinner时,我的应用程序都会崩溃,Spinner存在于一个名为;“第一片段”; 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //此微调器不位于MainActivity中,而是位于片段中 微调器s=(微调器)findViewById(R.id.spinner33); s、 setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){ @凌驾 已选择公共视图(适配器视图arg0、视图arg1、内部arg2、, 长arg3){ 字符串msupplier=s.getSelectedItem().toString(); Log.e(“所选项目:”,msupplier); } @凌驾 未选择公共无效(AdapterView arg0){ } });_Java_Android_Spinner - Fatal编程技术网

Java 您好,每次我调用Spinner时,我的应用程序都会崩溃,Spinner存在于一个名为;“第一片段”; 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //此微调器不位于MainActivity中,而是位于片段中 微调器s=(微调器)findViewById(R.id.spinner33); s、 setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){ @凌驾 已选择公共视图(适配器视图arg0、视图arg1、内部arg2、, 长arg3){ 字符串msupplier=s.getSelectedItem().toString(); Log.e(“所选项目:”,msupplier); } @凌驾 未选择公共无效(AdapterView arg0){ } });

Java 您好,每次我调用Spinner时,我的应用程序都会崩溃,Spinner存在于一个名为;“第一片段”; 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //此微调器不位于MainActivity中,而是位于片段中 微调器s=(微调器)findViewById(R.id.spinner33); s、 setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){ @凌驾 已选择公共视图(适配器视图arg0、视图arg1、内部arg2、, 长arg3){ 字符串msupplier=s.getSelectedItem().toString(); Log.e(“所选项目:”,msupplier); } @凌驾 未选择公共无效(AdapterView arg0){ } });,java,android,spinner,Java,Android,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)

//主要活动

   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) {

              
            }
        });

//第一个片段

<?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>


如果您正在从片段调用微调器初始化,但它存在于主活动中,它将崩溃,您需要在活动中调用它,或将其移动到framgnet的xml中。

activity\u main属于MainActivity。因此有一个片段\u first属于FirstFragment。您尝试初始化的微调器是渴望首先分割_,所以必须在FirstFragment中使用它或移动它(微调器)对于activity\u main

Hi bro在你的activity\u main.xml中你没有定义微调器,因此你无法获得你没有在xml文件中创建的视图,你可以从你的片段中获取它,或者你必须将微调器移动到activity\u main.xml。

那么你就不能使用findViewById,在组件需要你回复的地方使用它,但我没有明白了。我是Android新手,非常感谢您的帮助谢谢您——相反,我正在从主活动调用微调器初始化,但它存在于片段中。谢谢,我实际上在其片段活动中使用了它,但找不到符号方法findViewById(int)在片段中,您必须使用视图来访问findViewById方法。您必须初始化视图。类似这样:
@在CreateView上覆盖公共视图(LayoutFlater充气机、ViewGroup容器、Bundle savedInstanceState){view v=inflater.inflate(R.layout.first_fragment、容器、false);微调器sp=v.findViewById(R.id.Spinner);返回v;}
代码现在没有任何错误,但是当我单击微调器时,什么也没有发生。您是否初始化了
s.setOnItemSelectedListener
?是的,我在同一个片段类中初始化了它谢谢您,但是我如何从片段中获取它并在类MainActivity中正常使用它,而不必从fragmentAfeef wh移动微调器如果你想在主要活动中使用微调器,如果能向我解释你的任务或问题,我可能会建议你正确的解决方案。
<?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>