UI-findViewById (NullReference) 无法引用第二个视图的元素

UI-Elements of 2nd View arent referenceable by findViewById (NullReference)

我是 android-dev 的新手,遇到以下问题:
显示 ListView 项目的视图 list_excercises_item 的所有 UI-Elements 不能 findViewById() 只能在 MainActivity-文件.
我已经在自己的 ArrayAdapter 中引用了 list_excercises_itemUI-Elements

上下文:
我想使用 ArrayAdapter.

用一些数据填充我的 spinner

异常:

java.lang.NullPointerException at example.trackfit.MainActivity.onCreate(MainActivity.java:28)

这条指令抛出它:spinBodyCategory.setAdapter(arrayAdapter);
spinBodyCategorynull.


list_excercises_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" android:layout_marginBottom="10px">

<EditText
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/txt_excercise_name"
    android:hint="@string/hint_txt_excercise_name"
    android:layout_weight="0.4"
    android:inputType="text" />

<EditText
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/txt_input_weight"
    android:paddingLeft="5px"
    android:layout_weight="0.20"
    android:hint="@string/hint_txt_input_weight" />

<Spinner
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android1:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/spin_body_category"
    android:hint="@string/hint_txt_excercise_name"
    android:gravity="right"
    android:layout_weight="0.40"
    android:inputType="text" />
</LinearLayout>

MainActivity.java:

package example.trackfit;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import java.util.ArrayList;
import example.trackfit.Models.Body;
import example.trackfit.Models.Excercise;

public class MainActivity extends AppCompatActivity {

private Spinner spinBodyCategory = null;

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

    Body[] bodies = Body.values();
    ArrayAdapter<Body> arrayAdapter = new ArrayAdapter<Body>(this, R.layout.list_excercises_item, bodies);
    ListView listView = (ListView)findViewById(R.id.list_excercises);
    spinBodyCategory = (Spinner)findViewById(R.id.spin_body_category);
    spinBodyCategory.setAdapter(arrayAdapter);
}

private void populateExcerciseList() {
    ArrayList<Excercise> excercises = Excercise.getExcercises();
    CustomExcerciseAdapter adapter = new CustomExcerciseAdapter(this, excercises);
    ListView listView = (ListView)findViewById(R.id.list_excercises);
    listView.setAdapter(adapter);
    }
}

CustomExcerciseAdapter.java:

package example.trackfit;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;

import java.util.ArrayList;

import example.trackfit.Models.Excercise;

public class CustomExcerciseAdapter extends ArrayAdapter<Excercise> {

    public CustomExcerciseAdapter(Context context, ArrayList<Excercise> excercises) {
        super(context, 0, excercises);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Excercise excercise = getItem(position);
        if (convertView == null)
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_excercises_item, parent, false);
        EditText txtExcerciseName = (EditText) convertView.findViewById(R.id.txt_excercise_name);
        EditText txtInputWeight = (EditText) convertView.findViewById(R.id.txt_input_weight);

        txtExcerciseName.setText(excercise.ExcerciseName);
        txtInputWeight.setText(Float.toString(excercise.Weight));

        return convertView;
    }
}

编辑:
这个问题不是重复的,我知道 NullReferenceException 是什么。
我不知道为什么抛出这个异常,因为 - 我是 android-dev 的新手,不知道我只能访问我设置为 contentView 的视图的子视图。

因为 findViewById 是从 MainActivity.java 调用的,它正在尝试从您设置的布局中找到您的微调器:activity_main.xml。这就是为什么您会收到空指针异常。

由于列表中的每个项目都有一个微调器,因此您必须在自定义适配器中从 getView 进行设置。

尝试复制和粘贴

    Body[] bodies = Body.values();
    ArrayAdapter<Body> arrayAdapter = new ArrayAdapter<Body>(this, R.layout.list_excercises_item, bodies);
    spinBodyCategory = (Spinner)convertView.findViewById(R.id.spin_body_category);
    spinBodyCategory.setAdapter(arrayAdapter);

给你的getView