NullPointerException when i tried to get Extras in FragmentActivity - android

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.

Related

Android bundle value always reading 0

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

Android. The intent only the first time send what I want. Why?

I use intent to send data from activity to other, when I call the 2nd activity repeatedly nothing happens,
The 1st intent:
`
Intent intent = new Intent(this, EditActivity.class);
intent.putExtra("1stNameIntent", firstName);
intent.putExtra("2ndNameIntent", lastName);
intent.putExtra("mailIntent", mail);
intent.putExtra("mobileIntent", mobile);
intent.putExtra("idIntent", intId);
startActivity(intent);`
//There is no problem with values
...
The 2nd activity:
...
`
public String firstNameHint = null;
public String lastNameHint = null;
public String mailHint = null;
public String mobileHint = null;
public int id;
public String idStr = null;`
`protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
idStr = getIntent().getExtras().getString("idIntent");
id = Integer.parseInt(idStr);
firstNameHint = getIntent().getExtras().getString("1stNameIntent");
firstName = (EditText) findViewById(R.id.firstName_ID);
firstName.setText(firstNameHint);
firstName.setHint(firstNameHint);
lastNameHint = getIntent().getExtras().getString("2ndNameIntent");
lastName = (EditText) findViewById(R.id.lastName_ID);
lastName.setText(lastNameHint);
lastName.setHint(lastNameHint);`
...
I cant find out the problem. After the first use of 2nd activity only the default values appears.
Maybe your onCreate Activity is not being called twice, depending on the 'launchMode' of the activity.
Try setting text in your onStart method.
Hope this helps.
implement onNewIntent(Intent i) in your second activity.
If the second activity has already been created (as it has), any future new intents will go to this onNewIntentMethod(...)
So, pull out your intent reading code and the code to set views into another method, and have both onCreate and onNewIntent call this new method that actually does the work.
use
intent i = getIntent();
stringvariable = i.getStringExtra("1stNameIntent");
see this Using putExtra to pass values to intent service
you should make sure that you are calling putExtra() every time you are starting the activity. because I think it only works with one intent.
also try adding Log.d() on the onCreate() of the second activity to know when it is called.

Retrieving the intent values from another class

My question is a bit basic. I've been learning to code on JAVA and android. Here I am a bit confused on how to call the values that I have sent via an intent.
In my first activity this is the intent that I am using.
Intent intent = new Intent(MainActivity.this, Secondactivity.class);
String regName1 = regName;
intent.putExtra(regName1, regNameSplit[0]);
startActivity(intent);
Here regName1 will contain three values. SessionID,URL,Name split by "-".
In my SecondActivity
public class Secondactivity extends Activity {
public final String TAG = "###---Secondactivity---###";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
Log.i(TAG,"before if statement");
if (getIntent().getStringExtra("regName1") != null){
getIntent().getStringExtra("regName1").split("-");
String[] str = "regName";
Log.i(TAG, ""+str[0]+str[1]+str[2])
}
}
}
The value if regName1 always comes as null.
This line
intent.putExtra(regName1, regNameSplit[0]);
Needs to be like this instead
intent.putExtra("regName1", regNameSplit[0]); // note the quotes
BUT you are using regName1 as a variable... how do you expect the second class to know that variable?
Use a string resource instead.
and you are sure that the content of the variable regName is actually "regName"?
cause you set the value using
intent.putExtra(regName, ... )
and you get the value using
intent.getStringExtra("regName")
use firstactivity
dont use
String regname1=regname;
just:
intent.putExtra("regName1", regNameSplit[0]);
in second Activity
if (getIntent().getStringExtra("regName1") != null){
//
}

Send a variable between classes through the Intent

I'm getting problems using the Intent for navigate through the screens. I want to send a variable and use it in the other class.
I'm using a method, the method takes the variable but i don't know how to send it with the intent to the new screen, which will use it to do some things.
Main class calls the metod:
private void pantallaDetalles(final int identificador)
{
startActivityForResult(new Intent(this,MostrarDetalles.class),REQST_CODE);
}
MostrarDetalles.class is the *.java which will take the variable. I'm begining it like this:
public class MostrarDetalles extends Activity {
SQLiteDatabase db;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.detalles);
//more code...
Cursor c = db.rawQuery("SELECT * FROM table WHERE _id="+ identificador, null);
}
Did you see? I'm talking about this. I don't know how to send the "identificador" variable from the main class to the second class through the Intent.
Can you help me with this? Thank you very much in advice.
JMasia.
Use the extras bundle in the intent.
Intent i = new Intent(...);
i.putExtra("name_of_extra", myObject);
Then on onCreate:
getIntent.getIntExtra("name_of_extra", -1);
Screen 1:
Intent i=new Intent("com.suatatan.app.Result");
i.putExtra("VAR_RESULT", "Added !");
startActivity(i);
Screen 2: (Receiver):
TextView tv_sonuc = (TextView) findViewById(R.id.tv_sonuc);
Bundle bundle = getIntent().getExtras();
String var_from_prev_intent = bundle.getString("VAR_RESULT");
tv_sonuc.setText(var_from_prev_intent);
You can use Intent.putExtra() to bundle the data you want to send with the intent.

problem in passing data in android Activities?

can any one guide me what mistake am i doing in this code??? it not seems to be working..
i have two activies
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(DataPassing.this, DataPassing2.class);
Bundle b = new Bundle();
b.putInt("key", 1123);
intent.putExtras(b);
startActivity(intent);
finish();
}
and in second activity i have written
public void onCreate(Bundle savedInstanceState) {
Bundle b = getIntent().getExtras();
int value = b.getInt("key", 0);
Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
}
but the code is giving me error i dont know why.. i have added second activity to manifest file.. please guide what mistake i am doing ???
any help would be appriciated..
Can you debug the code, or perhaps include some try/catch-blocks, to try and detect where the error is happening, and what the error message is?
Other than that, try doing it this way instead:
Intent intent = new Intent(DataPassing.this, DataPassing2.class);
intent.putExtra("key", 1123);
startActivity(intent);
... and still fetch the bundle in DataPassing2 as you have been before. I don't know if it'll help, because I don't know much about what your error is, but it might.
Try this one may be it work.
public void onCreate(Bundle savedInstanceState) {
Bundle b = getIntent().getExtras();
int value = b.getInt("key");
Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
}

Categories

Resources