Sunday 21 August 2011

Why do we need non-static inner class in java


Non static inner classes can reach to its enclosing class elements because they are dynamic elements of their enclosing classes. You can do that.

class A {
     public void method(){}
     public class B {
     method(); // cannot do this if you define B as 'public static class B'
     }
}

And in this case (non-static inner class). If you d like to create an instance of you inner class (B) in some other class, you should instantiate it like below.

class C {
     public C(){
     A.B b = new A().new B(); // B has the same characteristic like other non-static class elements
     }
}

On the other hand if you aren't going to reach to the enclosing class elements from the inner class, you can define your inner class as static class. In this case you don't need to create an instance of enclosing class to create an instance of the inner class.


class C {
     public C(){
     A.B b = new A.B(); // this is a valid instantiation of a B's instance.
     }
}

Wednesday 3 August 2011

textview cannot be resolved to a type

There is that weird stuff in android. I have list_item.xml which is below

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
></TextView>

and ArrayAdapter(view:View,textviewId:Int,items:String[]) wants a textview into 2nd parameter I was passing R.id.text1 as parameter but I needed to pass the layout (R.layout.list_item) instead of that textview. So why do you retrun 'textview cannot be resolved to a type' error to me? An example to confusing exceptions.