2012-08-28 20 views
0

我有這個課的問題。我按下按鈕時會調用此活動。當我將Activity擴展到這個類時,程序會進入類,但是當我擴展ListActiviy並且單擊按鈕時,調試器會用紅色的詞語「源代碼未找到」告訴我,而不是其他任何東西,甚至不是logcat中的東西。 請告訴我,如果在此活動或清單的xml文件中缺少某些內容。爲什麼我更改擴展Activity以擴展ListActivity時會出現「未找到源」?

這是課外活動:

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> 

在此先感謝, alex

回答

2

有一點我想分享有關。 ListActivity。

佈局必須包含與Android一個ListView:id屬性設置 到@android:ID /列表

例如機器人:ID = 「@機器人:ID /列表」

有你佈局文件會像下面

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

+1

謝謝你的快速回答。我像你說的那樣改變了代碼,這個活動真的在模擬器上打開,沒有任何錯誤。不幸的是,當列表包含數據時,他顯示錯誤,但我認爲這是因爲我必須編寫一個適配器。但是,謝謝,現在我可以專注於這個問題:) –

1

你忘了2「。 。「(點)

替換:

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

有:

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

而且,你有2 android:labelapplication標籤

+0

感謝您的快速回答,我馬上試過,但不幸的是它不會改變任何東西。 –

+0

確保您的所有包和活動路徑和名稱都正確。 – slybloty

+0

我相信他們是正確的,因爲當我將擴展ListActivity更改爲擴展Activity時,他發現該類。 –

相關問題