Reputation: 39
I am working on porting over a Java Maze application into Android. What this means is that I am taking the Java code given to me (I did not write this part), rewriting some of the classes (i.e. Changing the Java graphics into Android graphics so that it works on Android, and I have created the Android UI activities already.
My problem is this...from what I understand the maze is built with a 'buildthread'. I am attempting to intervene during the maze building process thread (buildthread) to update a progress bar on my Generating Activity. But when I declare my Generating Activity in my Mazebuilder Class and then use my "generating activity" variable (GA) to try and send back the updated progress number to my android UI class it throws a nullpointerexpection.
I'm pretty sure I declared the variable in the code below and initialized it.
public GeneratingActivity GA;
/**
* Constructor for a randomized maze generation
*/
public MazeBuilder(){
random = new Random();
}
public MazeBuilder(GeneratingActivity activity){
random = new Random();
GA = activity ;
}
The error is thrown here (GA.increaseprogress(...)):
private Seg findPartitionCandidate(Vector<Seg> sl) {
Seg pe = null ;
int bestgrade = 5000; // used to compute the minimum of all observed grade values, set to some high initial value
int maxtries = 50; // constant, only used to determine skip
// consider a subset of segments proportional to the number of tries, here 50, seems to randomize the access a bit
int skip = (sl.size() / maxtries);
if (skip == 0)
skip = 1;
for (int i = 0; i < sl.size(); i += skip) {
Seg pk = (Seg) sl.elementAt(i);
if (pk.partition)
continue;
partiters++;
if ((partiters & 31) == 0) {
// During maze generation, the most time consuming part needs to occasionally update the current screen
//
if (GA.increaseProgress(partiters*100/expected_partiters))
{
// give main thread a chance to process keyboard events
try {
Thread.currentThread().sleep(10);
} catch (Exception e) { }
}
}
int grade = grade_partition(sl, pk);
if (grade < bestgrade) {
bestgrade = grade;
pe = pk; // determine segment with smallest grade
}
}
return pe;
}
This is my Generating Activity android Class that starts the mazebuilding process and has the increaseprogress method:
package edu.wm.cs.cs301.KatzAMaze;
public class GeneratingActivity extends Activity {
private ProgressBar progressBar1;
private int progressBarStatus = 0;
public MazeBuilder mazeBuilder;
public Maze maze;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generating);
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
Intent intent = getIntent();
int mazeDifficulty = intent.getIntExtra("difficulty", 0);
String mazeAlgorithm = intent.getStringExtra("algorithm");
if (mazeAlgorithm == "Prim's"){
mazeBuilder = new MazeBuilderPrim();
mazeBuilder.build(maze, mazeBuilder.skill_x[mazeDifficulty], mazeBuilder.skill_y[mazeDifficulty],
mazeBuilder.skill_rooms[mazeDifficulty], mazeBuilder.skill_partct[mazeDifficulty]); // changed THIS to maze
}
else {
mazeBuilder = new MazeBuilder();
mazeBuilder.build(maze, mazeBuilder.skill_x[mazeDifficulty], mazeBuilder.skill_y[mazeDifficulty],
mazeBuilder.skill_rooms[mazeDifficulty], mazeBuilder.skill_partct[mazeDifficulty]); // changed THIS to maze
}
}
public boolean increaseProgress(int percentage){
progressBar1.setProgress(percentage);
return true ;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_generating, menu);
return true;
}
// Configures the Back button to bring the user to the Title Screen
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Toast.makeText(this, "Going back to Title Screen", Toast.LENGTH_SHORT).show();
Log.i("GeneratingActivity", "You have selected to return to the Title Screen");
}
return super.onKeyDown(keyCode, event);
}
public void finishGenerating (View view) {
Button generatingButton = (Button) findViewById(R.id.button1);
generatingButton.isClickable();
switch(view.getId()) {
case R.id.button1:
if (generatingButton.isPressed()) {
Toast.makeText(this, "You have selected to Play", Toast.LENGTH_SHORT).show();
Log.i("GeneratingActivity", "You have selected to Play");
}
break;
}
Intent intent = new Intent(this, PlayActivity.class);
startActivity(intent);
}
}
Thanks for any help.
Upvotes: 1
Views: 333
Reputation: 39
Thanks a LOT @Squonk, changing
new MazeBuilder()
to
new MazeBuilder(this)
solved the problem.
Upvotes: 1
Reputation: 9574
I believe when you are calling GA.increaseProgress(partiters*100/expected_partiters)
you mean GeneratingActivity.increaseProgress(partiters*100/expected_partiters)
. If this is correct, then increaseProgress
must be a static method.
Here, GA is null.
Upvotes: 0
Reputation: 9190
You create your MazeBuilder
with the no-arg constructor, but you need to use the other one and pass in the GeneratingActivity
.
Upvotes: 2
Reputation: 41
Just from a quick look at your code, it doesn't seem as though you are initializing the variable partiters. Since it is called in your GA.increaseprogress(...) function maybe that's causing a problem? It seems as though it would throw an error during the if statement above this call though and there is also a possibility that it is initialized somewhere else? If that is not the case, I'll continue looking at the problem to see if there is a more valid issue.
Upvotes: -1