Frage


i am new to android and not no much about java. but know the Object-oriented Concepts.
I created a custom listview from this website and i was able to somehow implement it.

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92

adapter=new CustomAdapter(CustomListView, CustomListViewValuesArr,res);<br>

The above statement was used in the article and there were screenshots. But when i gave the same statement, eclipse shows "Cannot Instantiate the type customadapter". i tried cleaning the project, but did not work. But, when i added two braces at the end, the eclipse showed no error and it worked.

adapter=new CustomAdapter(CustomListView, CustomListViewValuesArr,res){};

i dont know why?? The question is why did adding the braces work?? is it a java thing/android thing or some OOPS concept i probably don't know.

War es hilfreich?

Lösung

Are you sure you followed the example exactly? This error usually happens when you try to instantiate an interface or I believe also with an abstract class, by nature neither of which can be instantiated. Check to see if your implementation of CustomAdapter is either of those.

In your second example, you're creating an anonymous subclass/implementation of CustomAdapter and so you have a concrete instantiable class and the error goes away.

Andere Tipps

Answer is simply. CustomAdapter class doesn't exist. When you do new CustomAdapter() you are creating an object, an instance of a class classed CustomAdapter. But in your project this class doesn't exists. You have to create it, in a file called CustomAdapter.java or you have to declare inside that {}.

Adding brackets means that you are creating in a single line an object of a new class, with methods that you should write inside brackets.

In example that you have taken CustomAdapter is implemented. Try to do exactly that and all should be fine.

However, this isn't an Android thing. It's basic oop ;)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top