【问题标题】:Why do i get "source not found" when I change extends Activity to extends ListActivity?当我将扩展 Activity 更改为扩展 ListActivity 时,为什么会出现“找不到源”?
【发布时间】:2012-08-28 13:51:59
【问题描述】:

我在这门课上有问题。当我按下按钮时,将调用此 Activity。当我将 Activity 扩展到此类时,程序会进入该类,但是当我扩展 ListActiviy 并单击按钮时,调试器会用红色字样告诉我“找不到源”,没有其他内容,甚至在 logcat 中也没有。 请告诉我此活动的 xml 文件或清单中是否缺少某些内容。

这是课堂活动:

public class SeeAllEntriesActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_entries);

        ActivitiesObjects ao = (ActivitiesObjects)this.getApplication();
        List<Customer> listOfCostumer = ao.getListOfCustomers();

        ListAdapter listAdapter = new ArrayAdapter(this, R.layout.activity_all_entries, listOfCostumer);
        ListView listViewCustomer = (ListView) findViewById(R.id.listViewCustomer);
        listViewCustomer.setAdapter(listAdapter);
    }
}

这是活动的xml:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="all entries" />


    <ListView
        android:id="@+id/listViewCustomer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    </ListView>

</LinearLayout>

这是清单的 xml:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application android:name="com.example.testapp.ActivitiesObjects" android:label="@string/app_name">
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="CreateActivity"></activity>
        <activity android:name="SeeAllEntriesActivity"></activity>
    </application>    

</manifest>

提前致谢, 亚历克斯

【问题讨论】:

    标签: java android android-activity listactivity extends


    【解决方案1】:

    关于 ListActivity 我想分享一点。

    您的布局必须包含一个设置了 android:id 属性的 ListView 到@android:id/list

    例如 android:id="@android:id/list"

    您的布局文件将如下所示

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="all entries" />
    
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
        </ListView>
    
    </LinearLayout>
    

    设置列表适配器可以使用setListAdapter函数

    public class SeeAllEntriesActivity extends ListActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_all_entries);
    
            ActivitiesObjects ao = (ActivitiesObjects)this.getApplication();
            List<Customer> listOfCostumer = ao.getListOfCustomers();
    
            ListAdapter listAdapter = new ArrayAdapter(this, R.layout.activity_all_entries, listOfCostumer);
            // no need to fetch list view instance
            // ListView listViewCustomer = (ListView) findViewById(R.id.listViewCustomer);
            setListAdapter(listAdapter);
        }
    }
    

    如果你想拥有列表视图的实例,那么你可以打电话给getListView ()

    【讨论】:

    • 感谢您的快速回答。我像你说的那样更改了代码,活动真的在模拟器上打开,没有任何错误。不幸的是,当列表包含数据时,他向我显示了一个错误,但我认为这是因为我必须编写一个适配器。不过谢谢,现在我可以专注于这个问题了:)
    【解决方案2】:

    您忘记了 2 个“.”(点)。

    替换:

    <activity android:name="CreateActivity"></activity>
    <activity android:name="SeeAllEntriesActivity"></activity>
    

    与:

    <activity android:name=".CreateActivity"></activity>
    <activity android:name=".SeeAllEntriesActivity"></activity>
    

    另外,您的 application 标记中有 2 个 android:label

    【讨论】:

    • 感谢您的快速回答,我立即尝试了,但不幸的是它没有任何改变。
    • 确保你所有的包和活动路径和名称都是正确的。
    • 我确信它们是正确的,因为当我将 extends ListActivity 更改为 extends Activity 时,他找到了类。
    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多