Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Android Component is crashing application on inflate

Tags:

android

Background: I'm creating a toolkit of custom components that extend current components functionality, layout, and skin.

Current Situation: I'm trying to extend a ListView as a proof of concept. The custom list view will do nothing different then the ListView (at this point, I just want to see it get loaded correctly first).

Problem: The application is crashing when I try to assign a layout to my Activity. The layout references my custom component.

Error Message:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.fs.hello/org.fs.hello.HelloActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class org.fs.hello.HelloListView
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class org.fs.hello.HelloListView
Caused by: java.lang.NoSuchMethodException: HelloListView(Context,AttributeSet)

Code

hello.xml

<?xml version="1.0" encoding="utf-8"?>
<org.fs.hello.HelloListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hello_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

HelloActivity.java

package org.fs.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class HelloActivity extends Activity {
    private HelloMsg[] messages = new HelloMsg[] {
            new HelloMsg("Hey there!", "Nick"),
            new HelloMsg("Hey. How are you?", "Corrine"),
            new HelloMsg("I'm doing good. How about you?", "Nick"),
            new HelloMsg("Not to shabby.", "Corrine"),
            new HelloMsg("Hey guys!", "Tyler"),
            new HelloMsg("Hey Tyler", "Nick"),
            new HelloMsg("Hey Tyler", "Corrine"),
            new HelloMsg("Well I've got to go.", "Corrine"),
            new HelloMsg("See you later", "Nick"),
            new HelloMsg("Bye", "Tyler"),
            new HelloMsg("Bye", "Corrine")
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello); //Error is thrown here

        HelloListView hListView = (HelloListView)findViewById(R.id.hello_view);
        hListView.setAdapter(new ArrayAdapter<HelloMsg>(this, R.layout.list_item, messages));
    }
}

HelloListView.java

package org.fs.hello;

import android.content.Context;
import android.widget.ListView;

public class HelloListView extends ListView {
    public HelloListView(Context context) {
        super(context);
    }
}
like image 458
Spidy Avatar asked Mar 25 '11 18:03

Spidy


1 Answers

The reason for why it crashes is that android tries to construct the HelloListView (using reflection, since you have added attributes in the xml file) by calling HelloListView(Context,AttributeSet), and you have not defined that constructor.

Add:

public HelloListView(Context context, AttributeSet aSet) { super(context, aSet); }

and it will work better!

like image 103
SoftCharles Avatar answered Oct 18 '22 12:10

SoftCharles