Android Game Development Using Java: parameter can't be resolved - android

I am developing an android board game and I am stuck with an error from one of my parameters. It is a mancala-based game with a view (TableView.class) containing rows of pits with stones in then, and a main activity (Game.class). I am attempting to define a pit and its contents.
Here is a snippet of the code:
public class Game extends Activity {
int pitIndex, pitContents;
int _pitContents=4;
int pitsPerRow=5;
//more code here .......
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
table_view = getInitialTableView();
tableView = new TableView(this);
setContentView(tableView);
tableView.requestFocus();
//...more code here
//
// ...
// define a playing pit
public int Pit(int pitIndex, int pitContents) {
this.pitIndex=pitIndex;
this.pitContents=pitContents;
}
// set player1's pits and populate them
public int Player1Pits[] = new int[16*2];
{
for (int i = 0; i < (2 * pitsPerRow); i++) {
if (i < pitsPerRow) {
Player1Pits[i] = new Pit(i,_pitContents);
else {
// do nothing
}
}
}
//.....
}
The error I get from Eclipse is that "Pit cannot be resolved to a type" when I try to instantiate a new pit:
Player1Pits[i] = new Pit(i,_pitContents);
Does anyone know where I am going wrong? Do I have to define Pit as a class outside the Game class?
I have searched exhaustively for a solution before posting this question. Your input will be appreciated. Thanks in advance.

Get rid of your method called Pit and make a pit class like this:
public class Pit {
public int mPitIndex, mPitContents;
public Pit(int pitIndex, int pitContents) {
this.mPitIndex = pitIndex;
this.mPitContents = pitContents;
}
}
What languages are you familiar with by the way?

Is Pit a class somewhere in the 'more code' sections? From what I see, you're trying to do new on a function directly, not on a class.

Related

Making a Global List variable to access from any activity

I am working on a shopping car app and need to have the product list and every product stock. for this what I am trying to do is implement a global list variable. I have read about global variables and I could implement it, but can't figure out how a list would be. any suggestions or help on how to implement it would be great, thanks.
public class Application extends android.app.Application {
private int data;
public int getData(){
return data;
}
public void setData(int d){
this.data=d;
}
}
((Application) this.getApplication()).setData(0);
x1=((Application) this.getApplication()).getData();
I figure it out following your suggestions. first on the applicacion class
public class Application extends android.app.Application {
public ArrayList myGlobalArray = null;
public Application() {
myGlobalArray = new ArrayList();
}
}
then for the porpouse of my app I add the respective values on their respectives indexes
for (int i=0;i<productsList.size();i++){
if (productsList.get(i).idProduct==idProduct){
values.add(i,Integer.parseInt(txtQuantity.getText().toString()));
((Application)getApplicationContext()).myGlobalArray = (ArrayList) values;
}
}
and when I need to get the values
ArrayList<Integer> test = new ArrayList<>();
for (int d=0;d< ((Application) getApplicationContext()).myGlobalArray.size();d++) {
test.add((Integer) ((Application)getApplicationContext()).myGlobalArray.get(d));
}
Thank you!

OnClickListener: Why I cannot access public methods of the object in ArrayAdapter? Any workaround?

Greetings from a greenhorn. This question is a bit long, sorry for that.
My android app loads xml to the listview, then after clicking any listview item I want to pass some information to second activity. With
piesneLVdata.getItemAtPosition(position).toString()
And here is my object:
public class Piesen {
private String nazov_piesne;
private String text_piesne;
}
+ some get and set methods and override of toString...
I can access the first property of the object and I can use it in my second activity. I guess it is because I have overriden the method inside my object definition:
#Override
public String toString() {
return nazov_piesne;
}
However I am not able to access the second property of my object. Tried this:
piesneLVdata.getItemAtPosition(position).getTextPiesne()
but getTextPiesne() seems not to be reachable - then why the "toString()" is reachable? The information is there, I can access it outside of the onClickListener with this formulation:
zoznamPiesni.get(position).getText_piesne().toString();
but when I try to use it in onClickListener I get the error:
Error:(64, 45) error: local variable zoznamPiesni is accessed from within inner class; needs to be declared final
But then, when I declare it final I cannot load a xml file into it. I feel officialy stucked in there. Maybe I should redesign the toString() method to contain an array of both properties of my object. Could you please point me to the correct direction? Thanks a lot in advance.
To be specific, here is my activity:
public class ListActivity extends AppCompatActivity {
ListView piesneLVdata;
Toolbar nadpis;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
nadpis = (Toolbar) findViewById(R.id.toolbar);
nadpis.setTitle(getResources().getString(R.string.nadpis));
piesneLVdata = (ListView) findViewById(R.id.piesneLVgui);
List<Piesen> zoznamPiesni = null;
try {
XMLPullParserHandler parser = new XMLPullParserHandler();
zoznamPiesni = parser.parse(getAssets().open("piesne.xml"));
//sort the collection piesne alphabetically a-z
Collections.sort(zoznamPiesni, new Comparator<Piesen>() {
#Override
public int compare(Piesen a1, Piesen a2) {
// String implements Comparable
return (a1.getNazov_piesne().toString()).compareTo(a2.getNazov_piesne().toString());
}
});
ArrayAdapter<Piesen> adapter = new ArrayAdapter<Piesen>(this,android.R.layout.simple_list_item_1,zoznamPiesni);
piesneLVdata.setAdapter(adapter);
}catch (IOException e) {
e.printStackTrace();
}
piesneLVdata.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent slovaIntent = new Intent(ListActivity.this, SongActivity.class);
Bundle balik = new Bundle();
balik.putString("NAZOV", piesneLVdata.getItemAtPosition(position).toString());
//balik.putString("TEXT", piesneLVdata.getItemAtPosition(position).getTextPiesne());
// balik.putString("TEXT", zoznamPiesni.get(position).getText_piesne().toString());
slovaIntent.putExtras(balik);
startActivity(slovaIntent);
}
});
}
The problem is that
piesneLVdata.getItemAtPosition(position)
return a Piesen treated as an Object. Object has toString method, so yo ucan call that method, that is overriden by the Piesen.class.
What you need is simply cast your object to a Piesed like follows:
((Piesen)piesneLVdata.getItemAtPosition(position)).getTextPiesne()

