你好,我試圖通過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){
}
}
});
}
}
什麼原因開始活動2次IntentTest – 2012-03-19 14:12:04