problem in passing data in android Activities? - android

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();
}

Related

getSerializableExtra() is null randomly

There is a link in one activity, after I click on it it opens another activity. I add an enum parameter for the second activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
binding.myLink.setOnClickListener(new View.OnClickListener() {
// ...
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivityContext(), SecondActivity.class);
intent.putExtra(KEY, MyEnum.ENUM_VALUE);
startActivity(intent);
}
});
// ...
}
In onCreate of second activity I read this parameter
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// get value
FirstActivity.MyEnum value = (FirstActivity.MyEnum ) getIntent()
.getSerializableExtra(AboutActivity.KEY);
// ...
}
Everything works but in Crashlytics I see that for some users value is null. Second activity is called only from first one and from nowhere else.
Can someone suggest me the scenario with this behavior? When can it happen like this?
I opened my app, opened the second activity and put app to background. After several hours I opened my app from the list of apps and everything is ok. No more ideas when it can happen.
use this :
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivityContext(), SecondActivity.class);
Bundle bundle=new Bundle();
bundle.putSerializable(serializableKEY,yourSerializableClass);
intent.putExtras(KEY, bundle);
startActivity(intent);
}
});
and in second activity :
Bundle bundle=getIntent().getExtras();
yourSerializableClass clazz=(yourSerializableClass)bundle.getSerializable(serializableKEY);

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

Don't understand why I'm getting NullPointerException

I don't understand why I my app keeps on crashing. According to LogCat, it is caused by a NullPointerExeption on a line, however, I cannot find why it is occurring. What's supposed to happen is an image resource for an ImageView is changed in MainActivity by pressing a button on another activity. I have followed multiple guides, but get the recurring error. From what I can see it occurs when extra information is passed through an Intent, something is null but I cannot find it.
LogCat
05-03 22:33:03.158: E/AndroidRuntime(31629): Caused by: java.lang.NullPointerException
05-03 22:33:03.158: E/AndroidRuntime(31629): at com.crackedporcelain.crackedcalibration.MainActivity.onCreate(MainActivity.java:21)
Line 21 (offender in MainActivity)
String gridPressed = content.getString("gridPressed");
Main Activity (receiver)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle content = getIntent().getExtras();
// Set image based on which button was pressed
String gridPressed = content.getString("gridPressed");
ImageView spotlightOne = (ImageView) findViewById(R.id.variousOne);
if (gridPressed.equals("one")) {
spotlightOne.setImageResource(R.drawable.one);
} else if (gridPressed.equals("two")) {
spotlightOne.setImageResource(R.drawable.two);
} else {
spotlightOne.setImageResource(R.drawable.one);
}
ImageView buttonGrid = (ImageView) findViewById(R.id.phoneReference);
buttonGrid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(
"com.crackedporcelain.crackedcalibration.IDENTIFICATION"));
}
});
}
Other Activity (sender)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.identification);
ImageView gridButton1 = (ImageView) findViewById(R.id.buttonImage1);
ImageView gridButton2 = (ImageView) findViewById(R.id.buttonImage2);
gridButton1.setOnClickListener(this);
gridButton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonImage1:
Intent oneA = new Intent(IdentificationActivity.this, MainActivity.class);
oneA.putExtra("gridPressed", "one");
startActivity(oneA);
break;
case R.id.buttonImage2:
Intent oneB = new Intent(IdentificationActivity.this, MainActivity.class);
oneB.putExtra("gridPressed", "two");
startActivity(oneB);
break;
}
}
Additional Info
Layouts for these classes are setup correctly, they work fine. It only force closes when I try to use Intents.
All help is appreciated! Thanks!
The received intent did not contain any extra values. Make sure the code sending the intent had added to the intent an extra String value with the name "gridPressed". You can use Intent.putExtra(String, String) for this purpose.
Intent.getExtras() will return null if there are no extra values. In addition, Bundle.getString(String key) will return null if the Bundle does not contain the key.
Finally, be sure to check that MainActivity is not invocable in any other way (like when the app icon is clicked). If that is possible, then this version of onCreate() will work better:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView spotlightOne = (ImageView) findViewById(R.id.variousOne);
// Set image based on which button was pressed, if any
String gridPressed = getIntent().getStringExtra("gridPressed");
if ("one".equals(gridPressed)) {
spotlightOne.setImageResource(R.drawable.one);
} else if ("two".equals(gridPressed)) {
spotlightOne.setImageResource(R.drawable.two);
} else {
spotlightOne.setImageResource(R.drawable.one);
}
ImageView buttonGrid = (ImageView) findViewById(R.id.phoneReference);
buttonGrid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(
"com.crackedporcelain.crackedcalibration.IDENTIFICATION"));
}
});
}
You're getting a NPE because you are not sending a bundle from the sender activity rather just a string extra. So, to access the String extra in the receiver activity, use :
getIntent().getStringExtra("gridPressed");
To send bundles, you need to first create a bundle, then add extras to that bundle and then pass the bundle with the intent :
Bundle bundle = new Bundle();
bundle.putString("KEY", "VALUE");
intent.putExtras(bundle);
If you send a bundle as mentioned in 2, your existing code in the receiving activity will work, otherwise you can change the code in the receiving activity as mentioned in 1.

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.

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.

Categories

Resources