BigNerdRanch android tutorial, ClassCast exception in geoquiz -


can`t find out bug. application crashes when pressing "cheat" button (starting second activity). throws classcast exception

geoquizactivity.java:

package com.bignerdranch.android.quiz;      import android.app.activity;  import android.content.intent;  import android.os.bundle;  import android.util.log;  import android.view.view;  import android.widget.button;  import android.widget.imagebutton;  import android.widget.textview;  import android.widget.toast;        public class geoquizactivity extends activity {      /** called when activity first created. */  	private static final string tag = "geoquizactivity";  	private static final string key_index ="index";     private button mtruebutton;     private button mfalsebutton;     private button mcheatbutton;     private imagebutton mnextbutton;     private imagebutton mprevbutton;     private textview mquestiontextview;     private boolean mischeater;          private void updatequestion(){  	   int question = mquestionbank[mcurrentindex].getquestion();  		mquestiontextview.settext(question);     }     private void checkanswer(boolean userpressedtrue){  	   boolean answeristrue = mquestionbank[mcurrentindex].istruequestion();  	   int messageresid = 0;  	   if(mischeater){  		   messageresid=r.string.judgment_toast;  	   }  	   else{  		   if (userpressedtrue==answeristrue){  			   messageresid =r.string.correct_toast;  		   } else{  			   messageresid = r.string.incorrect_toast;  		   }  		   toast.maketext(this, messageresid, toast.length_short).show();  	   }     }     @override     protected void onactivityresult(int requestcode, int resultcode, intent data){  	   if(data==null){  		   return;  	   }  	   mischeater = data.getbooleanextra(cheatactivity.extra_answer_shown, false);     }     private truefalse [] mquestionbank = new truefalse[] {  		   new truefalse(r.string.question_oceans, true),  		   new truefalse(r.string.question_mideast, false),  		   new truefalse(r.string.question_africa, false),  		   new truefalse(r.string.question_americas, true),  		   new truefalse(r.string.question_asia, true),     };     private int mcurrentindex = 0;  	@override      public void oncreate(bundle savedinstancestate) {  		log.d(tag, "oncreate(bundle) called");          super.oncreate(savedinstancestate);          setcontentview(r.layout.main);          if(savedinstancestate != null){          	mcurrentindex = savedinstancestate.getint(key_index, 0);          }          mquestiontextview = (textview)findviewbyid(r.id.question_text_view);          mquestiontextview.setonclicklistener(new view.onclicklistener(){          	@override          	public void onclick(view v){          		mcurrentindex=(mcurrentindex+1)%mquestionbank.length;          		updatequestion();          	}          });                    mtruebutton = (button)findviewbyid(r.id.true_button);          mtruebutton.setonclicklistener(new view.onclicklistener(){          	@override          	public void onclick (view v){          		checkanswer(true);          		    			          		}          	});                    mfalsebutton = (button)findviewbyid(r.id.false_button);          mfalsebutton.setonclicklistener(new view.onclicklistener(){          	@override          	public void onclick (view v){          		checkanswer(false);          	}          	});          mnextbutton =(imagebutton)findviewbyid(r.id.next_button);          mnextbutton.setonclicklistener(new view.onclicklistener(){          	@override          	public void onclick (view v){          		mcurrentindex= (mcurrentindex+1)%mquestionbank.length;          		updatequestion();          		mischeater=false;          	}          });          mprevbutton = (imagebutton)findviewbyid(r.id.prev_button);          mprevbutton.setonclicklistener(new view.onclicklistener(){          	@override          	public void onclick(view v){          		if(mcurrentindex==0){          			mcurrentindex=mquestionbank.length-1;          			          		}          		else{          		mcurrentindex=(mcurrentindex-1);          		          		}          		updatequestion();          		mischeater=false;          	          }          });          mcheatbutton = (button)findviewbyid(r.id.cheat_button);          mcheatbutton.setonclicklistener(new view.onclicklistener() {          				  			@override  			public void onclick(view v) {  				intent = new intent(geoquizactivity.this, cheatactivity.class);  				boolean answeristrue = mquestionbank[mcurrentindex].istruequestion();  				i.putextra(cheatactivity.extra_answer_is_true, answeristrue);  				startactivityforresult(i, 0);  								  			}  		});                    		          updatequestion();                    }  	@override  	public void onsaveinstancestate(bundle savedinstancestate){  		super.onsaveinstancestate(savedinstancestate);  		log.i(tag, "onsaveinstancestate");  		savedinstancestate.putint(key_index, mcurrentindex);  	}  	@override  	public void onstart(){  		super.onstart();  		log.d(tag, "onstart() called");  		}  	@override  	public void onpause(){  		super.onpause();  		log.d(tag, "onpause() called");  	}  	  	@override  	public void onresume(){  		super.onresume();  		log.d(tag, "onresume() called");  	}  	@override  	public void onstop(){  		super.onstop();  		log.d(tag, "onstop() called");  	}  	@override  	public void ondestroy(){  		super.ondestroy();  		log.d(tag, "ondestroy() called");  	}  }

cheatactivity.java:

package com.bignerdranch.android.quiz;    import android.app.activity;  import android.content.intent;  import android.os.bundle;  import android.view.view;  import android.widget.button;  import android.widget.textview;    public class cheatactivity extends activity {  	private boolean mansweristrue;  	private textview manswertextview;  	private button mshowanswer;  	public static final string extra_answer_is_true = "com.bignerdranch.android.quiz.answer_is_true";  	public static final string extra_answer_shown = "com.bignerdranch.android.quiz.answer_shown";  	private void setanswershownresult(boolean isanswershown){  		intent data = new intent();  		data.putextra(extra_answer_shown, isanswershown);  		setresult(result_ok, data);  	}  	  @override  protected void oncreate(bundle savedinstancestate){  	super.oncreate(savedinstancestate);  	setcontentview(r.layout.activity_cheat);  	mansweristrue = getintent().getbooleanextra(extra_answer_is_true, true);  	manswertextview = (textview)findviewbyid(r.id.answertextview);  	mshowanswer = (button)findviewbyid(r.id.showanswerbutton);  	setanswershownresult(false);  	mshowanswer.setonclicklistener(new view.onclicklistener() {  		  		@override  		public void onclick(view v) {  			if(mansweristrue){  				manswertextview.settext(r.string.true_button);  			}  			else{  				manswertextview.settext(r.string.false_button);  			}  			setanswershownresult(true);  			  			  		}  	});  }  }

main.xml

<?xml version="1.0" encoding="utf-8"?>    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:gravity="center"  android:orientation="vertical" >    <textview  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:padding="24dp"  android:id="@+id/question_text_view" />  <linearlayout  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:orientation="horizontal" >  <button      android:id="@+id/true_button"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="@string/true_button" />  <button      android:id="@+id/false_button"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="@string/false_button" />  </linearlayout>  <linearlayout  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:orientation="horizontal">  <imagebutton  android:id="@+id/prev_button"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:contentdescription="@string/prev_button"  android:src="@drawable/arrow_left"  />  <imagebutton  android:id="@+id/next_button"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:contentdescription="@string/next_button"   android:src="@drawable/arrow_right"  />  <button   android:id="@+id/cheat_button"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="@string/cheat_button"/>    </linearlayout>  </linearlayout>

<?xml version="1.0" encoding="utf-8"?>  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical"       android:gravity="center">        <textview  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:padding="24dp"  android:text="@string/warning_text"/>    <textview   android:id="@+id/answertextview"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:padding="24dp"/>    <textview  android:id="@+id/showanswerbutton"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="@string/show_answer_button"/>        </linearlayout>

thanks!

edit: logcat: dont know line causes exception or how find it. in logcat, its in 3rd line top.

05-15 15:32:57.116: w/dalvikvm(349): threadid=1: thread exiting uncaught exception (group=0x40015560)  05-15 15:32:57.256: e/androidruntime(349): fatal exception: main  05-15 15:32:57.256: e/androidruntime(349): java.lang.runtimeexception: unable start activity componentinfo{com.bignerdranch.android.quiz/com.bignerdranch.android.quiz.cheatactivity}: java.lang.classcastexception: android.widget.textview  05-15 15:32:57.256: e/androidruntime(349): 	at android.app.activitythread.performlaunchactivity(activitythread.java:1647)  05-15 15:32:57.256: e/androidruntime(349): 	at android.app.activitythread.handlelaunchactivity(activitythread.java:1663)  05-15 15:32:57.256: e/androidruntime(349): 	at android.app.activitythread.access$1500(activitythread.java:117)  05-15 15:32:57.256: e/androidruntime(349): 	at android.app.activitythread$h.handlemessage(activitythread.java:931)  05-15 15:32:57.256: e/androidruntime(349): 	at android.os.handler.dispatchmessage(handler.java:99)  05-15 15:32:57.256: e/androidruntime(349): 	at android.os.looper.loop(looper.java:123)  05-15 15:32:57.256: e/androidruntime(349): 	at android.app.activitythread.main(activitythread.java:3683)  05-15 15:32:57.256: e/androidruntime(349): 	at java.lang.reflect.method.invokenative(native method)  05-15 15:32:57.256: e/androidruntime(349): 	at java.lang.reflect.method.invoke(method.java:507)  05-15 15:32:57.256: e/androidruntime(349): 	at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839)  05-15 15:32:57.256: e/androidruntime(349): 	at com.android.internal.os.zygoteinit.main(zygoteinit.java:597)  05-15 15:32:57.256: e/androidruntime(349): 	at dalvik.system.nativestart.main(native method)  05-15 15:32:57.256: e/androidruntime(349): caused by: java.lang.classcastexception: android.widget.textview  05-15 15:32:57.256: e/androidruntime(349): 	at com.bignerdranch.android.quiz.cheatactivity.oncreate(cheatactivity.java:28)  05-15 15:32:57.256: e/androidruntime(349): 	at android.app.instrumentation.callactivityoncreate(instrumentation.java:1047)  05-15 15:32:57.256: e/androidruntime(349): 	at android.app.activitythread.performlaunchactivity(activitythread.java:1611)  05-15 15:32:57.256: e/androidruntime(349): 	... 11 more

logcat friend:

... @ com.bignerdranch.android.quiz.cheatactivity.oncreate(cheatactivity.java:28)  ... 

apparently, you're trying cast object class it's not assignable from. check cheatactivity.java, line 28.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -