【问题标题】:how to extend ListView. basic example ClassNotFoundExcpetion android.view.MyListView at dalvik.system如何扩展 ListView。 dalvik.system 上的基本示例 ClassNotFoundExcpetion android.view.MyListView
【发布时间】:2012-06-08 23:13:29
【问题描述】:

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

【问题讨论】:

    标签: android listview noclassdeffounderror extends


    【解决方案1】:

    你必须使用自己的命名空间:

        <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 分钟内接受你的回答!你很快!编辑:为了完整起见,在扩展列表视图或任何东西时,必须实现所有构造函数,并且必须使用您自己的命名空间来引用类。知道了! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多