如何在mainActivity中使用findViewById和片段?

问题描述 投票:0回答:1

[我看了其他有关此问题的问题,我了解到我的问题在那里,因为我试图找到不在实际显示视图中的视图(我想)。

但是我不知道如何解决此问题。

这里是我的MainActivity.java,其中出现了final ListView list...错误

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private Context context = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //-------------------------------------- Apero list ---------------------------
        ArrayList<AperoVersion> aperoList = new ArrayList<AperoVersion>();
        initList(aperoList);
        AperoAdapter adapter = new AperoAdapter(this, R.layout.apero_list_layout, aperoList);
        final ListView list = (ListView)findViewById(R.id.list_apero);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
                AperoVersion selectedItem = (AperoVersion) adapter.getItemAtPosition(position);
                Log.v("CustomAdapterExemple", "Selected apero: " + selectedItem.getName());
            }
        });
    }

然后我有了fragment_home

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.home.HomeFragment">

    <ListView
        android:id="@+id/list_apero"
        android:layout_width="407dp"
        android:layout_height="598dp"
        android:layout_marginTop="136dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        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"
        app:layout_constraintVertical_bias="0.043" />
</androidx.constraintlayout.widget.ConstraintLayout>

错误是:

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.lapero / com.example.lapero.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'voidandroid.widget.ListView.setAdapter(android.widget.ListAdapter)'空对象引用

java android android-fragments findviewbyid
1个回答
1
投票

[list_aperofragment_home中,而不在activity_main中。

活动中的[findViewById(R.id.list_apero)返回空值。

您应该在片段中而不是在活动中设置适配器。

© www.soinside.com 2019 - 2024. All rights reserved.