我正在创建一个Android联系人管理器,这是我的MainActvity类。当我运行应用程序时,它会出现一条错误消息,甚至没有显示屏幕。这是我的MainActivity.java代码。想知道问题是什么。
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 :(得分:0)
你的问题是:
您的内容必须包含一个ListView,其id属性为'android.R.id.list'
要使用ListView listView = getListView();
,您的布局中必须使用此属性ListView
:android:id="@android:id/list"