0

Heyho, I'm pretty new to java programming for android and i hope you can help me.

Here is the method that's giving me nightmares:

    public boolean CheckInput(View v) {

    if (v.getId() != R.id.job_hunting_btn) {

    LinearLayout layout = (LinearLayout)findViewById(R.id.job_hunt_lay);

            for (int i = 0; i < layout.getChildCount(); i++) {

                View view = layout.getChildAt(i);
                Rahmen(view);
            }
        }
    }

I basically want to iterate through all the Children of my given Linear Layout and do something with them.

this is the XML file for that specific layout:

<?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:id="@+id/job_hunt_lay"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="de.ohm.work4refugees.MainActivity">

<TextView
android:id="@+id/workas_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="@string/workas"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/workas_et"
android:layout_weight="1" />

<TextView
android:id="@+id/experience_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="@string/experience"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/experience_et"
android:layout_weight="1" />

<TextView
android:id="@+id/native_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="@string/nativel"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/native_et"
android:layout_weight="1" />

<TextView
android:id="@+id/communicate_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="@string/communicate"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/communicate_et"
android:layout_weight="1" />

<TextView
android:id="@+id/erwerbstaetigkeit_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="@string/erwerbstaetigkeit"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/erwerb">

    <RadioButton
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:text="yes"
        android:layout_weight="0.2"
        android:layout_gravity="start"
        android:scaleType="fitCenter"
        android:rotationY="10"
        android:onClick="OnButtonClick"
        android:id="@+id/yes_btn"/>

    <RadioButton
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:text="no"
        android:layout_weight="0.2"
        android:layout_gravity="center"
        android:scaleType="fitCenter"
        android:rotationY="10"
        android:onClick="OnButtonClick"
        android:id="@+id/no_btn"/>

    <RadioButton
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:text="i don't know"
        android:layout_weight="0.2"
        android:layout_gravity="end"
        android:onClick="OnButtonClick"
        android:id="@+id/dontknow_btn"/>

</RadioGroup>


<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/job_hunting_btn_1"
android:src="@drawable/btn_auswahl_rechts"
android:layout_weight="0.5"
android:padding="5dp"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:rotationY="10"
android:layout_gravity="right"
android:onClick="OnNextButtonClick"
/>
</LinearLayout>

But all i get is a NullpointerException at for (int i = 0; i < layout.getChildCount(); i++) .

Can you help me ? (p.s: of course you noticed that there are no return values in my boolean method, thats because the method is actually a lot longer but i didn't post the whole thing, only the part that throws the exception)

EDIT: CheckInput is called here

 public void OnNextButtonClick(View v) {
    if (CheckInput(v)) {
        ViewList.get(CurrentViewId).setVisibility(View.GONE);
        if (v.getId() == R.id.job_hiring_btn)
            CurrentViewId = ArbeitgeberViewId;
        else {

            CurrentViewId++;
            if (CurrentViewId == ArbeitgeberViewId)
                CurrentViewId = LastViewId;
        }
        ViewList.get(CurrentViewId).setVisibility(View.VISIBLE);
    }
}

I want to load the next page only if the edittext input is checked and proven not empty, what would happen in Rahmen()

kalu
  • 169
  • 1
  • 11
  • When are you calling Checkinput? Can it be that you call it before it finish inflating your layout? – fbwnd Nov 21 '16 at 17:47
  • Apparently your `layout` is null then you are trying to getChildCount. You should try doing this after the layout was inflated, check this http://stackoverflow.com/a/7735122/3060279 and invoke your method from inside the listener – lxknvlk Nov 21 '16 at 17:47
  • @lxknvlk but why is it null? Since I'm doing LinearLayout layout = (LinearLayout)findViewById(R.id.job_hunt_lay); – kalu Nov 21 '16 at 17:51
  • @fbwnd sorry for the stupid question, but im not really experienced. How do I know when my layout is inflated? – kalu Nov 21 '16 at 17:56
  • Because when you run code from the onCreate method, views are not inflated yet, i suppose. – lxknvlk Nov 21 '16 at 17:56
  • @kalu Take a look at: http://stackoverflow.com/questions/3264610/findviewbyid-returns-null In case you are calling it after setContentView() it should be fine though. If you will add your code (the part where you call the function), I will be able to help you. – fbwnd Nov 21 '16 at 18:00

0 Answers0