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

标签 android listview noclassdeffounderror extends

我想扩展 ListView 但我不明白我做错了什么。这是我的代码,非常简单和基本:

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

最佳答案

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

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

关于android - 如何扩展 ListView。基本示例 ClassNotFoundExcpetion android.view.MyListView 在 dalvik.system,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10957051/

相关文章:

android - OpenGL ES 的正射投影问题

java - 在gradle中获取Android项目的classpath

android - 格式错误的 url 异常 :protocol not found while adding or deleting data from xml in android?

android - java.lang.NoClassDefFoundError : com. 谷歌.android.gms.common.AccountPicker

java - 运行 Scala jar 文件时出现 NoClassDefFoundError

android - AWS SDK NoClassDefFoundError : com. amazonaws.services.s3.AmazonS3Client

android - Android上的HTML5声音问题

android - 为什么 Android 不让您知道 ListView 的滚动 y 位置?

java - 无法在 Recyclerview 的 Android Studio 中解析符号 'get'

android - ListView 上 Android 轻微左/右滑动操作的资源