I'm trying to pass a string between 2 different activities
When I run this program it keeps force closing. What am I doing wrong? I get the feeling I haven't understood the fundamentals of intents properly. I also get the feeling that I may be trying to run 2 intents at the same time, which may be causing problems.
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){
}
}
});
}
}