Eclipse NullPointerException我似乎无法修复

时间:2013-01-19 02:57:16

标签: java android nullpointerexception

我正在努力将Java Maze应用程序移植到Android中。这意味着我正在使用给我的Java代码(我没有写这部分),重写一些类(即将Java图形更改为Android图形以便它在Android上运行,我创建了Android UI活动已经开始。

我的问题是这个......据我所知,迷宫是用'buildthread'构建的。我试图在迷宫构建过程线程(buildthread)中进行干预,以更新我的生成活动上的进度条。但是当我在我的Mazebuilder类中声明我的Generating Activity然后使用我的“生成活动”变量(GA)尝试将更新的进度号发送回我的A​​ndroid UI类时,它会抛出nullpointerexpection。

我很确定我在下面的代码中声明了变量并对其进行了初始化。

    public GeneratingActivity GA;

/**
 * Constructor for a randomized maze generation
 */
public MazeBuilder(){
    random = new Random();
}

public MazeBuilder(GeneratingActivity activity){
    random = new Random();
    GA = activity ;
}

此处抛出错误(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;
}

这是我的Generating Activity android类,它启动了迷宫构建过程并具有increaseprogress方法:

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);
}
}

感谢您的帮助。

4 个答案:

答案 0 :(得分:2)

您使用no-arg构造函数创建MazeBuilder,但您需要使用另一个并传入GeneratingActivity

答案 1 :(得分:1)

感谢LOT @Squonk,改变了

new MazeBuilder() 

new MazeBuilder(this) 

解决了这个问题。

答案 2 :(得分:0)

我相信当你致电GA.increaseProgress(partiters*100/expected_partiters)时,你的意思是GeneratingActivity.increaseProgress(partiters*100/expected_partiters)。如果这是正确的,那么increaseProgress必须是静态方法。

此处,GA为空。

答案 3 :(得分:-1)

只需快速查看代码,就好像您正在初始化变量partiters一样。既然在GA.increaseprogress(...)函数中调用它可能会导致问题吗?看起来好像它会在这个调用之上的if语句中抛出一个错误,并且还有可能在其他地方初始化它?如果情况并非如此,我会继续查看问题,看看是否存在更有效的问题。