Trying to inflate layout in Activity not working

EGHDK picture EGHDK · Jan 21, 2014 · Viewed 7.2k times · Source

I'm trying to add a custom view that I made to an adapter of mine. I'm trying to do this to add a view to merge adapter by commonsware.

https://github.com/commonsguy/cwac-merge

This is my code of me trying to inflate the layout:

    LayoutInflater inflater = getLayoutInflater();
    LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row,
            null);


        TextView headerOne = (TextView) row
                .findViewById(R.id.titleTextView);
        TextView headerTwo = (TextView) row
                .findViewById(R.id.titleTextView);
        headerOne.setText("One");
        headerTwo.setText("Two");

        mergeAdapter.addView(headerOne);
        mergeAdapter.addAdapter(myArrayAdapter);

        mergeAdapter.addView(headerTwo);
        mergeAdapter.addAdapter(myOtherArrayAdapter);

This is my xml.

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:layout_weight="1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/titleTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-condensed"
        android:text="message text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/text_gray"
        android:textStyle="bold" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="#000000" />
</LinearLayout>

Update: Forgot to talk about what my "problem" is. First, is that I've never inflated a layout before so I'm unsure of how it works/whats the proper way to inflate. Second, I get this error:

01-20 23:18:46.427: E/AndroidRuntime(24810): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

EDIT:

My imports:

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.commonsware.cwac.merge.MergeAdapter;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import com.eghdk.myapp.R;
import com.eghdk.myapp.util.AppUtil;
import com.eghdk.myapp.util.DatabaseHelper;

public class MyActivity extends SherlockListActivity {

Answer

CommonsWare picture CommonsWare · Jan 21, 2014

Use two layout resources, not one. Do not inflate a layout, then attempt to take pieces of that layout and use them separately.

Or, to put it another way, the value you pass to addView() needs to be either:

  • the direct result from inflate(), or
  • something you create in Java using constructors