Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8388719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:32:00+00:00 2026-06-09T18:32:00+00:00

I am new to Android and am trying to understand the compound view concept

  • 0

I am new to Android and am trying to understand the compound view concept if android. I am not sure that my below implementation is practical or not but it is for learning purpose and i have taken this idea from Wrox Android book. So, i have three components i.e a button, a edit text and a list view. The purpose of edit text is to write some text, while the list gets updated with the text and the button is the clear button and it is suppose to clear the edit text. What i am planning to do is to keep the clear button and edit text in one view group. Below it the java and xml code for it.

public class CompoundView extends LinearLayout {

EditText editText;
Button clearButton;

public CompoundView (Context context, AttributeSet attr) {
    super(context, attr);
    String infService = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater li;

    li = (LayoutInflater)getContext().getSystemService(infService);
    li.inflate(R.layout.clearable_edit_text, this, true);

    editText = (EditText) findViewById(R.id.editText);
    clearButton = (Button) findViewById(R.id.clearButton);

    hookUpButton ();
}

public void hookUpButton () {

    clearButton.setOnClickListener(new Button.OnClickListener () {
        public void onClick (View v) {
            editText.setText ("");

        }
    });
}

public EditText getEditText() {
    return this.editText;
}

}

XML

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText 
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/clearButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Clear"
    />
</merge>

I have a strong feeling that this implementation is fine. In the next part I have the MainActivity.java and activity_main.xml. In this i am facing the problem and am most probably doing some thing wrong.

public class MainActivity extends Activity {

@SuppressLint({ "ParserError", "ParserError" })
public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView myListView = (ListView)findViewById(R.id.myListView);

    final CompoundView textAndClear = (CompoundView) findViewById(R.id.myEditAndClear) ;
    final EditText editText = textAndClear.getEditText();
    final ArrayList<String> toDoItems = new ArrayList<String>();

    final ArrayAdapter<String> aa;
    int resId = R.layout.todolist_item;
    aa= new ArrayAdapter<String> (this, resId, toDoItems);
    myListView.setAdapter(aa);

    editText.setOnKeyListener(new View.OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(event.getAction() == KeyEvent.ACTION_DOWN)
                if ((keyCode == KeyEvent.ACTION_DOWN) || (keyCode == KeyEvent.KEYCODE_ENTER)){
                    toDoItems.add(0,editText.getText().toString());
                    aa.notifyDataSetChanged();
                    editText.setText("");
                    return true;
                }
            return false;
        }
    });
    }
}

and the XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.example.wroxexample.CompoundView
    android:id="@+id/myClearableEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
<ListView
    android:id="@+id/myListView"
    android:layout_below="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</RelativeLayout>

and the error is

06-29 06:32:11.783: E/AndroidRuntime(5413): FATAL EXCEPTION: main
06-29 06:32:11.783: E/AndroidRuntime(5413): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.wroxexample/com.example.wroxexample.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.wroxexmple.CompoundView
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.os.Looper.loop(Looper.java:137)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at java.lang.reflect.Method.invoke(Method.java:511)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at dalvik.system.NativeStart.main(Native Method)
06-29 06:32:11.783: E/AndroidRuntime(5413): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.wroxexmple.CompoundView
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.Activity.setContentView(Activity.java:1835)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at com.example.wroxexample.MainActivity.onCreate(MainActivity.java:20)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.Activity.performCreate(Activity.java:4465)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-29 06:32:11.783: E/AndroidRuntime(5413):     ... 11 more
06-29 06:32:11.783: E/AndroidRuntime(5413): Caused by: java.lang.ClassNotFoundException: com.example.wroxexmple.CompoundView
06-29 06:32:11.783: E/AndroidRuntime(5413):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.createView(LayoutInflater.java:552)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-29 06:32:11.783: E/AndroidRuntime(5413):     ... 21 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:32:01+00:00Added an answer on June 9, 2026 at 6:32 pm

    Below the error you posted should be the real cause for the exception. My guess, after a simple look at your code, is that the exception comes from the fact that you only implemented the constructor that takes a Context(this constructor would normally be used when you instantiate the View in code). As you use the compound view in a xml layout the constructor that also takes an AttributeSet will be used(instead of the one you implemented that only takes a Context).

    Also, as a side note you search for your compound View in the onCreate method and you cast it to a LinearLayout. In the onKey callback you then try to call the getText() and setText() on it, methods which are not implemented for the class LinearLayout(you maybe had them in your custom view but if you cast the view as a LinearLayout the methods will not be available).

    Also you might want to read on the merge tag to improve the layout for your compound View.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to Android programming and am trying to understand the concept of
I'm still new to programming/Java/Android so I'm trying to understand everything I do and
I'm trying to understand Android's View structure and I'm a little confused on how
I understand that flash is quite new to android. Has anyone actually used flash
I'm trying to do something simple, but I can't understand why it's not working.
I am new to Android programming and am trying to understand the best practices.
I am new to Android and I am currently trying to understand how to
I'm new to Android(and Java also) and I'm trying to understand what are Fields
I'm new to android but I've done the notepad tutorial. Now I'm trying to
I am trying to understand the Android PackageManager (functionality and uses) and the new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.