Android - Create an Array of Variable Object Types

How do I create an array of different types? If each class is an extension of the _object class, can I just make an _object array and add the extensions to it?
Example:
class _object {
int type = 1;
public _object() {
type = 2;
}
public doSomething() {
}
}
class tree extends _object {
public tree() {
}
}
class apple extends _object {
public apple() {
}
}
public tree aTree = new tree();
public apple anApple = new apple();
public _object[] objects = new _object[] { aTree, anApple };
The example in your question works. This is known as polymorphism.
http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
After setting up the above code in android as a project and debugging it, it in fact does work, so long as you only call methods of the base object.

Android: Setting variables from other classes

I have been trying to find a way to set variables for one class while in another class withous changing intents. Maybe I'm searching the wrong things, maybe I'm over complicating this, I don't know.
Basically, what I am doing at the moment is getting the listView item that is selected, checking its index value, and chaning a variable or two(from a different class) based on the choice.
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
theGame settings = null;
switch(position){
case 0:
settings.baseWave = 10;
settings.baseRate = 5;
break;
case 1:
settings.baseWave = 20;
settings.baseRate = 10;
break;
case 2:
settings.baseWave = 30;
settings.baseRate = 15;
break;
}
}
"theGame" is my outside class and "baseWave" and "baseRate" are the corrisponding varriables. Obviously, what I am doing here is not working for me. Im fairly new at all of this so be gentle.
Thank you for any help you can offer, it is much appreciated :)
~TG
It's nice that it struck you that you may have a bad design, but it should also have struck you that you don't know Java, that you're having this problem because you don't know Java, and that a good Java book would help you.
Well, this is the shortest path to getting this attempt of yours working: have theGame store a reference to itself in a static member; use the static member in your code.
That is,
class TheGame {
public static TheGame theGame = null;
TheGame() {
theGame = this;
}
}
And elsewhere: TheGame.theGame.baseWave = 10 * (position + 1);
Or you may need it put this way:
public class TheGame extends Activity {
public static TheGame theGame = null;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
theGame = this;
}
}
A good Java book would describe this sort of thing to you, give you a name for it, and warn you away from it.

