Java:访问全局变量值的问题

时间:2011-01-31 22:14:09

标签: java android global-variables

请仔细阅读以下代码。

String[] Info包含2个这样的值:

  • shiftDirection & 1
  • currGear & 5和类似的对。

如果我收到shiftDirection & 0,我应该显示以前收到的currGear值。

所以我创建PresentGear作为全局变量,当收到currGear时,我将其值存储在PresentGear中。

然后,当我收到shiftDirection & 0时,我会尝试显示PresentGear,但不会显示任何内容。

PresentGear未分配任何值。如果有人能够说明为什么会这样,或者如果我的方法有误,那将会非常有用,请提出另一种方法。

干杯,

Madhu

public class Images extends Activity {
    /** Called when the activity is first created. */
    //LinearLayout mLinearLayout;
    //ImageView showImage;
    String PresentGear = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        

        Bundle extras = getIntent().getExtras();
        String[] Info = extras.getStringArray("MsgInfo");
        Log.d("TCP","in DA Images. Message name: " + Info[0] + ", value: " + Info[1]);
        if (Info[0].equals("currGear")) {   
            PresentGear = Info[1];
            setContentView(R.layout.image);
            TextView text_bottom = (TextView) findViewById(R.id.textView2);
            text_bottom.setText(Info[1]);
        }
        Log.d("TCP","in DA Images. Present gear1: " + PresentGear);
        DataAction(Info[0], Info[1]);            
    }

    public void DataAction (String mName, String mVal)  {
        String _mName = mName;
        String _mVal = mVal;

        if (_mName.equals("shiftDirection") && _mVal.equals("1")) {
            setContentView(R.layout.image);
            //TextView text_top = (TextView) findViewById(R.id.textView1);
            ImageView showImage = (ImageView) findViewById(R.id.imageView1);            
            //text_bottom.setText(Info[1]);
            showImage.setImageResource(R.drawable.shift_up);            
        } else if (_mName.equals("shiftDirection") && _mVal.equals("-1")) {
            setContentView(R.layout.image);
            //TextView text_bottom = (TextView) findViewById(R.id.textView2);
            ImageView showImage = (ImageView) findViewById(R.id.imageView1);            
            //text_bottom.setText(Info[1]);
            showImage.setImageResource(R.drawable.shift_down);          
        } else if (_mName.equals("recomGear") && _mVal != null) {
            Integer msgValue = Integer.parseInt(_mVal);
            Integer CurrentGear = (msgValue) - 1;
            Log.d("TCP","in DA Images. Current gear: " + CurrentGear);
            String Gear = Integer.toString(CurrentGear);
            setContentView(R.layout.image);
            TextView text_top = (TextView) findViewById(R.id.textView1);
            TextView text_bottom = (TextView) findViewById(R.id.textView2);
            ImageView showImage = (ImageView) findViewById(R.id.imageView1);            
            showImage.setImageResource(R.drawable.shift_up);
            text_bottom.setText(Gear);
            text_top.setText(_mVal);
        //} //else if (_mName.equals("currGear") && _mVal != null) {
            //PresentGear = _mVal;          
            //Log.d("TCP","in DA Images. Present gear1: " + PresentGear);
            //setContentView(R.layout.image);
            //TextView text_bottom = (TextView) findViewById(R.id.textView2);
            //text_bottom.setText(_mVal);                       
        } else if (_mName.equals("shiftDirection") && _mVal.equals("0")) {
            Log.d("TCP","in DA Images. Present gear: " + PresentGear);
            setContentView(R.layout.image);
            TextView text_bottom = (TextView) findViewById(R.id.textView2);
            TextView text_top = (TextView) findViewById(R.id.textView1);
            text_top.setText("Go on");  
            text_bottom.setText(PresentGear);           
        }

    }
}

更新

谢谢大家的回复。我将发布我从中获取字符串数组的部分。它可能不是最好的设计代码。如果有任何改变,我们会很高兴。

public class TCPListen extends Activity implements TCPListener {
    private TextView mTitle;
    public String data[] = new String[2];
    public String PGear;

    /** Called when the activity is first created. */
     @Override  
         public void onCreate(Bundle savedInstanceState) {  
             super.onCreate(savedInstanceState);  
             //setContentView(R.layout.main);            

             // Set up the window layout
             requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
             setContentView(R.layout.main);
             getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

             // Set up the custom title
             mTitle = (TextView) findViewById(R.id.title_left_text);
             mTitle.setText(R.string.app_name);
             mTitle = (TextView) findViewById(R.id.title_right_text);

             //TcpServiceHandler handler=new TcpServiceHandler(this);  
             //handler.execute("192.168.62.23");  

             TcpServiceHandler handler = new TcpServiceHandler(this,this);  
             Thread th = new Thread(handler);  
             th.start(); 
     }          

