Android bundle value always reading 0 - android

In my android app, I store a value 1 in a bundle, and then start the activity, then I read the bundle value from the new activity, and its 0. I'm not sure what's going wrong...
content.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(context, ThreadScreen.class);
myIntent.putExtra("thread_id", Integer.toString(thread.getId(), 10));
context.startActivity(myIntent);
Transition.TransitionForward(context);
}
});
the myIntent mExtras = Bundle[{thread_id=1}].
This code, puts a value 1 with key thread_id. Then I start the activity, and then I read it here
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thread_screen);
// activates the action bar
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
int thread_id = getIntent().getExtras().getInt("thread_id");
setUpScreen(thread_id);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
Here thread_id has a value of 0. Does anyone know what's wrong?
Thanks

You are writing a String key and reading an int. To write and read the same key, you need to use putExtra(String, int) and getInt.

getIntent().getExtras() will give you bundle
In your case you have put String type value in Intent Extra
So to get the value in ThreadClass Activity do below
int thread_id = Integer.parseInt(getIntent().getStringExtra("thread_id"));
Hope this helps

Related

NullPointerException when i tried to get Extras in FragmentActivity

i have a problem to pass extras between an Activity and a FragmentActivity.
This next code create the Intent to start the new Activity:
Intent fichaSerie = new Intent(getActivity(),ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getID());
getActivity().startActivity(fichaSerie);
When I tried to get the Extras in the Fragment Activity, i get a NullPointerException:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
String extra = getIntent().getExtras().getString("serie") //NullPointerException
}
Anyone know what mistake(s) i'm made in my code?
Thank you very much to all.
Thanks all your help guys, i solved my problem using this call:
getIntent().getStringExtra("serie")
I mark this question as solved.
Thank you again guys.
First of all Java is case sensitive so .getID() is not the same as .getId().
If you are trying to get an ID of an objet, use .getId(). That usually returns an Integer value
Pay attention on the type you are putting to extra as Michal said (String or Integer or anything else)
Intent fichaSerie = new Intent(getApplicationContext(), ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getId()); //presuming this id is an Integer or long int ... You could change this to string if it is still usable then (serie.getId().toString())... but you have to get an intent with myIntent.getStringExtra(...), but I think you will not be doing this.
startActivityForResult(fichaSerie, 0);
Getting an extra from ActividadSerie.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
Intent myIntent = getIntent();
String extra = myIntent.getStringExtra("serie"); //try diferent gets... getIntExtra(), getStringExtra()..
}
You are put an Integer value and get it into the String value so that it gets a NullPointerException.

Received Intent doesn't contain Extras

When I'm trying to pass data to another activity it for some reason gets lost. My sending activity looke like:
protected OnItemClickListener onArtistItemClick = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1, int cursor, long arg3) {
Intent artistCardIntent = new Intent(getBaseContext(), ArtistCardActivity.class);
Artist artist = (Artist) adapter.getItemAtPosition(cursor);
artistCardIntent.putExtra("artist_id", artist.getId());
artistCardIntent.putExtra("tt", "tt");
startActivity(artistCardIntent);
};
};
And while debugging I can see that artistCardIntent gets populated, however in the receiving activity Intent i doesn't contain any extra inforamation:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.artist_card);
Intent i = getIntent();
What am I doing wrong?
PS both activities extends FragmentActivity.
Thanks.
To receive the data use getextras() method. Find the following code. i think it helps you
Intent i = getIntent();
Bundle b=i.getExtras();
if(b!=null)
{
String artist= ("artist_id");
}
I don't see that you are getting the extra information.
Check out this answer How do I get extra data from intent on Android?
convert those values to string and try it , It will work
startActivity(new Intent(Home.this,galmenu.class).putExtra("page","home"));
Intent myIntent = getIntent();
test=myIntent.getExtras().getString("page");
it is working perfectly

How to getValue the data on setActivityForresult

