user3698624 user3698624 - 4 months ago 8
Java Question

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

I am new to android-dev and experiencing the following problem:

All

UI-Elements
of the View
list_excercises_item
, which shows a ListView's item, are not referenceable by
findViewById()
only in the
MainActivity
-file.

I do already reference
UI-Elements
of the
list_excercises_item
in an own
ArrayAdapter
.

Context:

I want to fill my
spinner
with some data using an
ArrayAdapter
.

Exception:


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


This instruction throws it:
spinBodyCategory.setAdapter(arrayAdapter);


spinBodyCategory
is
null
.





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;
}
}


Edit:

This question is not a duplicate, I ofc know what a
NullReferenceException
is.

I did not know why this exception was thrown, since - I am new to android-dev and didnt know that I can only access childs of the View I set as my
contentView
.

Answer

Because findViewById is called from MainActivity.java, it is trying to find your spinner from the layout you set: activity_main.xml. Which is why you are getting a null pointer exception.

Since you have a spinner for each item on your list, you will have to set it from getView in your custom adapter.

Try copying and pasting

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

to your getView