如何扩展ListView. dalvik.system的基本示例ClassNotFoundExcpetion android.view.MyListView

我想扩展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" >

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

RES /布局/ main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<MyListView android:id="@+id/android:list"
      android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

<TextView android:id="@+id/android:empty"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"/>


</LinearLayout>

TrymylistviewActivity.java

package com.my.test;

import android.app.ListActivity;
import android.os.Bundle;

public class TrymylistviewActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}

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"/>
上一篇:javascript – 在Vuex模块中进行继承的方法


下一篇:Java 继承