2012-03-19 61 views
0
之間的串

你好,我試圖通過2個不同的activites之間的字符串(我是新來的Android)傳遞活動

當我運行這個程序,它使強制關閉,請能有人告訴我是什麼做錯了嗎?我感覺我沒有正確理解意圖的基本原理。我也覺得我可能會試圖同時運行兩個意圖,這可能會導致問題。謝謝你提供的所有幫助。

package com.intent.test; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class Intenttest extends Activity { 
    /** Called when the activity is first created. */ 

    Button button1; 
    TextView text1; 
    String tests="hello there my friend"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     button1 =(Button)findViewById(R.id.button1); 
     text1=(TextView)findViewById(R.id.text1); 


     button1.setOnClickListener(new View.OnClickListener() { 


      @Override 
      public void onClick(View v) { 
       try{ 
        Bundle b = new Bundle(); 
        b.putString("key", tests); 
        Intent i = new Intent(Intenttest.this,DataPass.class); 
        i.putExtras(b); 
        startActivity(i); 
        text1.setText(tests); 
       }catch(Exception e){ 
        text1.setText("Error2"); 
       } 

       // TODO Auto-generated method stub 
       try{ 
        Class rClass = Class.forName("com.intent.test." +"DataPass"); 
        Intent ourIntent = new Intent(Intenttest.this,rClass); 
        startActivity(ourIntent); 
       }catch(Exception e){ 
        text1.setText("Error"); 
       } 

      } 
     }); 

    } 
} 



package com.intent.test; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class DataPass extends Activity{ 

     Button button2; 
     String tests; 
     TextView text2; 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.datapass); 

      button2 =(Button)findViewById(R.id.button2); 

      try{ 
       Bundle gotb = getIntent().getExtras(); 
       tests =gotb.getString("key"); 

      text2.setText(""+tests); 
      }catch(Exception e){ 
       text2.setText("error"); 
      } 

      button2.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 


        try{ 
         Class rClass = Class.forName("com.intent.test." +"Intenttest"); 
         Intent ourIntent = new Intent(DataPass.this,rClass); 
         startActivity(ourIntent); 
        }catch(Exception e){ 

        } 
       } 
      }); 
     } 
} 
+0

什麼原因開始活動2次IntentTest – 2012-03-19 14:12:04

回答

0

您不應該兩次運行活動。

無論刪除您Intenttest類以下內容:

// TODO Auto-generated method stub 
try{ 
    Class rClass = Class.forName("com.intent.test." +"DataPass"); 
    Intent ourIntent = new Intent(Intenttest.this,rClass); 
    startActivity(ourIntent); 
}catch(Exception e){ 
    text1.setText("Error"); 
} 

OR把它

button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try{ 
       Bundle b = new Bundle(); 
       b.putString("key", tests); 
       Intent i = new Intent(Intenttest.this,DataPass.class); 
       i.putExtras(b); 
       startActivity(i); 
       text1.setText(tests); 
      }catch(Exception e){ 
       text1.setText("Error2"); 

       try{ 
        Class rClass = Class.forName("com.intent.test." +"DataPass"); 
        Intent ourIntent = new Intent(Intenttest.this,rClass); 
        startActivity(ourIntent); 
       }catch(Exception e){ 
        text1.setText("Error"); 
       } 
      } 
     } 
    }); 
+0

我是這麼認爲的,感謝您的構象。如果我把它拿出來,我如何在不同的佈局之間切換? – mogoli 2012-03-19 14:11:33

+0

你是什麼意思切換不同的佈局?在我的答案代碼中,如果** Try **塊在執行時拋出任何錯誤,那麼** Catch **塊將被執行 – waqaslam 2012-03-19 14:20:53

+0

嘿Waqas我試過了這兩個選項,但每次按下按鈕時它都會保持強制關閉。我注意到,如果我註釋掉目標類中的包代碼,錯誤就會停止,顯然我仍然沒有我的數據。 – mogoli 2012-03-19 14:40:00

0

,你可以把它作爲額外的數據包含在意向。如果你的源活動,爲此,

Intent i = new Intent(this, TargetActivity.class); 
i.putExtra("myKey", myVal); 
startActivity(i); 

,並在目標活動,

Intent i = getIntent(); 
String myVal = i.getStringExtra("myKey"); 
+0

不要弄亂/設置包。只是做我上面寫的。 – 2012-03-19 14:34:26

+0

我試過你的代碼farble1670,但錯誤仍然存​​在,不知道爲什麼說實話。我可以將此轉化爲我製作我們的意圖的意圖嗎?我也試過,但也許我使用錯誤的命令? – mogoli 2012-03-19 14:41:53

+0

管理解決問題。 首先,我忘了申報我的目標應用程序中的文本視圖(IM這樣一個白癡!很抱歉) 我把設法代碼farbles1670解決方案,它的工作。 Waqas也會有效。 非常感謝您的幫助,非常感謝! – mogoli 2012-03-19 15:02:16