gpt4 book ai didi

java - Android 应用程序因图像而崩溃

转载 作者:行者123 更新时间:2023-12-02 16:06:13 25 4
gpt4 key购买 nike

我对 Android 还很陌生。我正在编写一个程序来更改按钮单击时的图像。没有图像,我的应用程序可以完美运行。但对于图像,应用程序会崩溃。

img 的大小 - 4kb,尺寸为 80 X 80。

我的代码-

 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());

}

堆栈跟踪:

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)

最佳答案

我希望您在 android 中将 RelativeLayout 转换为 ImageView。检查xml文件中的R.id.img1是什么?

关于java - Android 应用程序因图像而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com