Sunday 15 July 2012

android - NullPointerException when trying to use ImageViewer -



android - NullPointerException when trying to use ImageViewer -

so i'm trying create stone paper scissors game, worked fine until wanted add together fragments. had alter alot of things , can't thing work. it's hard explain show code it's important. i'm new programming overall.

fragmenttwo

private static fragment thefragment; private void clicklistener(choice mychoice){ hidebuttons(); selection compchoice = choice.choices.getrandom(); //int outcome = choice.decideoutcome(mychoice, compchoice); compchoice.getimage(thefragment, compchoice); } public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ final view view = inflater.inflate(r.layout.start_board, container, false); button rbutton = (button) view.findviewbyid(r.id.buttonrock); rbutton.setonclicklistener(new view.onclicklistener(){ public void onclick(view v) { view.setbackgroundresource(r.drawable.rockdone); selection mychoice = new choice(choices.rock); clicklistener(mychoice); } }); homecoming view; }

choices class

private static actionbaractivity theactivity; public static enum choices { rock, scissor, paper, public static selection getrandom() { homecoming new choice(values()[(int) (math.random() * values().length)]); } } public view getimage(fragment a, selection compchoice){ imageview image = (imageview) theactivity.findviewbyid(r.id.imageviewer); image.setimageresource(r.drawable.rockcom); image.setvisibility(view.visible); homecoming null; }

start_board.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/start_board" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/rps9" android:visibility="visible" > <button android:id="@+id/buttonrock" android:layout_width="60dp" android:layout_height="60dp" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="29dp" android:text="@string/brock" android:textcolorlink="@android:color/transparent" android:visibility="visible" /> <imageview android:id="@+id/imageviewer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:src="@drawable/rockcom" android:visibility="gone"/> </relativelayout>

logcat

11-14 08:57:20.964: e/androidruntime(2066): fatal exception: main 11-14 08:57:20.964: e/androidruntime(2066): process: com.gerfort.gerfortrps, pid: 2066 11-14 08:57:20.964: e/androidruntime(2066): java.lang.nullpointerexception: effort invoke virtual method 'android.view.view android.support.v7.app.actionbaractivity.findviewbyid(int)' on null object reference 11-14 08:57:20.964: e/androidruntime(2066): @ com.gerfort.gerfortrps.choice.getimage(choice.java:39) 11-14 08:57:20.964: e/androidruntime(2066): @ com.gerfort.gerfortrps.fragmenttwo.clicklistener(fragmenttwo.java:108) 11-14 08:57:20.964: e/androidruntime(2066): @ com.gerfort.gerfortrps.fragmenttwo.access$2(fragmenttwo.java:101) 11-14 08:57:20.964: e/androidruntime(2066): @ com.gerfort.gerfortrps.fragmenttwo$4.onclick(fragmenttwo.java:165) 11-14 08:57:20.964: e/androidruntime(2066): @ android.view.view.performclick(view.java:4756) 11-14 08:57:20.964: e/androidruntime(2066): @ android.view.view$performclick.run(view.java:19749) 11-14 08:57:20.964: e/androidruntime(2066): @ android.os.handler.handlecallback(handler.java:739) 11-14 08:57:20.964: e/androidruntime(2066): @ android.os.handler.dispatchmessage(handler.java:95) 11-14 08:57:20.964: e/androidruntime(2066): @ android.os.looper.loop(looper.java:135) 11-14 08:57:20.964: e/androidruntime(2066): @ android.app.activitythread.main(activitythread.java:5221) 11-14 08:57:20.964: e/androidruntime(2066): @ java.lang.reflect.method.invoke(native method) 11-14 08:57:20.964: e/androidruntime(2066): @ java.lang.reflect.method.invoke(method.java:372) 11-14 08:57:20.964: e/androidruntime(2066): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) 11-14 08:57:20.964: e/androidruntime(2066): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)

please inform me if forgot code.

pass imageview instead of fragment method getimage

change :

compchoice.getimage(thefragment, compchoice);

to :

compchoice.getimage(image, compchoice);

and initialize imageview in oncreateview :

image = (imageview) view.findviewbyid(r.id.imageviewer);

new method signature :

public void getimage(imageview image, selection compchoice)

and code :

public void getimage(imageview image, selection compchoice){ image.setimageresource(r.drawable.rockcom); image.setvisibility(view.visible); }

android android-fragments nullpointerexception android-imageview

No comments:

Post a Comment