2011-01-31 13 views
0

以下のコードを実行してください。Java:グローバル変数の値にアクセスする際の問題

    shiftDirection & 1
  • 又は
  • currGear & 5と同様のペア
  • String[] Infoは、このような2つの値を含みます。

私がshiftDirection & 0を受け取った場合、前に受信した値currGearを表示する必要があります。

グローバル変数としてPresentGearを作成し、currGearを受信した場合は、PresentGearに値を格納します。

shiftDirection & 0を受け取ったとき、私はPresentGearを表示しようとしますが、何も表示されません。

PresentGearには値が割り当てられません。なぜこれが起こっているのか、私のアプローチが間違っている場合は、別の方法で親切に提案することができれば、非常に役に立ちます。

乾杯、

マドゥ

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

      //} 

     } 
} 

、上記のコードでは、私は、0

+4

は 'PresentGear'は*ありません*グローバル変数です。 'Images'クラスの特定のインスタンスに関連付けられたインスタンス変数です。 Javaにはグローバル変数はありません。 –

+1

@Jonathan私は同意しない。 *グローバル変数*と静的クラス変数*の違いは何ですか?(いくつかの文脈では接頭辞 - どちらの場合も安定した識別子を通してメソッド/関数(およびオブジェクト)間で共有されます) –

+2

@pstこの例では静的ではありません。静的変数は、あなたが言ったように、クラスに関連するクラス変数です。クラスに関連付けられていない変数を持つことはできません。私たちはちょうど髪を分割しています - JavaはCの意味でグローバル変数を持っていません。 –

答えて

1

PresentGearの値は、あなたのActivityの寿命のために持続します。 OSがあなたのActivityを殺した場合、その値は失われます。 onPause()onStop()onDestroy()にログインし、アプリのライフサイクルがどのように機能しているかを確認する必要があります。 PresentGearの値をSharedPreferencesに書き込んでonPause()に書き込んで、onCreate()の結果を読んでください。ライフサイクルのために、このリンクをチェックアウト:

+0

ありがとうございます。少なくとも今のところ働いている解決策に私が手伝ってくれました。 –

1

をcurrGearの値を格納し、shiftDirectionでそれを使用しての問題を一時的に解決したデバッガでこのコードをステップ実行すべきであるような音と、このアクティビティにどの値が配信されているか調べます。どのようにあなたは配信されるインテントを構築していますか? (私はどちらかInfo[0]"currGear"またはInfo[1]""ではありませんが、他の何かが起こっていることができることを疑う。)

関連する問題