1

Java Maze アプリケーションを Android に移植する作業を行っています。これが意味することは、与えられた Java コード (この部分は私が書いたわけではありません) を使用して、いくつかのクラスを書き直しています (つまり、Android で動作するように Java グラフィックを Android グラフィックに変更し、Android を作成しました)。 UIアクティビティはすでに。

私の問題はこれです...私が理解していることから、迷路は「buildthread」で構築されています。生成アクティビティの進行状況バーを更新するために、迷路構築プロセス スレッド (buildthread) 中に介入しようとしています。しかし、Mazebuilder クラスで生成アクティビティを宣言し、「生成アクティビティ」変数 (GA) を使用して、更新された進行状況番号を Android 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;
}

これは、迷路構築プロセスを開始し、increaseprogress メソッドを持つ生成アクティビティ android クラスです。

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

4 に答える 4

2

MazeBuilder引数なしのコンストラクターで作成しますが、他のコンストラクターを使用してGeneratingActivity.

于 2013-01-19T03:24:06.347 に答える
1

@Squonk に感謝します。

new MazeBuilder() 

new MazeBuilder(this) 

問題を解決しました。

于 2013-01-20T01:18:02.867 に答える
0

あなたが電話をかけているとき、私はGA.increaseProgress(partiters*100/expected_partiters)あなたが意味すると信じていますGeneratingActivity.increaseProgress(partiters*100/expected_partiters)。これが正しけれincreaseProgressば、静的メソッドである必要があります。

ここで、GA は null です。

于 2013-01-21T03:03:54.373 に答える