java - 当我将“extends Activity”更改为“extends ListActivity”时,为什么会收到 "source not found"?

标签 java android android-activity listactivity extends

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

这是类 Activity :

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);
    }
}

这是 Activity 的 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>

这是 list 的 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>

提前致谢, 亚历克斯

最佳答案

我想分享关于 ListActivity 的一点。

your layout must contain a ListView with the android:id attribute set to @android:id/list

For example 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);
    }
}

如果你想要listview的实例,那么你可以调用getListView()

关于java - 当我将“extends Activity”更改为“extends ListActivity”时,为什么会收到 "source not found"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12160761/

相关文章:

java - BeanUtils.populate 是否有递归版本?

java - JDBC架构子系统

java - 使用 Http-request 在单个请求中获取响应代码和响应正文

android - stroke 属性不适用于 android studio 中的按钮

android - 从程序中启动隐藏的 Android 设置 Activity

android - 为什么要在 onPause() - onResume() 而不是 onStop() - onStart() 中提交/读取 Android 数据?

java - 了解 JDBC 内部结构

java - Java中交替显示2个字符串

android - 如何在 Android Studio 模拟器上安装 Google Play

Android 10 - 奇怪的 Activity 生命周期