This is what i want to do. When a user starts the game, he is taken to a dashboard screen with several categories that he is suppossed to choose from. On clicking any of the categories one is lead to to the a single activity where i need to know how to find which category button was choosen in the ActivityB.
So, in the DashboardActivity, i have this code:
public void onGeneralKnowledgeClick(View v) {
createIntent("GENERAL_KNOWLEDGE", 1);
}
........
........
........
public void onCelebritiesClick(View v) {
createIntent("CELEBRITIES", 6);
}
private void createIntent(String category, int result) {
Bundle bundle = new Bundle();
bundle.putInt(category, result);
Intent intent = new Intent(this.getApplicationContext(),
QuestionActivity.class);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
Now, in my QuestionActivity class, i have a method that tries to get the categoryNumber associated with the intent that was started. So, i have something like this:
private int getCategory() {
Bundle bundle = this.getIntent().getExtras();
int categ = bundle.getInt("GENERAL_KNOWLEDGE");
return categ;
}
My problem is that, how do i return the integer category such and not hardcording as i did up here. I want this method to return a the integer from the respective bundle. my idea is to have a switch statement inside the getCategory but i don't know what case value to use. Also, i saw someone saying that onActivityResult can be used but i don't see how.
Please help!
I believe you're a bit confused. You merely need to do:
bundle.putInt("CATEGORY", result); //where result == the categoryId and "CATEGORY" is always "CATEGORY"
Then in your getCategory():
int categ = bundle.getInt("CATEGORY") //you'll get the int value that you fed before
Unless I misunderstood.

Android Intent issue

I want to show the details of a calculation in another activity when click on a button.
How to achieve this.
my first activity java code is
public void onClick(View v)
{
if(v.getId()==R.id.Button07)
{
Intent intent=new Intent(AdvancedCalculator.this,calculatedData.class);
startActivity(intent);
}
The calculation i want to do is
String a,b;
Integer vis;
a = txtbox3.getText().toString();
b = textview1.getText().toString();
vis = (Integer.parseInt(a)*Integer.parseInt(b))/100;
tv.setText(vis.toString());
i want the result 'tv' to be shown in next activity when i press the submit button.
Where I need to include this calculation.and what are the further steps
Any help is greatly appreciated
Thanks
In your first activity:
public void onClick(View v) {
//Put your calculation code here
Bundle b = new Bundle();
b.putString("answer", youranswer);
//You could also use putInteger, whichever you prefer.
Intent intent=new Intent(AdvancedCalculator.this,calculatedData.class);
intent.putExtras(b);
startActivity(intent);
}
In your second activity, in the onCreate put this:
Bundle b = getIntent().getExtras();
String answer = b.getString("answer");
Answer is your key. it is used to identify what you want to get from the bundle. Using unique keys means you can pass more than one value to the next activity, if you want.

Passing value from one window to the another in android

I want to show the value inserted by user in first window to the next window.
I am accepting the User weight & height in first window and I want to show it on the second screen as Your weight & Height.
I search a lot and even tried a code but in emulator m getting forcefully closed error.
First Activity :
public class BMI_Main extends Activity
{
EditText BMI_weight;
public String weight;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_main);
Button submit =(Button)findViewById(R.id.BMI_submit);
BMI_weight = (EditText)findViewById(R.id.BMI_EdTx_kg);
submit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
weight = BMI_weight.getText().toString();
// create a bundle
Bundle bundle = new Bundle();
// add data to bundle
bundle.putString("wt", weight);
// add bundle to the intent
Intent intent = new Intent(v.getContext(), BMI_Result.class);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
}
);
Second Activity :
public class BMI_Result extends Activity
{
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi_result);
//get the bundle
Bundle bundle = getIntent().getExtras();
// extract the data
String weight = bundle.getString("wt");
Weight.setText(weight);
}
So please help me for it..
As far as I can see you have the following member definition in BMI_Result:
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
But you can only call findViewById after the class was initialized, since it is a member function of the View class, so change this line to:
TextView Weight;
And add this line to the onCreate method right after setContentView(...):
Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
Edit: It said "...right after super.onCreate(...)", now it's correct ;)
You should override onCreate()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
And the token at the end of onCreate is wrong.
You should use the Context.startActivity(Intent intent) method in your first window/Activity.
Store your data which you want to pass to the second window/Activity in the Intent object, like:
Intent intent = new Intent();
intent.putExtra("weight", weight);
intent.putExtra("height", height);
this.startActivity(intent);
And retrieve them in the second screen/Activity in the onCreate method, like:
Intent intent = getIntent(); // This is the intent you previously created.
int weight = intent.getExtra("weight");
int height = intent.getExtra("height");

Categories

Resources