0

So, I'm basically fooling around with some kind of login form, and trying to say a customized hello with the user name on the upcoming activity. The code compiles with no problem but the app crashes as soon as I click on the login button, the login worked successfully before I tried to implement the customized hello, so the problem has to be somewhere in the following code.

Here is where I call the activity:

Intent k = new Intent(this, MainActivity.class);
            //Sends login name to activity k
            k.putExtra("loginName", login.getText().toString()); 
 //login is the EditText variable name for the login text field
            startActivity(k);

Here is where I retrieve the extra data and try to use it as described:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ProfileActivity extends Activity {

    TextView helloString;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        Bundle extras = getIntent().getExtras();

        //Getting the hello text string
        helloString = (TextView)findViewById(R.id.textHello);
        String loginName = extras.getString("loginName");
        helloString.setText("¡Hello, " + loginName + "!");

    }

}

It somehow avoids the crash if I put in comment these two lines:

String loginName = extras.getString("loginName");
helloString.setText("¡Hello, " + loginName + "!");

Still, I can't be sure if the problem is really there or not, I thought it could have something to do with the type of data sent from the first activity not matching with the type being retrieved on the second, but still got no clue after trying some stuff around that.

Thanks in advance.

Editing:

I actually found out that I may have something to do with the fact that I am calling to mainActivity.class while the text is being shown in an activity called profileActivity.class, the problem is, profileActivity is being shown as a tab inside the mainActivity so I don't really know how should I approach that.

Editing 2:

So I solved it finally myself, for anyone interested I just sent the data to the MainActivity.class

Intent k = new Intent(this, MainActivity.class);

            //Sends login name to activity k
            k.putExtra("loginName", login.getText().toString());
            startActivity(k);

And, inside the Main Activity, when calling the ProfileActivity to set it up as a tab:

//Profile tab
            intent = new Intent(this, ProfileActivity.class);
            Bundle extras = getIntent().getExtras();
            intent.putExtra("loginName", extras.getString("loginName"));
            spec = mTabHost.newTabSpec("home")
                    .setIndicator("Home", res.getDrawable(R.drawable.profile_icon))
                    .setContent(intent);
            mTabHost.addTab(spec);

Problem solved, thanks for the help everyone anyways.

Heartcroft
  • 1,672
  • 2
  • 19
  • 31
  • 1
    Check the logcat and post it here, it will tell you the reason for the crash. – Jems Apr 16 '12 at 19:20
  • you are sending your data to the MainActivity but you are reading the intent data from the ProfileActivity..are you sure of this? – Mahdi Hijazi Apr 16 '12 at 19:22
  • Yeah, I just edited commenting on that, I guess that's the problem but since the ProfileActivity is inside the MainActivity as a tab, I don't really know how to approach it right now, I admit I didn't see that before posting so I guess we've gone one step forward now. – Heartcroft Apr 16 '12 at 19:26

3 Answers3

1

Your intent is refering to MainACtivity.class and you are trying to fetch the extras in ProfileActivity. Try changing the MainActivity.class to ProfileActivity.class if that is what your flow is.Please cross check your activities flow.

Hope it helps.

Intent k = new Intent(this, ProfileActivity.class);
            //Sends login name to activity k
            k.putExtra("loginName", login.getText().toString()); 
 //login is the EditText variable name for the login text field
            startActivity(k);
Deva
  • 3,919
  • 1
  • 27
  • 38
  • Yes, this way works but is not opening the Activity that I want to open. The thing is that I need to send the data to ProfileActivity while still opening the MainActivity since ProfileActivity is being shown inside the MainActivity as a tab. – Heartcroft Apr 16 '12 at 19:33
0

Since you stated that you want the data sent to ProfileActivity but need to open MainActivity, you can follow one of the 4 approaches.

NOTE: you should be checking when you use getExtra, that the returned value isn't null, and deal with it accordingly.

  1. send the data first to the MainActivity, and then when you launch ProfileActivity from MainActivity, send the data that was previously passed in.
  2. Put the data in a public static field in ProfileActivity. Public static variables can lead to issues, so I would recommend against this.
  3. Save the data to a sharedPreferences file and read it when you need it in ProfileActivity.
  4. SubClass Application, and you can set / access many variables there. The below was copied from Are static fields in Activity classes guaranteed to outlive a create/destroy cycle?:

    public class MyApplication extends Application{ private String thing = null;

        public String getThing(){
            return thing;
        }
    
        public void setThing( String thing ){
            this.thing = thing;
        }
    }
    
    public class MyActivity extends Activity {
        private MyApplication app;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            app = ((MyApplication)getApplication());
    
            String thing = app.getThing();
        }
    }
    
Community
  • 1
  • 1
Kyle
  • 611
  • 4
  • 6
0

On MainActivity:

String user = this.getIntent().getStringExtra("loginName");

And when you create the tabhost on MainActivity pass the string the same way:

Intent intent = new Intent().setClass(this, ProfileActivity.class);
intent.putExtra("loginName", user);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = mTabHost.newTabSpec("profile").setIndicator("Profile").setContent(intent);
mTabHost.addTab(spec);

Something like this.

But if you need the user name across the entire application use a Helper Class to store the user name.

Chronos
  • 1,972
  • 17
  • 22