1

我正在尝试实现一个应用程序,该应用程序允许使用 3 个不同的协同例程活动搜索二叉树。这些活动可以选择以任何顺序运行,并且每个活动都应该能够访问有关最近访问的节点的共享数据,以便他们可以继续搜索树。

用文字解释这个有点复杂,所以我想我应该在这里粘贴我的部分代码。下面是我的一个协程活动的代码(其他两个基本相同):

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

从我的主要活动的上述代码可以看出,我已经设置了字段treenodeList(访问的节点列表),current(当前访问的节点)和item(要在树中搜索的元素),public static以便我的协程活动可以轻松访问它们。但不知何故,这不起作用,我总是在调用任何协同例程活动时得到NullPointerException然后。RESULT_CANCELED

请帮我指出问题。任何建议表示赞赏。

4

0 回答 0