2015-01-21 5 views
9

पर नहीं डाला जा सकता है, मुझे यकीन नहीं है कि मुझे नीचे दिए गए कोड में क्लासकास्ट अपवाद क्यों मिल रहा है। गतिविधि में परिवर्तन गतिविधि बदलना त्रुटि को सुधारता है लेकिन मुझे ListActivity चाहिए क्योंकि यह उस बड़े कोड का हिस्सा है जिसे मैं बनाने की कोशिश कर रहा हूं। किसी भी तरह की सहायता को आभार समझेंगे। धन्यवादjava.lang.ClassCastException: android.widget.LinearLayout को android.widget.ListView

MainActivity.java

package com.example.debug; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 


public class MainActivity extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
    <!-- Name Label --> 
    <TextView 
    android:id="@+id/vehicleType" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#43bd00" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:paddingTop="6dip" 
    android:text="Mobile1" 
    android:paddingBottom="2dip" /> 
    <!-- Description Label --> 
    <TextView 
    android:id="@+id/vehicleColor" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#acacac" 
    android:text="Mobile2" 
    android:paddingBottom="2dip" /> 


    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <!-- Cost Label --> 
    <TextView 
    android:id="@+id/fuel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:gravity="left" 
    android:textStyle="bold" 
    android:text="Mobile3" /> 
    <!-- Price Label --> 
    <TextView 
    android:id="@+id/treadType" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#acacac" 
    android:text="Mobile4" 
    android:paddingBottom="2dip" /> 
    </LinearLayout> 
    </LinearLayout> 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 
</LinearLayout> 

स्टैकट्रेस

FATAL EXCEPTION: main 
Process: com.example.debug, PID: 1370 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.debug/com.example.debug.MainActivity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView 
    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.main(Native Method) 
Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView 
    at android.app.ListActivity.onContentChanged(ListActivity.java:241) 
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293) 
    at android.app.Activity.setContentView(Activity.java:1929) 
    at com.example.debug.MainActivity.onCreate(MainActivity.java:14) 
    at android.app.Activity.performCreate(Activity.java:5231) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
    ... 11 more 

उत्तर

13

अपने समस्या एलईएम आप इसे android:id="@android:id/list" आईडी ListView विजेट के लिए एंड्रॉयड भंडार देना अपने लेआउट

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

की जड़ के साथ है। इसे किसी और चीज में बदलें। उदाहरण के लिए

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/my_linear_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 
5

एक ListActivity एंड्रॉयड में android:id="@android:id/list" साथ विजेट के लिए लग रहा है और एक ListView उम्मीद लेकिन आपके मामले में यह जड़ LinearLayout पाता है और एक ListView के लिए यह इसलिए अपवाद कास्ट करने के लिए कोशिश करता है:

Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView 

इसे हल करने के लिए, आपको ब्लैकबेल्ट द्वारा उल्लिखित एक अलग आईडी प्रदान करने की आवश्यकता है।

3

आपका रेखीय लेआउट आईडी के रूप में इस प्रकार है

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/my_linear_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
0

समस्याग्रस्त आईडी के सभी उपयोगों का पता लगाएं होना चाहिए।
आईडीईए आईडीई में आप Ctrl दबा सकते हैं और सभी उपयोग प्राप्त करने के लिए अपनी आईडी पर क्लिक कर सकते हैं।

नोट: यह इस आईडी के साथ केवल एक आइटम होना चाहिए।

0

यह समस्या हो सकता है जब हम एक LinearLayout बिना आईडी घोषित इतने सारे मामले में एक कंटेनर है, तो आप एक Android डाल चाहिए: id = "@ + id/layout_container" दूसरा स्तर पर अपने LinearLayout में

1

इस त्रुटि का समाधान LinearLayout आईडी विशेषता की अपेक्षा करता है।

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout_container2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <include 
     android:id="@+id/toolbar_noaction" 
     layout="@layout/toolbar"/> 

</LinearLayout> 
संबंधित मुद्दे