Results 1 to 9 of 9
  1. #1
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Question access method of main class (android)

    Hi to all,

    I am very new to java and I am trying to create a small android app which I need to be Gesture sensitive. My problem is that that I have implemented an onGestureListener class like:
    Java Code:
    public class Gestures implements OnGestureListener{
    ...
    ...
    ...
    @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){ 
            onFling();
            return true;
        }
    }
    and I need to access a method of my main Activity. The method is public and is implemented like:
    Java Code:
    public void onFling(){
        //do something...
        }
    My onGestureListener class doesn't recognize the onFling() method of my (main)Activity class and only accepts a boolean as return.
    Is there someone that could help me surpass this issue or should I address a more android specific forum?

    Any help would be greatly appreciated,
    I thank you in advance,
    George.

  2. #2
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    0

    Default

    Ok. It sounds like your Gestures and Actvity classes are separate classes. So how is the Gestures class supposed to access the onFling method in Activity? By your code below, Gestures has no relationship to Activity. If you want to give Gestures access, it's going to need an Activity object in it (or passed to it). Maybe something like this (just taking a stab here):


    Java Code:
    public class Gestures implements OnGestureListener{
    ...
    [B]Activity aObj;[/B]
    ...
    @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){ 
            [B]aObj.[/B]onFling();
            return true;
        }
    }

  3. #3
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    You are probably right... Being new I thought that just because they belong in the same package, they would have access to each other.

    Now, Activity in android is the equivalent of a class containing public static void main(String[] args){...} so instantiating it inside a subclass does't seem the right solution.
    It sounds like your Gestures and Actvity classes are separate classes
    So would it be more appropriate to nest the classes in my case? And if yes, could you direct me to some documentation that explains the procedure?

    Thank you.

  4. #4
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    Trying to clarify my case a bit more, I have to state that I have instantiate Gestures inside my Activity. Gestures implements onGestureListener witch performs callbacks to methods such as onFling. Wile being able to override them, compiler won't let me change the return statement and since FLING may be: up, down, left, right, boolean can't cover all the possibilities.

    Open in any suggestions while preparing a great "thank you" post for the person with the patience to help me! ;)

  5. #5
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    0

    Default

    It's hard to debug this without seeing your entire code. It might help if I could see your entire Activity class. But if you don't want to post/send that, I understand.

  6. #6
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    Java Code:
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.ImageView;
    
    public class main extends Activity {
    	
    	// Fields
    	private MazeBlock[][] maze;
    	private int[] place = new int[2];
    	private int[] size = {20, 20};
    	private int[] screenBlocks = {40, 20};
    	private ImageView[][] imageList = new ImageView[screenBlocks[1]][screenBlocks[0]];
    	int enemys = 3;
    	Enemy[] enemylist = new Enemy[enemys];
    	GenerateMaze generator;
    	int level = 2;
    	[B]Gestures scanner =new  Gestures();[/B]
    	
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            new LayoutGenerator(this, screenBlocks);
            setContentView(LayoutGenerator.makeScreen());
            imageList = LayoutGenerator.imagelist;
            generateMaze(size[0],size[1]);
            place[0] = size[0]/2;
            place[1] = size[1]/2;
            createEnemys();
            move();
        }
    
        private void createEnemys(){
        	for(int i = 0 ; i < enemylist.length ; i++){
        		enemylist[i] = new Enemy(level);
        		enemylist[i].findPlace(this.maze);
        	}
        }
        
        public boolean onMoove(){
        	while(true){
        		move();
        	}
        }
        
        private void enemymove(){
        	for(Enemy  e:this.enemylist){
        		int[] p = e.getPlace();
        		this.maze[p[0]][p[1]].setEnemyFlag(false);
        		this.maze[p[0]][p[1]].setWallFlag(false);
        		int[] m = e.move(this.generator);
        		this.maze[m[0]][m[1]].setEnemyFlag(true);
        		this.maze[m[0]][m[1]].setWallFlag(true);
        		
        	}
        }
    
            private void generateMaze(int x, int y){
    	this.generator = new GenerateMaze(x,y);
    	this.maze = generator.getMaze();
        }
    
    [B]    @Override
        public boolean onTouchEvent(MotionEvent me)
        {
        	char dir = scanner.onTouchEvent(me);
        	switch(dir){
        	case 'u': onUp(); return true;
        	case 'd': onDown(); return true;
        	case 'r': onRight(); return true;
        	case 'l': onLeft(); return true;
        	}
            return false;
        }[/B]
    	
    	private void move(){
    		enemymove();
    		int countY = 0;
    		for(int y = -screenBlocks[1]/2; y < screenBlocks[1]/2; y++){
    			int countX = 0;
    			for(int x = -screenBlocks[0]/2; x < screenBlocks[0]/2; x++){
    				try{
    					if(this.maze[this.place[0]+y][this.place[1]+x].getWallFlag()){
    						if(this.maze[this.place[0]+y][this.place[1]+x].getEnemyFlag()){
    							imageList[countY][countX].setBackgroundResource(R.drawable.floor);
    							imageList[countY][countX].setImageResource(R.drawable.enemy);
    						}else imageList[countY][countX].setBackgroundResource(R.drawable.wall);
    					}else{imageList[countY][countX].setBackgroundResource(R.drawable.floor);}
    				}catch(ArrayIndexOutOfBoundsException e){
    					imageList[countY][countX].setBackgroundResource(R.drawable.wall);
    					}
    				countX++;
    			}
    			countY++;
    		}
    		this.imageList[screenBlocks[1]/2][screenBlocks[0]/2].setImageResource(R.drawable.user);
    	}
    
    	[B]public void onLeft(){
    		if(!this.maze[this.place[0]][this.place[1]+1].getWallFlag()){
    		this.place[1]+=1;
    		move();
    		}
    	}
    	
    	public void onRight(){
    		if(!this.maze[this.place[0]][this.place[1]-1].getWallFlag()){
    		this.place[1]-=1;
    		move();
    		}
    	}
    	
    	public void onUp(){
    		if(!this.maze[this.place[0]-1][this.place[1]].getWallFlag()){
    			this.place[0]-=1;
    			move();
    		}
    	}
    	
    	public void onDown(){
    		if(!this.maze[this.place[0]+1][this.place[1]].getWallFlag()){
    		this.place[0]+=1;
    		move();
    		}
    	}[/B]
    }
    this is my Activity class and this:
    Java Code:
    import android.view.MotionEvent;
     
    import android.view.GestureDetector.OnGestureListener;
     
    import android.view.GestureDetector;
     
    
    public class Gestures implements OnGestureListener
     
    {    
     
        private GestureDetector gestureScanner;
        private char  none = 'q';
     
     
        public Gestures()
        { 
            gestureScanner = new GestureDetector(this);
     
        }
     
     
        public boolean onTouchEvent(MotionEvent me)
     
        {
     
            return gestureScanner.onTouchEvent(me);
     
        }
     
       
     
        @Override
     
        public boolean onDown(MotionEvent e)
     
        {
             return true;
         }
     
       
     
        [B]@Override
     
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
     
        {
        	float y = velocityY;
        	float x = velocityX;
        	if(velocityY < 0) y = -velocityY;
        	if(velocityX < 0) x = -velocityX;
        	
        	LayoutGenerator.texts[2].setText("x" + x+ " y"+ y);
        	if(x > y)
        		if(velocityX > 0){
        			return 'r';
        		}else return 'l';
        	else{
        		if(velocityY > 0){
        			return 'd';
        		}else return 'u';
        	}
        }[/B]
     
       
     
        @Override
     
        public void onLongPress(MotionEvent e){return;}
     
        @Override
     
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
     
        {
     
            LayoutGenerator.texts[3].setText("x" + distanceX+ " y"+ distanceY);
     
            return true;
     
        }
     
       
     
        @Override
     
        public void onShowPress(MotionEvent e)
     
        {
        	return;
        }    
     
       
     
        @Override
     
        public boolean onSingleTapUp(MotionEvent e)    
     
        {
        	return true;
        }
     
    }
    is the onGestureListener.

    there are obvious mistakes on the return types left behind from experimentation...

  7. #7
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    0

    Default

    I think I see your problem now. What you need to do is enhance your Gestures class by adding some attributes/methods:

    Java Code:
    public class Gestures implements OnGestureListener
     
    {    
     
        private GestureDetector gestureScanner;
        private char  none = 'q';
    [B]    private char cDirection;  //for example 'r', 'u',...
    
        public getDirection() { return cDirection; }[/B]
    
    ...
    
        @Override
     
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
     
        {
        	float y = velocityY;
        	float x = velocityX;
        	if(velocityY < 0) y = -velocityY;
        	if(velocityX < 0) x = -velocityX;
        	
        	LayoutGenerator.texts[2].setText("x" + x+ " y"+ y);
        	if(x > y)
        		if(velocityX > 0){
        			[B]cDirection =  'r';[/B]
        		}else return 'l';
        	else{
        		if(velocityY > 0){
        			[B]cDirection = 'd';[/B]
        		}else [B]cDirection = 'u';[/B]
        	}
        }
    So what you are doing above is storing the direction in an attribute of your class when it happens. You have also created a public method called getDirection() to access that value. This public method gives other classes (e.g. Activity) access to this data. Now here's the change in your Activity class:

    Java Code:
        @Override
        public boolean onTouchEvent(MotionEvent me)
        {
        	char dir = scanner.[B]getDirection()[/B];
        	switch(dir){
        	case 'u': onUp(); return true;
        	case 'd': onDown(); return true;
        	case 'r': onRight(); return true;
        	case 'l': onLeft(); return true;
        	}
            return false;
        }
    I hope I didn't confuse things or go completely off topic. Let me know.

  8. #8
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    I haven't tried it yet but this seems to be it!!! Thank you very much for the help and your time!

    Cheers!

  9. #9
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    0

Similar Threads

  1. main method not found in class
    By eLancaster in forum New To Java
    Replies: 5
    Last Post: 02-09-2011, 08:48 AM
  2. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  3. why i cant access a method in a sub class?
    By javanew in forum New To Java
    Replies: 7
    Last Post: 05-03-2010, 06:29 PM
  4. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 06:20 PM
  5. executing a method in main class
    By rangers27 in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 05:04 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •