使用公共静态字段在活动之间共享数据

时间:2012-07-28 05:27:08

标签: android static nullpointerexception shared public

我正在尝试实现一个允许使用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

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

0 个答案:

没有答案