0

So,i have my MainActivity with its layout activity_main.xml.From this Activity,when a Print button is pressed i want that activity to send the data that the users inputs,and add TextViews to my lista.xml that will get turned into a bitmap and then sent to my receipt printer.

After some headache with getting a NullPointerException i've learned that i have to do something like this setContentView(R.layout.lista); before doing this ll1= (LinearLayout) findViewById(R.id.layoutlista1);. The problem is that this switches the layout i see,when using setContentView it shows my lista.xml.I guess i could fix this by using setContentView(R.layout.activity_main) but i'm pretty sure this isn't how things should be done to accomplish what i want.

So my question is,how do you add Views to another layout from the current activity layout without getting a nullPointerException ?

This is my lista.xml layout :

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="S.C. INTER S.R.L."
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="GALATI" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="Data:" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="-----------------------------------------" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1000" >

    <LinearLayout
        android:id="@+id/layoutlista1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="500"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nume produs" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutlista2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="250"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cantitate" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutlista3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="250"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Pret" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:text="-----------------------------------------" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TOTAL: " />

    <TextView
        android:id="@+id/totallista"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0.0" />
</LinearLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="-----------------------------------------" />

This is how i initialize the layouts :

    public LinearLayout ll1;
public LinearLayout ll2;
public LinearLayout ll3;
    setContentView(R.layout.lista);
ll1= (LinearLayout) findViewById(R.id.layoutlista1);
ll2= (LinearLayout) findViewById(R.id.layoutlista2);
ll3= (LinearLayout) findViewById(R.id.layoutlista3);
Sorin Grecu
  • 1,036
  • 11
  • 30
  • 1
    I think you want to use `LayoutInflater` to load some layout and add it as a child to some other layout. But I don't understand which layout should be included where. – zapl Jul 11 '13 at 00:29
  • When i open my app,the main activity launches with its layout `activity_main`.In that activity i have a button that must add TextViews to some layouts inside another layout called `lista.xml`.Though i intialize the layouts i get nullPointerExceptions on them.Why is that ? – Sorin Grecu Jul 11 '13 at 00:38
  • 2
    `Activity#findViewById()` will only find a view in the view hierarchy you've set with `setContentView`. It won't find it in another layout. But you can load the other layout via `LayoutInflater` (returns a `View`) and then use `view.findViewById()` on the layout you've loaded there. That layout does not need to be visible for that to work – zapl Jul 11 '13 at 00:41
  • That sounds like what i need ! I was just researching LayoutInflaters but i couldn't find what i need.Could you please give me an answer with an example of what i should do ? – Sorin Grecu Jul 11 '13 at 00:45

1 Answers1

2

Umh if you're still want to know the answer then you could read from this post How to inflate one view with a layout.

In case you're still wonder about how to initialize an inflater then this would be another way

LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

I've get the idea from Inflate a view / layout into another layout?. I'll try to wrote it again and hope this could help. Did not check it's actually working or not

//at first add contain view to main layout
setContentView(R.layout.activity_main);

//set reference to recently mail layout. Set id to your main layout
//change it to watever LinerLayout ...etc
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.your_main_layout);

// create inflater 
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// infalate lista and infalate it to root - main layout in this case.
// check http://developer.android.com/reference/android/view/LayoutInflater.html for more

View view = (View) inflater.inflate(R.layout.lista, mainLayout, true);

// doing blah blah with view

Well I did not check is it work or not but IMO it should work.

Community
  • 1
  • 1
Van Vu
  • 193
  • 1
  • 8
  • I've done this and it doesn't return NPE but it doesn't add the view either ` View view; LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.lista, null); LinearLayout ll1 = (LinearLayout) view.findViewById(R.id.layoutlista1); TextView temporar=new TextView(this); temporar.setText("TEST"); ll1.addView(temporar);` – Sorin Grecu Jul 11 '13 at 01:45
  • ll1 now inflated ... you've created temporar view and add it to ll1. But did you add ll1 view which inflated to ContentView ??? – Van Vu Jul 11 '13 at 02:54