Sunday, August 30, 2015

[Laravel5][Resolved] Class 'App\Http\Controllers\Auth' not found


Error messgae (Laravel version 5.1):
FatalErrorException in ArticlesController.php line 63: Class 'App\Http\Controllers\Auth' not found

Go to your Controller and add use Auth at the top but under the namespace declaration statement.

use Auth;

Reference:
https://laracasts.com/discuss/channels/general-discussion/class-apphttpcontrollersauth-not-found?page=1

Thursday, August 27, 2015

[Android][Resolved] android make ImageView fit screen width

Add this line before you use your ImageView.
imgview.setScaleType(ScaleType.FIT_XY);

Example:
File wallpaperFile = new File(targetDir + "/wallpaper.jpg");
Uri wallpaperDir = Uri.fromFile(wallpaperFile);
ImageView testView = (ImageView) findViewById(R.id.testView);
Bitmap wallpaperBitmap = BitmapLoader.loadBitmap(wallpaperDir.getEncodedPath(), 200, 200);
testView.setScaleType(ImageView.ScaleType.FIT_XY);
testView.setImageBitmap(wallpaperBitmap);

Reference:
http://stackoverflow.com/questions/4668001/android-stretch-image-in-imageview-to-fit-screen

[Android][Resolved] Remove remove title bar in android



Add this line at onCreate() method
 requestWindowFeature(Window.FEATURE_NO_TITLE);
Sample Code:



Reference:
http://stackoverflow.com/questions/14475109/remove-android-app-title-bar

Wednesday, August 26, 2015

[Android] Convert uri to bitmap

Do not use toString().
uripath.toString()

Use getEncodedPath() get the encoded uri path, gives you an encoded string.
uripath.getEncodedPath()

I use BitmapFactory to convert uri file to bitmap :
Bitmap bitmap = BitmapFactory.decodeFile(cacheDir.getEncodedPath());
avatarView.setImageBitmap(bitmap);

Reference:
http://stackoverflow.com/questions/7022306/uri-tostring-returns-a-reference-instead-of-the-string
http://developer.android.com/reference/android/net/Uri.html#getEncodedPath()

Tuesday, August 25, 2015

[Android][Resolved] java.lang.IllegalArgumentException: Unterminated quote

Error Message :

Double check see if you got any illegal quote in your code, for example:
SimpleDateFormat dateFormat = new SimpleDateFormat("'_yyyyMMdd_HHmmss");
        fileName = filename + dateFormat.format(date) + ".jpg";

Solution is removing the single quote highlighted in yellow and in red at the code shown above.
SimpleDateFormat dateFormat = new SimpleDateFormat("_yyyyMMdd_HHmmss");
        fileName = filename + dateFormat.format(date) + ".jpg";

Monday, August 24, 2015

[Android] Android Studio show lines in editor

Step 1 : Click "File" and then "Settings" in main menu.

 

Step 2 : Select "Editor" at left panel, click "Show line numbers" and then the "OK" button.


And the line numbers in editor would be shown.

[Android][Solved with image] ListView Background becomes strange color when scrolling

This is the original color, the background is black, text in white.

However, if you scroll the screen, the ListView,
background of listView would change to some strange color.
(For my case, to white, but my text are in white)


android:scrollingCache="false"

If disable the scrollingCache,
works but found it would be quite slow .
About the reason why is cos scrolling Cache helped to cache the
http://android-developers.blogspot.hk/2009/01/why-is-my-list-black-android.html

For me, i would more suggest using set cacheColorHint to transparent so that the strange color i memtioned at title won't affect your layout.

android:cacheColorHint="#00000000"


Reference.
http://stackoverflow.com/questions/2833057/background-listview-becomes-black-when-scrolling

Sunday, August 23, 2015

[Android] Check android device serial number and status in windows 7

you can use the command adb devices to check device serial number and status. 

adb devices


in my case, the command return result:
line 1)   List of devices attached
line 2)   0a204f7b            device
in line 2, the first set of number and letters is your android device serial number which connected to computer with windows platflat. the second set is the device static, it could be "device" or "offline", "offline" means the android with the specified serial number haven't connect to the computer, or no any response. "device" means the android device was connected to computer only.

Sunday, August 16, 2015

[Android][Resolved] Cannot resolve symbol @string/app_name in AndroidManifest-xml in Android Studio

Check "File" and then Invalidate Caches/ Restart


Select Invalidate and Restart


Then Android Studio will restart and the problem should be disappeared.

Reference:
http://stackoverflow.com/questions/18927972/my-debug-androidmanifest-xml-is-giving-me-cannot-resolve-symbol-errors

