2013-10-17 109 views
0

我正在創建Android聯繫人管理器,這是我的MainActvity類。當我運行該應用程序時,它會顯示一條錯誤消息,甚至不顯示該屏幕。這裏是我的MainActivity.java代碼。想知道問題是什麼。Android聯繫人管理器不顯示

import java.util.ArrayList; 
import java.util.HashMap; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 

import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

/* 
* This is the Main Activity and the Home screen to the application. This screen will display 
* the list of all the contacts. Once various buttons/icons are pressed, different screens/activities 
* will be displayed. 
*/ 
public class MainActivity extends ListActivity { 

    // Initializing the listView and the contactList. The contactList is made public so that it is 
    // accessible from any class. 

    private TextView contactId; 


    //The object that allows me to manipulate the Database 
    DBTools dbTools = new DBTools(this); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ArrayList<HashMap<String, String>> listOfContacts = dbTools.getAllContacts(); 



     // Checking to make sure that there are contacts to display. 
     // Then setting up the view of the Home Screen 
     if(listOfContacts.size()!=0){ 
      setupListView(listOfContacts); 
     } 

    } 

    private void setupListView(ArrayList<HashMap<String, String>> listOfContacts){ 

     ListView listView = getListView(); 

     listView.setOnItemClickListener(new OnItemClickListener(){ 

      // Once any contact is clicked, the details are passed as extra information for 
      // the ViewContactsDetails class (using a Bundle). The application is then taken to 
      // the ViewContactDetails screen. 
      @Override 
      public void onItemClick(AdapterView<?> parentView, View clickedView, int clickedViewPosition, long id){ 

       //When an item is clicked get the TextView with matching checkId 

       contactId = (TextView) clickedView.findViewById(R.id.contactId); 

       //Convert that contactId into a String 

       String contactIdValue = contactId.getText().toString(); 

       // Intention to go to the ViewContactDetails class/screen 

       Intent theIntent = new Intent(getApplication(), ViewContactDetails.class); 
       // Put additional data in for EditContact to use 

       theIntent.putExtra("contactId", contactIdValue); 

       // calls for ViewContactDetails 

       startActivity(theIntent); 

      } 

     }); 

     ListAdapter adapter = new SimpleAdapter(MainActivity.this, listOfContacts, R.layout.contact_entry, new String[] {"contactId", "lastName", "firstName"}, new int[] {R.id.contactId, R.id.lastName, R.id.firstName}); 

     setListAdapter(adapter); 


    } 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 


    public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()) { 
     // Once the add contact button is pressed, the application is taken to the AddContact screen. 
     case R.id.addImageButton: 
      Intent intent = new Intent(this, AddContact.class); 
      this.startActivity(intent); 
      break; 

     default: 
      return super.onOptionsItemSelected(item); 
     } 

     return true; 
    } 
} 

在logcat中的錯誤是:

10-17 14:55:01.873: D/dalvikvm(1310): GC_FOR_ALLOC freed 69K, 8% free 2779K/3008K, paused 35ms, total 39ms 
10-17 14:55:01.883: I/dalvikvm-heap(1310): Grow heap (frag case) to 3.939MB for 1127536-byte allocation 
10-17 14:55:01.933: D/dalvikvm(1310): GC_FOR_ALLOC freed 2K, 6% free 3877K/4112K, paused 46ms, total 46ms 
10-17 14:55:02.023: D/AndroidRuntime(1310): Shutting down VM 
10-17 14:55:02.033: W/dalvikvm(1310): threadid=1: thread exiting with uncaught exception (group=0x41465700) 
10-17 14:55:02.043: E/AndroidRuntime(1310): FATAL EXCEPTION: main 
10-17 14:55:02.043: E/AndroidRuntime(1310): java.lang.RuntimeException: Unable to start activity ComponentInfo{dgop507.softeng206.contactsmanagerapplication/dgop507.softeng206.contactsmanagerapplication.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.os.Looper.loop(Looper.java:137) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at java.lang.reflect.Method.invoke(Method.java:525) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at dalvik.system.NativeStart.main(Native Method) 
10-17 14:55:02.043: E/AndroidRuntime(1310): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.app.ListActivity.onContentChanged(ListActivity.java:243) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.app.Activity.setContentView(Activity.java:1895) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at dgop507.softeng206.contactsmanagerapplication.MainActivity.onCreate(MainActivity.java:39) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.app.Activity.performCreate(Activity.java:5133) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
10-17 14:55:02.043: E/AndroidRuntime(1310):  ... 11 more 
+0

我們想知道錯誤信息是什麼。當你運行模擬器時,你需要使用 – ramaral

+0

。它只是說應用程序遇到問題(並關閉),而不顯示主屏幕 – Dheeraj

+0

如果您使用的是Eclipse,則錯誤在LogCat中。發表它。 – ramaral

回答

0

您的問題是:

您的內容必須有一個ListView其id屬性爲 'android.R.id.list'

要使用ListView listView = getListView();,你必須在你的佈局ListView瓦特ith此屬性:android:id="@android:id/list"