Tuesday 21 June 2011

Java Abstraction

Abstract class is a class that is declared abstract
*  it may or may not include abstract methods.
*  Abstract classes cannot be instantiated, but they can be subclassed.
*  If a class includes abstract methods, the class itself must be declared abstract.
When an abstract class is subclassed, the subclass must have implementations for all of the 
   abstract  methods.
* When an abstract class inherited from an abstract class there is no need to 
  implement its super abstract methods


Abstract method is a method that is declared without an implementation:(without braces, and followed by a semicolon)
abstract void setUserName(String name);

Friday 17 June 2011

Java Overloading

Overloading is the same method name with different parameters.
the Constructors can be overloaded
**Methods with Same name in The Class or its Subclasses with different Signature.
Signature (Parameters DataTypes or Number)ex
Class A{
public A(){....}
public A(int x){....}
public A(char x){....}
public void getSomeThing(){.....}
public void getSomeThing(char x){.....}
public void getSomeThing(String x,int y){.....}
public void getSomeThing(double x){.....}
}
Class B extends A{
public void getSomeThing(String x,char y){.....}
}
** You can pass float or double to method who's parameter datatype is double but contrary is prohibited


The overloaded methods can be static , final and or abstract.


Hint: Java uses method Parameters Types and count to specify which method.
Java doesn't differentiate between methods depending on there return DataType 


Hint: Use Overloading when you need multiple methods with different parameters, but methods do the same thing.
Don't use Overloading when methods do different tasks


For more info: http://www.java-samples.com/showtutorial.php?tutorialid=284


Thursday 16 June 2011

Null returned when using findViewById


Hello,
sometime you have nullpointerexception when using findViewById like the code snippet bellow
                TextView tv=(TextView)findViewById(R.id.textViewVV);
if(tv!=null)
tv.setText("Not Null");
this is done because you call different of the set layout
the solution is to call views from the layout you use on the current Activity
like this

setContentView(R.layout.test);
TextView tv=(TextView)findViewById(R.id.textViewVV);
if(tv!=null)
tv.setText("Good");
textViewVV is in test layout, so, this code works fine.


Tuesday 7 June 2011

Read file into byte array in j2me

import java.io.InputStreamimport java.io.OutputStreamimport javax.microedition.io.Connectorimport javax.microedition.io.file.FileConnection/**
 * This class to read file from memory and create a file on phone memory
 * @author Mohammad Abu Hmead
 * 08.03.11
 */
public class CreateAndReadFileOnAndFromMemory {

    public static final 
String FILE_LOCATIO_IN_PHOTOS=System.getProperty("fileconn.dir.photos");
    public static final 
String FILE_LOCATIO_IN_VIDEOS=System.getProperty("fileconn.dir.videos");
    public static final 
String FILE_LOCATIO_IN_TONES=System.getProperty("fileconn.dir.tones");
    public static final 
String FILE_LOCATIO_ON_MEMORY_CARD=System.getProperty("fileconn.dir.memorycard");
    
    private 
String fileLocationURL="Not specified yet";

    public 
CreateAndReadFileOnAndFromMemory(String fileLocation,String fileNameWithItsExtension)
    {
        
fileLocationURL=fileLocation+fileNameWithItsExtension;
    }
    public 
String getFileLocationURL(){
        return 
fileLocationURL;
    }
    public 
void setFileLocationURL(String fileLocationURL) {
        
this.fileLocationURL fileLocationURL;
    }

    public 
void writeIntoFile(String textToBeWrittenthrows Exception
    
{
            
FileConnection fileConnection;

            
fileConnection = (FileConnectionConnector.open(fileLocationURLConnector.READ_WRITE);

            if(!
fileConnection.exists())
                
fileConnection.create();

            
OutputStream outputStream=fileConnection.openOutputStream();
            
outputStream.write(textToBeWritten.getBytes("UTF-8"));
            
            
outputStream.flush();
            
outputStream.close();
            
fileConnection.close();
    }

    public 
boolean  createFileOnMem(byte[]  fileBsthrows Exception
    
{
        
boolean isCreated=false;
        try {
            
FileConnection fileConnection;
            
fileConnection = (FileConnectionConnector.open(fileLocationURLConnector.READ_WRITE);
            if(!
fileConnection.exists())
                
fileConnection.create();

            
OutputStream outputStream=fileConnection.openOutputStream();
            
outputStream.write(fileBs);
            
outputStream.flush();
            
outputStream.close();
            
fileConnection.close();
            
isCreated=true;
        } catch (
Exception e) {
        }
            

        return 
isCreated;
            
    }

    public 
byte[] readFileByts() throws Exception
    
{
        
byte[] filebBs=null;

        
FileConnection fileConnection = (FileConnection)
                
Connector.open(fileLocationURLConnector.READ_WRITE);
        if(
fileConnection.exists())
        {
            
filebBs=new byte[(int)fileConnection.fileSize()];
            
InputStream is=fileConnection.openInputStream();
            
is.read(filebBs0filebBs.length);
            
is.close();
            
is=null;
            
fileConnection.close();
        }
        return 
filebBs;
    }

    public 
boolean deleteTheFile()
    {
        
boolean isDeleted=false;
        
FileConnection fileConnection;
        try {
            
fileConnection = (FileConnectionConnector.open(fileLocationURLConnector.READ_WRITE);
            if(
fileConnection.exists())
            {
                
fileConnection.delete();
                
isDeleted=true;
            }
        } catch (
Exception ex) {

        }
        return 
isDeleted;
    }
}