Wednesday, August 12, 2015

Thursday, August 6, 2015

[Java][Resolved] Cannot reference a field before it is defined

Error message :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    at static1.main(static1.java:12)

class static1 {
    static {
        ++staticVar;
    }
    static int staticVar ;
    static {
        ++staticVar;
    }
    public static1() {
        System.out.println("Constructor:" + staticVar);
    }
    public static void main(String args[]) {
        new static1();
    }
}
The first static initiation block cannot get the static valuable “staticVar” during the compilation, this causes the compilation error. To solve that, define the static valuable “staticVar” before using that.

class static1 {
    static int staticVar ;
    static {
        ++staticVar;
    }
    static {
        ++staticVar;
    }
    public static1() {
        System.out.println("Constructor:" + staticVar);
    }
    public static void main(String args[]) {
        new static1();
    }
}

Wednesday, August 5, 2015

[Java][Resolved] Cannot make a static reference to the non-static method count() from the type xxxx

class static1{
    static int x = count();
    int count(){return 10;}    
    public static void main(String args[]){
        static1 static1 = new static1();
        System.out.print(static1.count());
    }
}
Error message:   
Cannot make a static reference to the non-static method count() from the type static1

Since static methods and variables can’t assess the methods which is not be created, can change modifier of count() to static, so that the static variable referencing a static method ().

Solution :
class static1{
    static int x = count();
    static int count(){return 10;}
    public static void main(String args[]){
        static1 static1 = new static1();
        System.out.print(static1.count());
    }
}

[Java][Resolved] Type mismatch: cannot convert from int to short

Incorrect sample:
public class static1{
    public static void main(String[] args){
        short s1 = 1;
        s1 = s1 + 1;
        System.out.println(s1);
    }
}

s1 +1 would return int, int can’t assign to short. Solution is converting the operation result from int type to short before assign to s1.

Correction:
public class static1{
    public static void main(String[] args){
        short s1 = 1;
        s1 = (short) (s1 + 1);
        System.out.println(s1);
    }
}

Remark :
It’s also work:
public class static1{
    public static void main(String[] args){
        short s1 = 1;
        s1 += s1;
        System.out.println(s1);
    }
}


Reference:
http://way2java.com/casting-operations/java-int-to-short/
http://619lucky.blogspot.hk/2010/08/java.html

Sunday, August 2, 2015

[Java] Static initialization block

Static initialization block preceded with the “static” keyword, is a block of code in braces {}.
Static {
    //do sth here
}
A class can have any number of static initialization block, no matter how many objects of that type you create and can appear anywhere in class body. The static block only gets called once no matter how many objects of that type you created, and it would be executed when class is loaded by class loader.

Example 1:

This example show static initialization block would be executed when class is loaded by class loader.
public class static1 {
    public static void main(String[] args) {}
    static {System.out.print("d ");}
}
Result 1:
d

Example 2:

The static initialization block can have any number of static initialization block, and would be executed one by one from top to bottom.
public class static1 {
    static {System.out.print("c ");}
    public static void main(String[] args) {}
   static {System.out.print("d ");}
   static {System.out.print("e ");}
   static {System.out.print("f");}
   static {System.out.print("g");}
}
Result 2:
c d e f g

Example 3: 

This example show static initialization block only gets called once no matter 3 objects of that type you created.
public class static1 {
    static {System.out.print("c ");}
    public static void main(String[] args) {
        new static1();
        new static1();
        new static1();
    }
    static {System.out.print("g ");}
}
Result 3:
c g

Reference:
http://stackoverflow.com/questions/2420389/static-initialization-blocks
http://stackoverflow.com/questions/13699075/calling-a-java-method-with-no-name

Saturday, August 1, 2015

[Java] Instance initialization block

Instance initialization block runs before the constructor each time you instantiate an object.

Example1

public class static1 {
    static1() {
        System.out.print("c ");
    }   
    public static void main(String[] args) {
        new static1();
    }
    {System.out.print("x ");}
}

Result:
x c
Reamrk:
This example show Instance initialization block runs before the constructor.

Example2

public class static1 {
    static1() {
        System.out.print("c ");
    }   
    public static void main(String[] args) {
        new static1();
        new static1();
        new static1();
    }
    {System.out.print("x ");}
    static {System.out.print("y ");}
}
Result:
y x c x c x c
This example show Instance initialization block runs each time when object with that type was created. And static initialization block run once only before Instance initialization block (when the class loader load the class.

What is a static initialization block: [Java] static initialization block

Reference:
http://stackoverflow.com/questions/3987428/what-is-an-initialization-block