ItsJonSnow ItsJonSnow - 4 months ago 19
Android Question

Android app crash due to images

I am pretty new to android. I was writing a program to change the image on button click. Without the images, my app works perfectly. But with images, the app crashes.

SIZE of img - 4kb, 80 X 80 in dimension.

My code -

protected void clickMe(View view){
EditText phone = (EditText) findViewById(R.id.phone);
EditText name = (EditText) findViewById(R.id.name);

ImageView img = (ImageView) findViewById(R.id.img1);
img.setImageResource(R.drawable.x);

Toast.makeText(getApplicationContext(), "yo dudE "+name.getText().toString(), Toast.LENGTH_LONG).show();

Log.i("Phone : ", phone.getText().toString());

}


Stacktrace :


07-04 11:06:04.964 10037-10037/com.satyakisanyal.myfirstandroidproject
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.satyakisanyal.myfirstandroidproject, PID: 10037
java.lang.IllegalStateException: Could not execute method for
android:onClick
at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
cannot be cast to android.widget.ImageView
at
com.satyakisanyal.myfirstandroidproject.MainActivity.clickMe(MainActivity.java:22)
at java.lang.reflect.Method.invoke(Native Method) 
at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Answer
ImageView img = (ImageView) findViewById(R.id.img1);

It appears that id img1 is of RelativeLayout out and not of an ImageView

Comments