Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im working on a game with buttons in android. I am translating my VB2010 game on java but i dont know what is the equivalent of VB coding to java such as "Button1.TEXT = Button2.TEXT" and...

VB
Sub CheckButton(ByRef Butt1 As Button, ByRef Butt2 As Button)
       If Butt2.Text = "" Then
           Butt2.Text = Butt1.Text
           Butt1.Text = ""
       End If
   End Sub
   Sub CheckSolved()
       If Form1.Button1.Text = "1" And Form1.Button2.Text = "2" And Form1.Button3.Text = "3" And
       Form1.Button4.Text = "4" And Form1.Button5.Text = "5" And Form1.Button6.Text = "6" And
       Form1.Button7.Text = "7" And Form1.Button8.Text = "8" And Form1.Button9.Text = "9" And
       Form1.Button10.Text = "10" And Form1.Button11.Text = "11" And Form1.Button12.Text = "12" And
       Form1.Button13.Text = "13" And Form1.Button14.Text = "14" And Form1.Button15.Text = "15" Then
           MsgBox("Wow, You did it in " & Form1.Count & " Clicks ", vbInformation, "Shuffle")
       End If
       Form1.Count = Form1.Count + 1
       Form1.Text = "Shuffle : " & Form1.Count & " Clicks"
   End Sub

...to Java. In short, I am translating my VB2010 project into an android app.
Here's my codes in eclipse and i cant make it work.

Java
import android.widget.Button;
import android.widget.TextView;


public class Activ2 extends Activity implements OnClickListener {
	
	Button btn1, btn2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activ2);

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				CheckButton(btn1, btn2);
			
			}
	});
        public static void CheckButton(Button Butt1, Button Butt2)
    	{
    		if (Butt2.equals(""))
    		{
    			Butt2.setText(Butt1.getText());
    			Butt1.setText("");
    		}
    	}
        }



i have an error in these lines:
Java
CheckButton(btn1, btn2);
Error: The method CheckButton(Button, Button) is undefined for the type new View.OnClickListener(){}

AND
Java
public static void CheckButton(Button Butt1, Button Butt2)
Error: Multiple markers at this line
	- Syntax error on token ")", ; expected
	- void is an invalid type for the variable 
	 CheckButton
	- Syntax error on token "(", ; expected
	- Syntax error on token ",", ; expected
	- btn cannot be resolved to a type
	- Syntax error on token ",", ; expected
Posted
Updated 11-Dec-13 12:31pm
v2
Comments
Fredrik Bornander 11-Dec-13 10:11am    
What about it doesn't work. Does it not compile, does it crash?
JepoyCastle 11-Dec-13 17:48pm    
it doesn't compile.
Ganesh KP 11-Dec-13 12:15pm    
Syntax might be different with Vb.Net and Java but if you consider overall functionality then it should work, so tell us what is your games does? then try to replicate the same thing in Java or PHP what ever language you like, no matter, the over all logic will remain same every where. Dont try to convert to line by line, it mights leads to extra burden and leads to errors.
JepoyCastle 11-Dec-13 17:53pm    
it is a picture puzzle game. i have 16 imagebuttons, one is blank, and the user will click those buttons around the blank button until the picture is fixed.

First of all, you don't clear your question, on which line of android code you got stuck or get any exception? Second, i think you need a little bit edit to make it work:
Java
public static void CheckButton(Button Butt1, Button Butt2)
{
    if (Butt2.getText().toString() == null || Butt2.getText().length() == 0)
    {
        Butt2.setText(Butt1.getText().toString());
        Butt1.setText("");
    }
}
 
Share this answer
 
You can't call a static method on a parent class without qualifying it. Also I think you want to sort your if-statement in CheckButton.

Try changing your Activ2 class to look something like this;

Java
import android.widget.Button;
import android.widget.TextView;

public class Activ2 extends Activity implements OnClickListener {
	
	Button btn1, btn2;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activ2);
 
        	btn1 = (Button) findViewById(R.id.btn1);
	        btn2 = (Button) findViewById(R.id.btn2);
	        btn1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Activ2.CheckButton(btn1, btn2);
			
			}
		});
	}
        
	public static void CheckButton(Button Butt1, Button Butt2)
    	{
    		if (Butt2.getText().equals(""))
    		{
    			Butt2.setText(Butt1.getText());
    			Butt1.setText("");
    		}
    	}
}


Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900