我遇到了我无法理解的错误:
07-19 22:14:50.750 23192-23192/com.example.shun.zeatbeta E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shun.zeatbeta, PID: 23192
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shun.zeatbeta/com.example.shun.zeatbeta.ReauthenticateActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
at com.example.shun.zeatbeta.ReauthenticateActivity.onCreate(ReauthenticateActivity.java:59)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
在活动中使程序崩溃的行:
private Button loginButton;
loginButton = (Button) findViewById(R.id.loginButton);
activity_reauthenticate2.xml:
<com.example.shun.zeatbeta.ComfortaaButton
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="@dimen/button_height"
android:layout_marginTop="20dp"
android:background="@drawable/round_button_blue"
android:text="@string/login"
android:textColor="@color/white"
android:textSize="15dp"
app:layout_constraintBottom_toTopOf="@id/facebookWrapper"
app:layout_constraintTop_toBottomOf="@id/textFields" />
ComfortaaButton.java
package com.example.shun.zeatbeta;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
public class ComfortaaButton extends AppCompatButton {
public ComfortaaButton(Context context) {
super(context);
init();
}
public ComfortaaButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ComfortaaButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/Comfortaa-Regular.ttf");
setTypeface(tf, 1);
}
}
为什么会出现此错误?该按钮显然是Button
,而不是ImageButton
。我尝试清理项目,重新启动,清除缓存等。甚至尝试创建新的活动和布局文件,错误再次出现!
更新:我在活动中获得了该行崩溃的代码的信息:
Unexpected cast to AppCompatButton: layout tag was ImageButton (Id bound to an ImageButton in activity_landing_page.xml) less... (⌘F1)
Keeps track of the view types associated with ids and if it finds a usage of the id in the Java code it ensures that it is treated as the same type.
因此,似乎它在activity_landing_page.xml中而不是activity_reauthenticate2.xml中按ID查找活动。为什么?我该如何更改?
应用程序gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
applicationId "com.example.s.company"
minSdkVersion 17
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'com.android.support:support-v4:27.1.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.facebook.android:facebook-android-sdk:4.33.0'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
implementation 'com.stripe:stripe-android:6.1.2'
compile 'com.google.firebase:firebase-auth:15.0.0'
compile 'com.google.firebase:firebase-core:15.0.0'
compile 'com.google.firebase:firebase-database:15.0.0'
compile 'com.google.firebase:firebase-storage:15.0.0'
compile 'com.firebaseui:firebase-ui-storage:3.3.1'
compile 'com.android.volley:volley:1.1.0'
compile 'com.felipecsl.asymmetricgridview:library:2.0.1'
compile 'com.google.android.gms:play-services-maps:15.0.1'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.github.ittianyu:BottomNavigationViewEx:1.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support.constraint:constraint-layout:1.1.0'
compile 'com.github.arimorty:floatingsearchview:2.1.1'
implementation 'info.hoang8f:android-segmented:1.0.6'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
答案 0 :(得分:1)
您的loginButton
必须有其他重复ID。只要确保您从R.id.loginButton
中输入loginButton = (Button) findViewById(R.id.loginButton);
作为目标Activity或xml即可正确导入。
答案 1 :(得分:0)
更新: 按钮也应该可以正常工作。
以AppCompatButton初始化视图,
private AppCompatButton mLoginButton;
mLoginButton=findViewById(R.id.loginButton);
答案 2 :(得分:-1)
//我错误编码时出现这个错误 val deleteButton = itemView.findViewById<(Button)>(R.id.item_delete_button) //实际上在 XML 文件中,我使用了图像视图而不是按钮来使该图像 // 可点击。 // 所以我把 <(Button)> 改成了 <(ImageView)>。然后这个问题就解决了。