2017-04-19 2 views
-8

Nach dem Hinzufügen der folgenden Relative-Layout und verschachtelte Elemente (Image, Textview) in das Hauptlayout gegossen werden, schließt meine app Kraft beim Öffnen, mit dem Fehler java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView:Image kann nicht auf android.widget.TextView

Hier die Stacktrace:

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)  

Hier ist der Ausschnitt aus meiner Layout-Datei:

<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> 

`` `

Hier ist die Codezeile mit dem Thema:

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

Ändern der (TextView) zu (ImageView) führt dies nicht beheben.

Wie ändere ich den ID-Typ zu ImageView?

+2

zeigen Sie Ihren Code bitte –

+3

recht klare Ausnahme, Sie versuchen, eine Image zu einem Textview in Zeile 163 in Ihrer Klasse ActivityExpotecMainView.java zu werfen. – Denny

+0

Zeigen Sie Ihren Java-Code – vishnumm93

Antwort

1

Verwenden Sie tvMainDownloadEvent für Textansicht und ivMainDownloadEvent für Bildansicht.

Layout ändern zu

<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

Danke, und danke auch, wenn Sie meine Frage nicht abgelehnt haben. – cdabel

+1

Ich habe nicht downvote. – Pehlaj

2

Das Problem liegt in Ihrem Java-Code, nicht in XML. Höchstwahrscheinlich haben Sie die TextView und ImageView ID's in findViewById() Methode widerlegt.

4

Ihre Logcat wirft ClassCastException

Geworfen, um anzuzeigen, dass der Code ein Objekt in eine Unterklasse zu werfen versucht hat, von denen es keine Instanz ist.

Problem beginnt von

android:id="@+id/tvMainDownloadEvent" 

Ihre Textview ist Id .DO dies nicht gesetzt in Image Abschnitt.

Sie sollten dies von Ihrer Java-Klasse ändern.

Verwandte Themen