2017-04-19 80 views
-8

添加以下相對佈局和嵌套元素(ImageView的,TextView的),以主要佈局後,我的應用程序強制關閉時開放,與錯誤java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextViewImageView的不能轉換爲android.widget.TextView

這裏的堆棧跟蹤:

E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.fogtechnologies.expotec.urban, PID: 2898 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fogtechnologies.expotec.urban/com.fogtechnologies.expotecshell.urban.activities.ActivityExpotecMainView}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
    at android.app.ActivityThread.access$800(ActivityThread.java:135) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5017) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
    at dalvik.system.NativeStart.mai(Native Method) 
Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView 
    at com.fogtechnologies.expotecshell.urban.activities.ActivityExpotecMainView.findViews(ActivityExpotecMainView.java:163) 
    at com.fogtechnologies.expotecshell.urban.activities.ActivityExpotecMainView.onCreate(ActivityExpotecMainView.java:77) 
    at android.app.Activity.performCreate(Activity.java:5231) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)  
    at android.app.ActivityThread.access$800(ActivityThread.java:135)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:136)  
    at android.app.ActivityThread.main(ActivityThread.java:5017)  
    at java.lang.reflect.Method.invokeNative(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:515)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  
    at dalvik.system.NativeStart.main(Native Method)  

這裏是我的佈局文件中的片段:

<RelativeLayout 
    android:id="@+id/RelativeLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/tvMainDownloadEvent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="80dp" 
     android:layout_marginRight="80dp" 
     android:textColor="@color/white" 
     android:onClick="downloadEventButtonAction" 
     android:src="@drawable/now_performing_shape" 
     android:scaleX="0.8" 
     android:scaleY="0.8" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@id/tvMainDownloadEvent" 
     android:layout_alignTop="@id/tvMainDownloadEvent" 
     android:layout_alignRight="@id/tvMainDownloadEvent" 
     android:layout_alignBottom="@id/tvMainDownloadEvent" 
     android:layout_margin="1dp" 
     android:gravity="center" 
     android:textColor="@color/white" 
     android:textSize="16sp" 
     android:scaleX="0.9" 
     android:scaleY="0.9" 
     android:text="Download Event" /> 

</RelativeLayout> 

```

下面是這個問題的代碼行:

tvMainDownloadEvent = (TextView) findViewById(R.id.tvMainDownloadEvent);

改變(TextView)(ImageView)不解決這個問題。

如何將ID的類型更改爲ImageView?

+2

顯示您的代碼,請 –

+3

很明顯的例外,你想在行163類ActivityExpotecMainView.java投一個的ImageView到一個TextView。 – Denny

+0

顯示您的Java代碼 – vishnumm93

回答

1

對textview使用tvMainDownloadEvent,對imageview使用ivMainDownloadEvent

更改佈局

<RelativeLayout 
    android:id="@+id/RelativeLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/ivMainDownloadEvent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="80dp" 
     android:layout_marginRight="80dp" 
     android:textColor="@color/white" 
     android:onClick="downloadEventButtonAction" 
     android:src="@drawable/now_performing_shape" 
     android:scaleX="0.8" 
     android:scaleY="0.8" /> 

    <TextView 
     android:id="@+id/tvMainDownloadEvent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@id/ivMainDownloadEvent" 
     android:layout_alignTop="@id/ivMainDownloadEvent" 
     android:layout_alignRight="@id/ivMainDownloadEvent" 
     android:layout_alignBottom="@id/ivMainDownloadEvent" 
     android:layout_margin="1dp" 
     android:gravity="center" 
     android:textColor="@color/white" 
     android:textSize="16sp" 
     android:scaleX="0.9" 
     android:scaleY="0.9" 
     android:text="Download Event" /> 

</RelativeLayout> 
+0

謝謝,也謝謝你,如果你沒有downvote我的問題。 – cdabel

+1

我沒有downvote。 – Pehlaj

2

問題出在你的java代碼中,而不是xml中。最有可能的是你在findViewById()方法中混淆了TextViewImageView的id。

4

你的logcat拋出ClassCastException

拋出,表明代碼試圖將對象強制轉換爲 子類,它是不是一個實例。

問題開始從

android:id="@+id/tvMainDownloadEvent" 

是你的TextView編號。不設置這個在的ImageView節。

你應該改變你的Java類。

相關問題