         public String[] callCompleted(String source){ 
                Log.d("TCP", "Std parser " + source);
                //mTitle.setText(source);
                //String data[] = new String[2]; 

                //if (source.matches("<MSG><N>.*</N><V>.*</V></MSG>"))  {           
                    Document doc = null;  
                    try{
                       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
                       DocumentBuilder db = dbf.newDocumentBuilder();  
                       doc = (Document) db.parse(new ByteArrayInputStream(source.getBytes()));  
                       NodeList n = doc.getElementsByTagName("N");  
                       Node nd = n.item(0);  
                       String msgName = nd.getFirstChild().getNodeValue();  
                       NodeList n1 = doc.getElementsByTagName("V");  
                       Node nd1 = n1.item(0);  
                       String tmpVal = nd1.getFirstChild().getNodeValue();  
                       data[0] = msgName;  
                       data[1] = tmpVal; 
                       Log.d("TCP", "Inside Std parser " + data[0] + " " + data[1]);
                       actionOnData(data[0], data[1]);
                      }  
                    catch(Exception e){  
                    e.printStackTrace();  
                }
                Log.d("TCP", "Just outside Std parser " + data[0] + " " + data[1]);
                return data;
                //} else Log.d("TCP", "Message in wrong format " + source);
                //mTitle.setText("Message in wrong format " + source);
                //return data;
            }


       //Function to display driver messages/images based on individual messages
         public void actionOnData(String name, String value) {
            String tempName = name;
            String tempVal = value;             

                if(tempName.equals("shiftDirection") && tempVal.equals("1"))    {
                    Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
                    //mTitle.setText("Change to next higher gear");
                    Intent myIntent = new Intent();
                    myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
                    myIntent.putExtra("MsgInfo", data); // key/value pair, where key needs current package prefix.
                    startActivity(myIntent); 
                    finish();
                } else if(tempName.equals("recomGear")) {
                    Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
                    //mTitle.setText("Drive like a man");                   
                    Intent myIntent = new Intent();
                    myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
                    myIntent.putExtra("MsgInfo", data); // key/value pair, where key needs current package prefix.
                    startActivity(myIntent);
                    finish();
                } else if(tempName.equals("shiftDirection") && tempVal.equals("-1"))    {
                    Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
                    //mTitle.setText("Change to next higher gear");
                    Intent myIntent = new Intent();
                    myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
                    myIntent.putExtra("MsgInfo", data); // key/value pair, where key needs current package prefix.
                    startActivity(myIntent); 
                    finish();
                } else if(tempName.equals("currGear"))  {
                    Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
                    //mTitle.setText("Change to next higher gear");
                    PGear = data[1];
                    Intent myIntent = new Intent();
                    myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
                    myIntent.putExtra("MsgInfo", data); // key/value pair, where key needs current package prefix.
                    startActivity(myIntent); 
                    //finish();
                } else if(tempName.equals("shiftDirection") && tempVal.equals("0")) {
                    Log.d("TCP","in actionOnData " + data[0] + " " + data[1]);
                    //mTitle.setText("Change to next higher gear");
                    data[1] = PGear;
                    Intent myIntent = new Intent();
                    myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images");
                    myIntent.putExtra("MsgInfo", data); // key/value pair, where key needs current package prefix.
                    startActivity(myIntent); 
                    finish();
                } else mTitle.setText("Just show something");
                //Log.d("TCP", "Just show an image");

            //}

         }
}

实际上,在上面的代码中,我暂时解决了存储currGear值并在shiftDirection中使用它的问题,0。

2 个答案:

答案 0 :(得分:1)

听起来您应该在调试器中单步执行此代码,并检查要向此活动传递的值。你是如何构建交付的Intent的? (我怀疑Info[0]不是"currGear"Info[1]"",但还有其他事情可以继续。)

答案 1 :(得分:1)

PresentGear的值只会在Activity的生命周期内持续存在。如果操作系统杀死了您的Activity,则该值将丢失。您应该在onPause()onStop()onDestroy()中进行一些登录,并检查应用的生命周期是如何工作的。您可能需要在PresentGear中写出SharedPreferencesonPause()的值,并在onCreate()中读取结果。查看生命周期的链接: