Eram Nansovic
Eram Nansovic

Reputation: 27

Forceclose on Inflating XML

In my app I am adding an identical linearlayout to a linearlayout already on the screen. Here is the code where I try to add it:

LinearLayout addItem=(LinearLayout)findViewById(R.id.lladdItem);
                insideScroll.addView(addItem);

I get an Error in LogCat which is posted here:

06-24 14:58:16.873  13304-13304/com.frostbytedev.randomgenie   E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.NullPointerException
        at android.view.ViewGroup.addView(ViewGroup.java:3148)
        at android.view.ViewGroup.addView(ViewGroup.java:3131)
        at com.frostbytedev.randomgenie.Test.onClick(Test.java:49)
        at android.view.View.performClick(View.java:4204)
        at android.view.View$PerformClick.run(View.java:17355)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)

And the XML that is inflated:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/lladdItem"
              android:paddingLeft="15dp"
              android:weightSum="100"
              android:layout_width="fill_parent"
              android:layout_height="80dp"
              android:orientation="horizontal">
    <TextView
            android:layout_weight="10"
            android:layout_width="10dp"
            android:layout_height="80dp"
            android:text="2."
            android:id="@+id/tvItem2"/>
    <EditText
            android:layout_weight="90"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:hint="List Item 2"
            android:id="@+id/etItem2"
            android:paddingTop="50dp"/>

</LinearLayout>

XML where it is inflated to:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="465dp"
            android:id="@+id/svItems"
            android:layout_gravity="center"
            >
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:orientation="vertical"
                android:id="@+id/insideScroll">

            <LinearLayout
                    android:paddingLeft="15dp"
                    android:weightSum="100"
                    android:layout_width="fill_parent"
                    android:layout_height="80dp"
                    android:orientation="horizontal">
                <TextView
                        android:layout_weight="10"
                        android:layout_width="10dp"
                        android:layout_height="80dp"
                        android:text="1."
                        android:id="@+id/tvItem1"/>
                <EditText
                        android:layout_weight="90"
                        android:layout_width="100dp"
                        android:layout_height="80dp"
                        android:hint="List Item 1"
                        android:id="@+id/etItem1"
                        android:paddingTop="50dp"/>

            </LinearLayout>
            <LinearLayout
                    android:paddingLeft="15dp"
                    android:weightSum="100"
                    android:layout_width="fill_parent"
                    android:layout_height="80dp"
                    android:orientation="horizontal">
                <TextView
                        android:layout_weight="10"
                        android:layout_width="10dp"
                        android:layout_height="80dp"
                        android:text="2."
                        android:id="@+id/tvItem2"/>
                <EditText
                        android:layout_weight="90"
                        android:layout_width="100dp"
                        android:layout_height="80dp"
                        android:hint="List Item 2"
                        android:id="@+id/etItem2"
                        android:paddingTop="50dp"/>

            </LinearLayout>
        </LinearLayout>
    </ScrollView>
<LinearLayout
        android:layout_height="50dp"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:weightSum="100">
    <Button
            android:layout_weight="50"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add"
            android:id="@+id/bAdd"/>

    <Button
            android:layout_weight="50"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Remove"
            android:id="@+id/bRemove"
            android:layout_gravity="center"/>
    </LinearLayout>
</LinearLayout>

Java:

package com.frostbytedev.randomgenie;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Steven on 6/23/13.
 */
public class Test extends Activity implements View.OnClickListener{
    java.util.List<TextView> listOfET = new ArrayList<TextView>();
    LinearLayout insideScroll;
    ScrollView svItems;
    TextView etItem1, etItem2;
    Button Add, Remove;
    int numOfItems = 2, width, height;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynamictest);
        initialize();
    }

    private void initialize() {
        Add=(Button)findViewById(R.id.bAdd);
        Remove=(Button)findViewById(R.id.bRemove);
        insideScroll=(LinearLayout)findViewById(R.id.insideScroll);
        etItem1=(TextView)findViewById(R.id.etItem1);
        etItem2=(TextView)findViewById(R.id.etItem2);
        svItems=(ScrollView)findViewById(R.id.svItems);
        Add.setOnClickListener(this);
        Remove.setOnClickListener(this);

        listOfET.add(etItem1);
        listOfET.add(etItem2);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.bAdd:
                numOfItems += 1;
                LinearLayout addItem=(LinearLayout)findViewById(R.id.lladdItem);
                insideScroll.addView(addItem);

                break;
            case R.id. bRemove:

                listOfET.remove(numOfItems);
                numOfItems -= 1;
                break;

        }
    }

    private int getDP(float i) {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        float dp = i;
        float fpixels = metrics.density * dp;
        int pixels = (int) (fpixels + 0.5f);
        return pixels;
    }

}

Upvotes: 0

Views: 72

Answers (2)

Dan Breslau
Dan Breslau

Reputation: 11522

Since your code (Test.java:49) is a couple of calls down the stack from the site of the NPE, it's clear that insideScroll is not null there. That leaves addItem as the likely culprit. And the way I read your XML, the call findViewById(R.id.lladdItem) has to return null, because lladdItem is not defined in the XML file that defines your content view (dynamictest.xml).

What I believe you need to do is to inflate lladditem, rather than looking for it where it doesn't exist:

            LayoutInflater inflater = LayoutInflater.from(view.getContext());
            LinearLayout addItem = 
                (LinearLayout) inflater.inflate(R.layout.lladditem, null);
            insideScroll.addView(addItem);

Upvotes: 2

codeMagic
codeMagic

Reputation: 44571

Correct me if I am wrong, there is a lot to look through, but it looks like the item you are trying to add is in a different xml file than the one that you inflate with setContentView() and I don't see where you inflate that Layout so you would need to do that or that LinearLayout will be null

Now, that can't create a NPE at insideScroll.addView(addItem); and from what I can see it is initialized properly. I would try cleaning your project in case the editor didn't pick up changes to your xml.

Window --> Project --> Clean...   

and choose your project

Upvotes: 0

Related Questions