Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用公共静态字段在活动之间共享数据_Android_Static_Nullpointerexception_Shared_Public - Fatal编程技术网

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

Android 使用公共静态字段在活动之间共享数据,android,static,nullpointerexception,shared,public,Android,Static,Nullpointerexception,Shared,Public,我正在尝试实现一个应用程序,它允许使用3种不同的共同例程活动搜索二叉树。这些活动可以选择以任何顺序运行,并且每个活动都应该能够访问有关最近访问的节点的共享数据,以便它们可以继续搜索树 用文字解释这一点有点复杂,所以我想我应该在这里粘贴部分代码。以下是我的一项共同日常活动的代码(其他两项基本相同): 主要活动的代码: public class SearchSharedTreeActivity extends Activity implements OnClickListener { pri

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

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

主要活动的代码:

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");
        }
    }
}
从我的主要活动的上述代码可以看出,我已经将字段
节点列表
(访问的节点列表),
当前
(当前访问的节点)和
(要在树中搜索的元素)设置为
公共静态
,以便我的共同例程活动可以轻松访问它们。但不知何故,这不起作用,在调用任何共同例程活动时,我总是得到
NullPointerException
,然后
RESULT\u取消


请帮我指出问题所在。谢谢你的建议。

也许我是瞎子。。。但是我没有看到
节点列表
(或其他
静态
字段)设置为除
null
之外的任何其他字段。这些是在哪里定义的?我只在协作例程活动中的列表中添加一个新节点,当它们开始搜索树并访问第一个节点时。
是在主活动的
onCreate
方法中定义的,而
是在用户单击目标按钮时设置的(因此在
onClick
方法中定义)。但是如果
SearchSharedTreeActivity.nodeList
null
,则不能
。向其添加
。您必须首先将其声明为
新向量(…)
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");
        }
    }
}