如何扩展ListView。基本示例ClassNotFoundExcpetion android.view.MyListView位于dalvik.system
我想扩展ListView,但我不明白我做错了什么。以下是我的代码,非常简单和基本: manifest.xml如何扩展ListView。基本示例ClassNotFoundExcpetion android.view.MyListView位于dalvik.system,android,listview,noclassdeffounderror,extends,Android,Listview,Noclassdeffounderror,Extends,我想扩展ListView,但我不明白我做错了什么。以下是我的代码,非常简单和基本: manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.test" android:versionCode="1" android:versionName="1.0" >
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TrymylistviewActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MyListView.java
package com.my.test;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
如果我在res/layout/main.xml中使用ListView而不是myListView,应用程序将在模拟器中运行。当然,如果我用MListview扩展ListView,那么我想使用它。我做错了什么
这是堆栈跟踪的开始和有趣的部分
06-08 22:51:33.409: E/AndroidRuntime(1141): FATAL EXCEPTION: main
06-08 22:51:33.409: E/AndroidRuntime(1141): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.test/com.my.test.TrymylistviewActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class MyListView
06-08 22:51:33.409: E/AndroidRuntime(1141): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)... 11 more
....
06-08 22:51:33.409: E/AndroidRuntime(1141): Caused by: java.lang.ClassNotFoundException: android.view.MyListView
06-08 22:51:33.409: E/AndroidRuntime(1141): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-08 22:51:33.409: E/AndroidRuntime(1141): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
....
06-08 22:51:33.409: E/AndroidRuntime(1141): ... 21 more
您必须使用自己的命名空间:
<MyListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
应该是:
<com.my.test.MyListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
谢谢。我知道这是一个愚蠢的错误,但我自己也没办法弄明白,很难找到答案。希望它能帮助别人。我可以在10分钟内接受你的回答!你真快!编辑:为了完整性,当扩展listview或任何东西时,必须实现所有构造函数,并且必须使用您自己的命名空间引用类。明白了!:)
<MyListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<com.my.test.MyListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>