android - Changing ImageView Resource Error -
i new android , starting out making basic pokedex. i've created 2 buttons on activity each button leading same new activity. works until add in change resource on imageview.
i want able change image in new activity depending on button user presses. have tried button within case statements , there error. moved out mainactivity , received error.
package com.example.pokedex; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.imageview; import android.app.activity; import android.content.intent; public class mainactivity extends activity implements onclicklistener { button buttonsquirtle; button buttonelectabuzz; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); buttonsquirtle=(button)findviewbyid(r.id.moves); buttonelectabuzz=(button)findviewbyid(r.id.moves2); buttonsquirtle.setonclicklistener(this); buttonelectabuzz.setonclicklistener(this); imageview imagemark = (imageview) findviewbyid(r.id.imageview1); imagemark.setimageresource(r.drawable.electabuzzicon); } @override public void onclick(view v) { switch (v.getid()){ case r.id.moves: intent i= new intent(mainactivity.this, squirtle.class); startactivity(i); break; case r.id.moves2: intent j= new intent(mainactivity.this, squirtle.class); startactivity(j); break; default: break; } } }
logcat error posted below:
03-10 11:05:01.560: e/androidruntime(32080): fatal exception: main 03-10 11:05:01.560: e/androidruntime(32080): java.lang.runtimeexception: unable start activity componentinfo{com.example.pokedex/com.example.pokedex.mainactivity}: java.lang.nullpointerexception 03-10 11:05:01.560: e/androidruntime(32080): @ android.app.activitythread.performlaunchactivity(activitythread.java:2295) 03-10 11:05:01.560: e/androidruntime(32080): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2349) 03-10 11:05:01.560: e/androidruntime(32080): @ android.app.activitythread.access$700(activitythread.java:159) 03-10 11:05:01.560: e/androidruntime(32080): @ android.app.activitythread$h.handlemessage(activitythread.java:1316) 03-10 11:05:01.560: e/androidruntime(32080): @ android.os.handler.dispatchmessage(handler.java:99) 03-10 11:05:01.560: e/androidruntime(32080): @ android.os.looper.loop(looper.java:137) 03-10 11:05:01.560: e/androidruntime(32080): @ android.app.activitythread.main(activitythread.java:5419) 03-10 11:05:01.560: e/androidruntime(32080): @ java.lang.reflect.method.invokenative(native method) 03-10 11:05:01.560: e/androidruntime(32080): @ java.lang.reflect.method.invoke(method.java:525) 03-10 11:05:01.560: e/androidruntime(32080): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1187) 03-10 11:05:01.560: e/androidruntime(32080): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1003) 03-10 11:05:01.560: e/androidruntime(32080): @ dalvik.system.nativestart.main(native method) 03-10 11:05:01.560: e/androidruntime(32080): caused by: java.lang.nullpointerexception 03-10 11:05:01.560: e/androidruntime(32080): @ com.example.pokedex.mainactivity.oncreate(mainactivity.java:28) 03-10 11:05:01.560: e/androidruntime(32080): @ android.app.activity.performcreate(activity.java:5372) 03-10 11:05:01.560: e/androidruntime(32080): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1104) 03-10 11:05:01.560: e/androidruntime(32080): @ android.app.activitythread.performlaunchactivity(activitythread.java:2257) 03-10 11:05:01.560: e/androidruntime(32080): ... 11 more
here activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <button android:id="@+id/moves" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="50dp" android:text="@string/squirtle_button" /> <button android:id="@+id/moves2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/moves" android:layout_centerhorizontal="true" android:layout_margintop="20dp" android:text="@string/electabuzz_button" /> </relativelayout>
and here other xml file imageview1 located:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".squirtle" > <button android:id="@+id/moves" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:layout_marginright="58dp" android:layout_margintop="52dp" android:text="@string/moves" /> <button android:id="@+id/coverage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/moves" android:layout_below="@+id/moves" android:text="@string/weaknesses" /> <button android:id="@+id/evolution" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/coverage" android:layout_below="@+id/coverage" android:text="@string/evolution" /> <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/evolution" android:layout_toleftof="@+id/moves" android:src="@drawable/squirtleicon" android:contentdescription="@string/squirtle_image_desc" /> </relativelayout>
thank in advance.
i accomplish using intent when launching second activity:
// launch same activity both buttons, add different value in extras case r.id.moves: intent i= new intent(mainactivity.this, squirtle.class); i.putextra("buttonpressed", "squirtle"); startactivity(i); break; case r.id.moves2: intent j= new intent(mainactivity.this, squirtle.class); j.putextra("buttonpressed", "electabuzz"); startactivity(j); break;
then, in second activity's oncreate method:
// intent used launch activity intent launchingintent = getintent(); // extras bundle content = launchingintent.getextras(); // set image based on button pressed string buttonpressed = content.getstring("buttonpressed"); imageview imageview = (imageview) findviewbyid(r.id.imageview1); if (buttonpressed.equals("squirtle")) { imageview.setimageresource(r.drawable.squirtleicon); } else if (buttonpressed.equals("electabuzz")) { imageview.setimageresource(r.drawable.electabuzzicon); }
this mean different image displayed in second activity, based on button user pressed in first activity.
might idea define strings using constants prevent mis-type errors e.g. in mainactivity:
public final static string extra_button_pressed = "buttonpressed"; public final static string button_squirtle = "squirtle"; public final static string button_electabuzz = "electabuzz";
and reference in code instead:
i.putextra(extra_button_pressed, button_squirtle);
...
j.putextra(extra_button_pressed, button_electabuzz);
and in second activity:
string buttonpressed = content.getstring(mainactivity.extra_button_pressed);
...
if (buttonpressed == mainactivity.button_squirtle) { ...
the first way work more susceptible typos, amongst other things :)
Comments
Post a Comment