简体   繁体   English

android中两个语句之间的区别

[英]The difference between the two statements in android


i am new to android and not no much about java. 我是android的新手,对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 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". 但是当我给出相同的语句时,eclipse显示“无法实例化类型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. 是Java事物/ Android事物还是某些OOPS概念,我可能不知道。

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. 检查您的CustomAdapter实现是否是其中之一。

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. 在第二个示例中,您正在创建CustomAdapter的匿名子类/实现,因此您有了一个具体的可实例化类,并且错误消失了。

Answer is simply. 答案很简单。 CustomAdapter class doesn't exist. CustomAdapter类不存在。 When you do new CustomAdapter() you are creating an object, an instance of a class classed CustomAdapter. 当您执行new CustomAdapter()您将创建一个对象,即一个名为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 {}. 您必须在名为CustomAdapter.java的文件中创建它,或者必须在该{}中声明。

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. 在示例中,您实现了CustomAdapter。 Try to do exactly that and all should be fine. 尝试做到这一点,一切都很好。

However, this isn't an Android thing. 但是,这不是Android设备。 It's basic oop ;) 这是基本的哎呀;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM