Reputation: 5
I am completely new to Android Studio and would like some help. I'm trying to pass an image ID from my FirstActivity class to my SecondActivity Class. The program is supposed to display a horizontally sliding row of buttons containing the text description of a work of art (this part works). When a button is clicked, ImageActivity should be launched to display the corresponding work of art (application stops running in the emulator).
Any ideas? Thank you! Again, I'm totally new to this, so any insight, even if it's really obvious, would be helpful.
Here's MainActivity:
private LinearLayout mLinearList;
private String id;
private Painting painting;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
//REFERENCE THE SCROLLABLE LAYOUT STRUCTURE IN MAIN_SCREEN.XML
mLinearList = (LinearLayout) findViewById(R.id.linearList);
//FILL THE SCROLLABLE LAYOUT STRUCTURE WITH PAINTINGS
fillTextCarousel();
}
private void fillTextCarousel() {
// POPULATE THE LINEAR LIST CAROUSEL WITH PAINTINGS AND DESCRIPTIONS
Button buttonItem;
for (int i = 0; i < RenaissanceDatabase.description.length; i++) {
//STORE THE INDIVIDUAL PAINTINGS AS BUTTONS
buttonItem = new Button(this);
painting = new Painting(RenaissanceDatabase.description[i], RenaissanceDatabase.id[i]);
//USE THE CONTENT DESCRIPTION PROPERTY TO STORE
//PAINTING DATA
buttonItem.setContentDescription(painting.getDescription());
buttonItem.setText(painting.getDescription());
//SET AN ONCLICK LISTENER FOR THE TEXT BUTTON
buttonItem.setOnClickListener(displayPainting);
//ADD THE IMAGE BUTTON TO THE SCROLLABLE LINEAR LIST
mLinearList.addView(buttonItem);
}
}
private View.OnClickListener displayPainting = new View.OnClickListener() {
public void onClick(View btn) {
// COLLECT THE IMAGE STORED FOR THE PAINTING
Intent imgIntent = new Intent(getApplicationContext(), ImageActivity.class);
imgIntent.setAction(imgIntent.ACTION_SEND);
imgIntent.putExtra("image_id:",painting.getId());
startActivity(imgIntent);
}
};
Here's what I have so far for ImageActivity:
private Button imgButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
painting = new Painting(RenaissanceDatabase.description[index], RenaissanceDatabase.id[index]);
Intent objIntent=this.getIntent();
ImageView art = (ImageView) findViewById(R.id.imageView2);
art.setImageResource(Integer.parseInt(getExtra()));;
}
private String getExtra(){
String image_id = getIntent().getStringExtra("image_id");
return image_id;
}
And here is what the paintings database looks like:
public static String description[] = {
"Venus of Urbino\nTitan, 1538",
"St. John the Baptist\nLeonardo da Vinci, 1516",
"Protrait of Baldassare Castiglione\nRaphael, 1515",
"The Entombent of Christ\nCaravaggio, 1603",
"Coronation of the Virgin\nFra Angelico, 1435",
"Mars and Venus\n Sandro Bottcelli, 1483"};
public static int id[] = {
R.drawable.painting1, // VENUS OF URBINO
R.drawable.painting2, // ST.JOHN BAPTIST
R.drawable.painting3, // BALDASSARE
R.drawable.painting4, // ENTOMBENT OF CHRIST
R.drawable.painting5, // CORONOATION
R.drawable.painting6 // MARS AND VENUS
};
Here's the stack trace when it crashes:
03-21 15:41:01.283 14004-14004/edu.augustana.csc490.renaissancepaintings E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.augustana.csc490.renaissancepaintings/edu.augustana.csc490.renaissancepaintings.ImageActivity}: java.lang.NumberFormatException: Invalid int: "null" at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) at android.app.ActivityThread.access$600(ActivityThread.java:130) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NumberFormatException: Invalid int: "null" at java.lang.Integer.invalidInt(Integer.java:138) at java.lang.Integer.parseInt(Integer.java:355) at java.lang.Integer.parseInt(Integer.java:332) at edu.augustana.csc490.renaissancepaintings.ImageActivity.onCreate(ImageActivity.java:40) at android.app.Activity.performCreate(Activity.java:5008) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) at android.app.ActivityThread.access$600(ActivityThread.java:130) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) 03-21 15:41:03.083 14004-14004/? I/Process: Sending signal. PID: 14004 SIG: 9
Upvotes: 0
Views: 766
Reputation: 44230
Two problems, you have a typo in:
1) imgIntent.putExtra("image_id:",painting.getId());
image_id:
has a colon in it, and the corresponding key retrieval does not have a colon. So that explains your null.
2) in your retrieval, stop casting to Integer, and instead use getIntExtra("image_id")
instead of getStringExtra
In the future, you can also check to see if your Intent Bundle contains the key you are looking for, before you try to retrieve it, preventing a null exception from crashing you app, using getIntent().hasExtra("key")
Upvotes: 0
Reputation: 7070
Change
art.setImageDrawable(getResources().getDrawable(Integer.parseInt(getExtra())));
by
art.setImageResource(Integer.parseInt(getExtra()));
Upvotes: 1