Why do i get “source not found” when I change extends Activity to extends ListActivity?

☆樱花仙子☆ 提交于 2019-12-25 15:52:23

问题


I've got a problem with this class. This Activity will be called when I press a button. When I extend Activity to this class the programm gets into the class, but when I extend ListActiviy and I click the button the debugger tells me in red words "source not found" and nothing else, not even something in the logcat. Please tell me if there is something missing in the xml-file of this activity or the manifest.

This is the class 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);
    }
}

This is the xml of the activity:

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

This is the xml of the manifest:

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

Thanks in advance, alex


回答1:


One point I like to share regarding ListActivity.

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

For example android:id="@android:id/list"

there for your layout file will be like below

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

For setting list adapter you can use setListAdapter function

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

If you want to have instance of listview then you can call getListView ()




回答2:


You forgot 2 "." (dots).

Replace:

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

with:

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

Also, you have 2 android:label in your application tag.



来源:https://stackoverflow.com/questions/12160761/why-do-i-get-source-not-found-when-i-change-extends-activity-to-extends-listac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!