0

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){
                        
                    }
                }
            });
      }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
mogoli
  • 2,153
  • 6
  • 26
  • 41

3 Answers3

0

You shouldn't run activity twice.

Either remove the following in your Intenttest class:

// 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 put it inside the catch block

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");
                }
            }
        }
    });
Nimantha
  • 6,405
  • 6
  • 28
  • 69
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • I thought so, thanks for the conformation. How do I then switch between different lay outs if i take it out? – mogoli Mar 19 '12 at 14:11
  • what do you mean by switching different layouts? in my answer code, if **Try** block throws any error upon execution, then **Catch** block will be executed – waqaslam Mar 19 '12 at 14:20
  • Hey Waqas i tried both ur options but everytime I press button it keeps force closing still. I noticed that if I comment out the bundle code from the target class the error stops, obviously I still dont have my data though. – mogoli Mar 19 '12 at 14:40
  • can you add the log's exception trace too – waqaslam Mar 19 '12 at 14:59
  • Managed to solve the problem. First off I forgot to declare the text view in my target app (Im such an idiot!, so sorry) I took manages to code farbles1670 solution and it worked. Waqas woud have worked as well. Thanks very much for all your help, really appreciate it! – mogoli Mar 19 '12 at 15:02
0

You should use intent extras.

You can put primitives without any problems, for more complex objects you'll need to use "Parcelable" (your data object should implement this).

How do I get extra data from intent on Android?

Community
  • 1
  • 1
MarkySmarky
  • 1,609
  • 14
  • 17
0

You can pass it as extra data included in the intent. If your source activity, do this,

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

and in the target activity,

Intent i = getIntent();
String myVal = i.getStringExtra("myKey");
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • don't mess with getting / setting the bundle. just do what i wrote above. – Jeffrey Blattman Mar 19 '12 at 14:34
  • I tried your code farble1670 but the error still persists, not sure why to be honest. Can I pass this into the intent I made ourIntent? I tried that too but maybe Im using the wrong command? – mogoli Mar 19 '12 at 14:41
  • Managed to solve the problem. First off I forgot to declare the text view in my target app (Im such an idiot!, so sorry) I took manages to code farbles1670 solution and it worked. Waqas woud have worked as well. Thanks very much for all your help, really appreciate it! – mogoli Mar 19 '12 at 15:02