带有微调器和上下文的 Probelm - Android Java

Probelm with spinner and context - Android Java

提问人:Remek 提问时间:10/18/2022 最后编辑:TheLibrarianRemek 更新时间:10/27/2022 访问量:27

问:

在向应用程序添加微调器后,我无法启动应用程序(应用程序冻结)。 它可能没有让我正确地“上下文”——可能。 也许问题出在别处,但我无法在我的阶段验证它 在添加 Spinner 之前,一切正常。 Git 应用代码:https://github.com/RemekLago/project_Factory2.git

我将不胜感激任何帮助。

主要活动代码:

package com.example.projecttech_v4;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager2 viewPager2;
    private PagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabLayout = findViewById(R.id.tabLayout);
        viewPager2 = findViewById(R.id.viewPager2);

        tabLayout.addTab(tabLayout.newTab().setText("Maszyna01"));
        tabLayout.addTab(tabLayout.newTab().setText("Maszyna02"));
        tabLayout.addTab(tabLayout.newTab().setText("Maszyna03"));
        tabLayout.addTab(tabLayout.newTab().setText("Maszyna04"));
        tabLayout.addTab(tabLayout.newTab().setText("Maszyna05"));
        tabLayout.addTab(tabLayout.newTab().setText("Maszyna06"));

        FragmentManager fragmentManager = getSupportFragmentManager();
        adapter = new PagerAdapter(fragmentManager, getLifecycle());
        viewPager2.setAdapter(adapter);

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager2.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position){
                tabLayout.selectTab(tabLayout.getTabAt(position));
            }
        });

        Spinner spinner1 = findViewById(R.id.spinner1_operator);
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource
                (this, R.array.operator, android.R.layout.simple_spinner_item);

        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);

    }
}

code: XML 主要活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:orientation="vertical">

        <Spinner
            android:id="@+id/spinner1_operator"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Spinner
            android:id="@+id/spinner2_data"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Spinner
            android:id="@+id/spinner3_godzina"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Button
            android:id="@+id/button_zatwierdz_operator"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Zatwierdź"
            android:textSize="11sp"
            android:textColor="@color/black"
            android:layout_weight="1"/>


    </TableRow>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:tabGravity="fill"
        app:tabIndicatorAnimationMode="elastic"
        app:tabIndicatorGravity="stretch"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/white" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>
Android 这个 微调器

评论


答:

0赞 snachmsm 10/18/2022 #1

TableRow应该是 Only 的子项。exchange to with 属性TableLayout<TableRow<LinearLayoutandroid:orientation="horizontal"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!-- TableRow becomes LinearLayout -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp">
        
        <!-- spinners and rest of code -->

Powodzenia