3 つの異なるコルーチン アクティビティを使用してバイナリ ツリーを検索できるアプリケーションを実装しようとしています。これらのアクティビティは任意の順序で実行するように選択でき、ツリーの検索を続行できるように、それぞれが最後にアクセスしたノードに関する共有データにアクセスできる必要があります。
これを言葉で説明するのは少し複雑なので、コードの一部をここに貼り付けるべきだと思います。以下は、私のコルーチン アクティビティの 1 つのコードです (他の 2 つは基本的に同じです)。
public class CoRoutineOne extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
try{
SearchSharedTreeActivity.current = SearchSharedTreeActivity.tree.search
(SearchSharedTreeActivity.item, (BinaryTree.Node)SearchSharedTreeActivity.nodeList.get(SearchSharedTreeActivity.nodeList.size() - 1));
SearchSharedTreeActivity.nodeList.add(SearchSharedTreeActivity.current);
setResult(Activity.RESULT_OK);
}catch(Exception e){
setResult(Activity.RESULT_CANCELED);
}
finish();
}
}
メイン アクティビティのコード:
public class SearchSharedTreeActivity extends Activity implements OnClickListener {
private EditText target = null;
private EditText corout1 = null;
private EditText corout2 = null;
private EditText corout3 = null;
private Button target_btn = null;
private Button corout1_btn = null;
private Button corout2_btn = null;
private Button corout3_btn = null;
private static final int COROUT_ONE_REQCODE = 1;
private static final int COROUT_TWO_REQCODE = 2;
private static final int COROUT_THREE_REQCODE = 3;
public static BinaryTree tree = null;
public static Vector nodeList = null;
public static BinaryTree.Node current = null;
public static Comparable item = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tree = new BinaryTree();
Comparable[] values = {20, 10, 30, 5, 17, 25, 40, 11, 35, 55};
tree.insert(values);
target = (EditText) findViewById(R.id.target_input);
corout1 = (EditText) findViewById(R.id.co1_output);
corout2 = (EditText) findViewById(R.id.co2_output);
corout3 = (EditText) findViewById(R.id.co3_output);
target_btn = (Button) findViewById(R.id.target_btn);
corout1_btn = (Button) findViewById(R.id.co1_btn);
corout2_btn = (Button) findViewById(R.id.co2_btn);
corout3_btn = (Button) findViewById(R.id.co3_btn);
target_btn.setOnClickListener(this);
corout1_btn.setOnClickListener(this);
corout2_btn.setOnClickListener(this);
corout3_btn.setOnClickListener(this);
}
public void onClick(View view){
Button btn = (Button) view;
int id = btn.getId();
if (id == R.id.target_btn){
item = target.getText().toString();
try{
Integer.parseInt((String) item);
target.setText("Target is " + item);
}catch (Exception e){
target.setText("");
corout1.setText("");
corout2.setText("");
corout3.setText("");
}
}
else if (id == R.id.co1_btn){
Intent i1 = new Intent(this, CoRoutineOne.class);
startActivityForResult(i1, COROUT_ONE_REQCODE);
}
else if (id == R.id.co2_btn){
Intent i2 = new Intent(this, CoRoutineTwo.class);
startActivityForResult(i2, COROUT_TWO_REQCODE);
}
else if (id == R.id.co3_btn){
Intent i3 = new Intent(this, CoRoutineThree.class);
startActivityForResult(i3, COROUT_THREE_REQCODE);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == COROUT_ONE_REQCODE){
if (resultCode == Activity.RESULT_OK){
if (item.equals(current.value))
corout1.setText("Found " + current.value);
else if (current == null)
corout1.setText("Not found");
else
corout1.setText("Reached " + current.value);
}
else
corout1.setText("Problem finding target");
}
else if (requestCode == COROUT_TWO_REQCODE){
if (resultCode == Activity.RESULT_OK){
if (item.equals(current.value))
corout2.setText("Found " + current.value);
else if (current == null)
corout2.setText("Not found");
else
corout2.setText("Reached " + current.value);
}
else
corout2.setText("Problem finding target");
}
else if (requestCode == COROUT_THREE_REQCODE){
if (resultCode == Activity.RESULT_OK){
if (item.equals(current.value))
corout3.setText("Found " + current.value);
else if (current == null)
corout3.setText("Not found");
else
corout3.setText("Reached " + current.value);
}
else
corout3.setText("Problem finding target");
}
}
}
上記のメイン アクティビティのコードからわかるように、フィールド(訪問したノードのリスト)、tree
(現在訪問しているノード)、および(ツリー内で検索される要素) を設定して、コルーチンが活動はそれらに簡単にアクセスできます。しかし、どういうわけかこれは機能しません。コルーチン アクティビティを呼び出すと、常に取得されます。nodeList
current
item
public static
NullPointerException
RESULT_CANCELED
問題を指摘するのを手伝ってください。どんな提案でも大歓迎です。