I am trying to make a google map app that has a lot of markers and when the user clicks one of those markers, a new activity will open showing that marker's name and some additional info. I'm planning on putting buttons and stuff so I think infowindow won't cut it.
My code is a total mess since i'm a newbee and just put codes as long as they work so i'll just show the onMarkerClick part (where i am encountering the problem so it won't be too confusing
public boolean onMarkerClick(Marker loc) {
Intent intent = new Intent(this, stars.class);
Bundle bundle = new Bundle();
for (index3=0; index3 < array1.length; index3++) { //array1 stores all of my markers' id
if (loc.getTitle().equals(array5[index3])){ //array5 stores all of my markers' names
bundle.putString("name", loc.getTitle());
intent.putExtras(bundle);
startActivity(intent);
}
}
return true;
}
and my stars.java
public class stars extends MapsActivity {
String sname;
TextView tv;
Bundle bundle = getIntent().getExtras();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stars);
TextView tv = (TextView)findViewById(R.id.tv);
if(!bundle.isEmpty()){
sname = bundle.getString("name");
}
tv.setText(sname);
}
}
When i run this I get this error "Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference"
I tried making array5 public then use it in stars.java but it tells me that it's null. Also tried experimenting with bundle but i keep on getting the null error thing
I would use this statement inside the onCreate method :
Bundle bundle = getIntent().getExtras();
And do check for nulls
if (bundle != null) {
String sname = bundle.getString("name");
}
Related
I need to pass a String created from 'Activity A' to 'Activity B' so that I can display it in a TextView.
The problem is that the code causes Android to not respond, its identical to the other tutorials online.
Thanks for any feedback.
Activity A.onCreate()
check_button = (Button) findViewById(R.id.check_button);
check_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent i = new Intent(AddActivity.this, DetailActivity.class);
String hash = text_hash.toString();
i.putExtra("hash" , hash);
startActivity(i);
}
});
Activity B.onCreate()
Bundle extras = getIntent().getExtras();
if (extras != null)
{
passedHash = (String) getIntent().getExtras().getString("hash");
hash.setText(passedHash);
}
stack trace:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
According to your log, it seems that you didn't initialize your TextView in Activity B. Do this before setting the text to your TextView:
TextView hash = (TextView)findViewById(R.id.hash_textview);
Ok, so I needed to use X.getText().toString();
By reading the stack trace I figured I also needed to findViewByID on the TextView BEFORE trying to set text.
Thanks for reading.
If text_hash is TextView or EditText then use use
String hash = text_hash.getText().toString();
In ActivityB onCreate() use this:
String newString;
if (savedInstanceState != null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("hash");
}
}
EDIT
From your error log, You didn't initialize TextView in your ActivityB. First you have to initialize that TextView then set Text.
your textView object is nullreferenced, you need to initialize the hash variable in the oncreate before you begin to call any method of that TextView...
Example:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView hash = (TextView)findViewById(R.id.hash_textview);
after that you can call the settext method
hash.setText(passedHash);
I am puzzled by an instance of a public static final String returning null when referenced. I've used this exact construct hundreds of times but do not see what I might be doing differently this time. Code follows:
public class NSNItemDisplayActivity extends ExpandableListActivity
implements LoaderManager.LoaderCallbacks<Cursor>
{
public static final String NSN_ID_KEY = "NSN_ID_KEY";
...
}
Referenced by code that does this:
card.setOnClickListener(new Card.OnCardClickListener() {
#Override
public void onClick(Card card, View view) {
Intent nsnListIntent = new Intent(
mContext,
NSNItemDisplayActivity.class);
Bundle bundle = nsnListIntent.getExtras();
bundle.putString(NSNItemDisplayActivity.NSN_ID_KEY, //Null Pointer Exception
card.getId());
mContext.startActivity(nsnListIntent);
}
}
);
I receive a null pointer exception, then sit and scratch my head.
Thank you in advance, I feel silly asking such a simple question.
Intent nsnListIntent = new Intent(
mContext,
NSNItemDisplayActivity.class);
Bundle bundle = new Bundle()
bundle.putString(NSNItemDisplayActivity.NSN_ID_KEY, //Null Pointer Exception
card.getId());
nsnListIntent.PutExtra("", bundle);
mContext.startActivity(nsnListIntent);
// do this mate
This line
bundle.putString(NSNItemDisplayActivity.NSN_ID_KEY
won't return null unless bundle is null, from what you are showing us. Initialize your Bundle first
Intent nsnListIntent = new Intent(mContext, NSNItemDisplayActivity.class);
Bundle bundle = new Bundle();
bundle.putString(NSNItemDisplayActivity.NSN_ID_KEY, card.getId());
You don't have any extras in your Intent to initialize this Bundle to since you just create it on the previous line.
Maybe you mean to initialize it from the extras in an Intent used to create this Activity. In which case, you would want something like
Bundle bundle = getIntent().getExtras();
I explained this badly originally. This is my question: The Intent I send to the startActivity() method, contains a private field, mMap, which is a Map containing the strings I sent to putExtra(). When the target activity starts, a call to getIntent() returns an Intent that does not contain those values. The mMap field is null. Obviously, something in the bowels of the View hierarchy or the part of the OS that started the new activity created a new Intent to pass to it, since the object IDs are different.
But why? And why are the putData() values not carried fowrard to the new Intent?
The activity that starts the new activity extends Activity. Here's the startup code:
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case 4:
i = new Intent(this, StatList.class);
i.putExtra("Name1", "Test1");
i.putExtra("Name3", "Test2");
startActivity(i);
}
}
I've tried the key values with and without the (recommended) complete package name prefix.
In the Eclipse debugger, I have verified the values for the player names are being inserted into i.mExtras.mMap properly.
Here's the code from the startee:
public class StatList extends ListActivity {
private final StatsListAdapter statsAdapter;
public StatList() {
statsAdapter = StatsListAdapter.getInstance(this);
} // default ctor
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent i = getIntent();
final Bundle extras = i.getExtras();
< more code here >
}
When execution gets to this method, mIntent.mExtras.mMap is null, and mIntent.mExtras.mParcelledData now contains some values that don't look sensible (it was null when startActivity() was called). getIntent() returns mIntent.
I've also tried startActivityForResult(), with the same result.
From the docs and the samples I've seen online & in the sample apps, this should be easy. I've found another way to meet my immediate need, but I'd like to know if anyone can help me understand why something this simple doesn't work.
In your main Activity:
i = new Intent(this, StatList.class);
i.putExtra("Name1", "Test1");
i.putExtra("Name3", "Test2");
startActivity(i);
Then in StatList.class
Bundle extras = getIntent().getExtras();
String name1 = extras.getString("Name1");
String name3 = extras.getString("Name3");
Log.i("StatList", "Name1 = " + name1 + " && Name3 = " + name3)
Update the following two line
final Intent i = getIntent();
final Bundle extras = i.getExtras();
Replace it with
Bundle extras = getIntent().getExtras();
if(extras!= null){
String var1= extras.getString("Name1");
String var2= extras.getString("Name2");
}
Question is simple, I am not exactly new to Android but I cannot, for the life of me, retrieve the extras passed via an intent from Activity A to Activity B.
See Activity A: This is actually a ListFragment, that implements onListItemClick() to start another activity via an intent.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
Intent i = new Intent(getActivity(), ExpandedTweetView.class);
twitter4j.Status status = adapter.getItem(position);
Bundle extras = new Bundle();
extras.putString(KEY_TEXT, status.getText());
extras.putString(KEY_HANDLE, status.getUser().getScreenName());
extras.putString(KEY_NAME, status.getUser().getName());
extras.putString(KEY_TIMESTAMPS, status.getCreatedAt().toString());
extras.putLong(KEY_RETWEETS, status.getRetweetCount());
i.putExtra(KEY_EXTRAS, extras);
startActivity(i);
}
This part just works, I tested it usng Log.v(TAG, "status.getText()" to make sure that the error was not coming from the Adapter passing an empty item via getItem().
Here is the code on Activity B:
public class ExpandedTweetView extends Activity {
TextView text;
TextView name;
TextView handle;
TextView createdAt;
TextView retweets;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expanded_list_item);
Bundle extras = getIntent().getExtras();
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
text = (TextView) findViewById(R.id.h_content);
name = (TextView) findViewById(R.id.h_name);
handle = (TextView) findViewById(R.id.h_handle);
createdAt = (TextView) findViewById(R.id.h_timestamp);
retweets = (TextView) findViewById(R.id.h_retweet_count);
if(extras != null) {
text.setText(extras.getString(TimelineFragment.KEY_TEXT));
name.setText(extras.getString(TimelineFragment.KEY_NAME));
handle.setText(extras.getString(TimelineFragment.KEY_HANDLE));
createdAt.setText(extras.getString(TimelineFragment.KEY_TIMESTAMPS));
retweets.setText(String.valueOf(extras.getLong(TimelineFragment.KEY_RETWEETS)));
}
}
As you can see, I believe I am using the right code to obtain the extras, using the same code on other applications worked. Not sure why, when the ExpandedTweetView is created via an intent, ALL of the textViews are empty. See: https://www.dropbox.com/s/pso6jbyn6rpks9n/empty_activity.png
What is even MORE strange is that I had initially tried checking to see if the bundle was null by calling this:
if (extras == null) {
Log.v(TAG, "Extras are empty :(");
}
But that line was never executed, meaning the bundle is not null. I also thought that maybe the keys being used to retrieve the individual Strings from the bundle were mismatching; however, in order to remedy that I decided to create constants that could be used on both sides. As you can see on the code, both the key to set the Extra and the Key to retrieve the Extra are the same.
Any ideas as to what the heck is going on?
Bundle extras = getIntent().getExtras();
if (extras != null) {
extras = extras.getBundle("KEY_EXTRAS");
String status = extras.getString("KEY_TEXT");
}
Try adding the extra variable to intent rather than in Bundle
Ex:
i.putExtra(KEY_1, a);
i.putExtra(KEY_2, b);
i.putExtra(KEY_3, c);
Then retrieve it from other activity from intent
Ex:
getIntent().getStringExtra(KEY_1) ;
In Activity A:
Intent i = new Intent(MainActivity.this, AnotherActivity.class);
Bundle b = new Bundle();
b.putString("thisc", "my name");
i.putExtra("bundle", b);
startActivity(i);
In Activity B:
**Bundle bun = getIntent().getBundleExtra("bundle");
if (bun.containsKey("thisc")) {
Log.i("TAG", bun.getString("thisc"));
} else {
Log.i("TAG", "no thisc");
}**
Check the first line of code in Activity B, that's the main difference actually!!
//put value
Intent inatent = new Intent(this,text.class);
inatent_logo.putExtra("message","hello");
startActivity(inatent);
//get vale
String msg = getIntent().getStringExtra("message").toString();
It's Difficult to maintain intent to Bundle and Bundle to Intent if number of data you want ti share from one Activity to Other Activity.
just Simply use Intent with PuExtra() with different argument.
you can pass number of data in intent like :
Sender's Side :
Create your Intent.
Intent My_Intent = new Intent(FromClass.this,ToClass.class);
Add your value which you want to share with other activity.
My_Intent.putExtra("VAR_A",a_value);
My_Intent.putExtra("VAR_B",b_value);
My_Intent.putExtra("VAR_C",c_value);
Call your Intent.
StartActivity(My_Intent);
Receiver's Side :
Intent My_Intent = getIntent();
First_Value=My_Intent.getStringExtra("VAR_A");
Sec_Value=My_Intent.getStringExtra("VAR_B");
Thred_Value=My_Intent.getStringExtra("VAR_C");
I Think its Easy for you to Handel your data from one Activity to other .
I'm having some problems.
In a class "A" I have the code:
Bundle extras = getIntent().getExtras();
if ( extras == null ){
Log.e("extras", "Extra NULL");
}
String arrayWatt = extras.getString("valoresWatt");
String arrayHorario = extras.getString("valoresHorario");
Bundle extras = getIntent().getExtras(); --> this is returning NULL`
This method throws NUllPointerException. What is the problem here? Syntax?
---EDIT----
So sorry, I forgot this code:
(This code is from another class that starts the activity)
Intent i = new Intent();
i.setClassName("org.me.android",
"org.me.android.GraphViewDemo");
i.putExtra("valoresWatt", watt);
i.putExtra("valoresHorario", hora);
startActivity(i);
If you want to get the extras, this is what I would do:
Intent i = getIntent();
String arrayWatt = i.getStringExtra("valoresWatt");
String arrayHorario = i.getStringExtra("valoresHorario");
Where is the code you listed for getting the Extras located? Are you overriding the onCreate method? If so, make sure you call super.onCreate(bundleVariableName) before trying to work with the Extras. So...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
// ...
Bundle data = getIntent().getExtras();
if (data != null ) {
// should actually verify that the key exists, so:
// if (data.containsKey("valoresWatt")) {
// ... do something with the value
String watt = data.getString("valoresWatt");
String horario = data.getString("valoresHorario");
}
// ...
}
In your original code, you say that this first line is returning null:
Bundle extras = getIntent().getExtras(); // --> this is returning NULL
From the Android docs for Intent, it seems that the getExtras() method will return null if no extras have been added yet.
In that case, you may have to add your String extras by calling .putExtra(key, value) on the Intent object directly, rather than on its Map of extras, which doesn't exist yet.
For the NPE crash
Bundle extras = getIntent().getExtras();
if ( extras == null ){
Log.e("extras", "Extra NULL");
} else {
String arrayWatt = extras.getString("valoresWatt");
String arrayHorario = extras.getString("valoresHorario");
}
Instead of
i.putExtra("valoresWatt", watt);
i.putExtra("valoresHorario", hora);
try
i.putStringArrayListExtra("valoresWatt", watt);
i.putStringArrayListExtra("valoresHorario", hora);
if it is an arraylist of strings.