Having difficulty with a global variable in android application

Hey I am having trouble declaring, changing, and using a global variable. I tried the whole "make a class that extends your application and put variables there" thing but I'm not sure how to implement it. Here is my class with the variable in it.
public class MyApp extends Application {
public int listPos;
}
I then tried to access and change int listPos here.
public class Browse extends ListActivity{
MyApp app = ((MyApp)getApplicationContext());
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] coffeeTypes = getResources().getStringArray(R.array.coffeeTypes);
setListAdapter(new ArrayAdapter<String>(this, R.layout.listview, coffeeTypes));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
app.listPos = position;
startActivity(new Intent(Browse.this, CoffeeTypes.class));
}
});
}
}
Then I tried to access the variable in the following activity to determine the outcome of an if else statement
public class CoffeeTypes extends Activity{
MyApp app = ((MyApp)getApplicationContext());
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(app.listPos == 0){
Toast.makeText(this, "WEEEEEEE!0", Toast.LENGTH_LONG).show();
}
else if(app.listPos == 1){
Toast.makeText(this, "RAWR!1", Toast.LENGTH_LONG).show();
Anyone have any idea what I am doing wrong?
You can use soorya's answer with some modifications. I would personally still use your myApp class but change listPos to static and access it that way. Doing it this way lets you use the Application class onCreate method to initialize values if needed (though it won't be needed in this example) or other Android methods.
public class MyApp extends Application {
public static int listPos;
}
//~~ Elsewhere:
MyApp.listPos = 5; //etc
But this isn't the best way to handle your type of problem.
You should be passing the list position information (or the id of the item clicked or however you are handling your data) via the Intent.
Instead of having a global variable to track this information, keep it local to just the Intent:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//app.listPos = position;
Intent intent = new Intent(Browse.this, CoffeeTypes.class);
intent.putExtra("position", position);
startActivity(intent);
//startActivity(new Intent(Browse.this, CoffeeTypes.class));
}
This passes the position data to the new Activity via the intent. In your CoffeeTypes Activity you should start with something along the following:
//in onCreate...
int incomingPosition = getIntent().getIntExtra("position",-1));
if(incomingPosition != -1) { //do stuff }
This will read the "position" data from the incoming Intent so you can use it. The -1 above is the default value if nothing is added.
One final warning: you might want to be careful sending the list position back and forth, depending on how your application is set up if new items are added/items are deleted the list position might no longer refer to the item you thought it did. If these coffee types/whatever you are using have a separate unique ID that might be more appropriate to avoiding the above situation, consider using that instead.
I'm not sure if it's a visibility issue with the android OS, are you able to make an public functions that may be able to act as getters for the variable?
int getListPos(){ return this.listpos; }
I know that you can pass variables between contexts, you may have to do that.
Perhaps a temporary workaround to get you moving could be creating a static class of accessible variables too?
Create a class like this
class globalClass
{
static int lastPos;
}
You can set values using
globalClass.lastPos = value;
and get function is
int myVal = globalClass.lastPos;
i do Application variable classes by making the variables private, and then create public get() and set() methods to change the variables. you can try that, but it seems like you are doing it right, technically
one reason why it might be going wrong is because you are implementing the stuff in onCreate and not onStartCommand. if you are testing this function with the wrong assumptions/knowledge of the Activity cycle, it could go wrong

Categories

Resources