1 package com.example.listview_1; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.database.Cursor; 5 import android.graphics.Color; 6 import android.os.Bundle; 7 import android.provider.ContactsContract; 8 import android.provider.ContactsContract.PhoneLookup; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.View; 12 import android.widget.AdapterView; 13 import android.widget.LinearLayout; 14 import android.widget.ListAdapter; 15 import android.widget.ListView; 16 import android.widget.SimpleCursorAdapter; 17 import android.widget.Toast; 18 19 public class MainActivity extends ActionBarActivity { 20 21 LinearLayout M_LL; 22 ListView M_LV; 23 @SuppressWarnings("deprecation") 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 //setContentView(R.layout.activity_main); 28 29 //创建linearlayout布局对象 30 M_LL = new LinearLayout(this); 31 32 //设置LinearLayout的布局属性 33 M_LL.setOrientation(LinearLayout.VERTICAL); 34 M_LL.setBackgroundColor(android.graphics.Color.BLACK); 35 36 //创建ListView对象 37 M_LV = new ListView(this); 38 LinearLayout.LayoutParams LP = 39 new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); 40 M_LV.setBackgroundColor(Color.BLACK); 41 42 //添加M_LV到M_LL布局 43 M_LL.addView(M_LV, LP); 44 45 //设置显示M_LL布局 46 this.setContentView(M_LL); 47 48 //获取数据库Phones的Cursor 49 Cursor cur = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 50 this.startManagingCursor(cur); 51 52 //ListAdapter是ListView和后台的数据桥梁 53 ListAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cur,new String[]{ 54 PhoneLookup.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER},new int[]{android.R.id.text1,android.R.id.text2}); 55 //定义list中的每一行显示模板 56 //表示每一行包含两个数据项 57 //数据库Cursor对象 58 //从数据库的name和number两列中取得数据 59 //与name和number对应的Views 60 //将adapter添加到M_LV中 61 M_LV.setAdapter(adapter); 62 63 //为M_LV视图添加setOnItimSelectedListener监听 64 M_LV.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 65 66 @Override 67 public void onItemSelected(AdapterView<?> arg0,View arg1,int arg2,long arg3) 68 { 69 DisplayToast("滚动到第"+Long.toString(arg0.getSelectedItemId())+"项"); 70 } 71 @Override 72 public void onNothingSelected(AdapterView<?> arg0) 73 { 74 //没有选中 75 } 76 }); 77 //为M_LV视图添加setOnItemClickListener监听 78 M_LV.setOnItemClickListener(new AdapterView.OnItemClickListener() { 79 @Override 80 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 81 { 82 //对选中的项进行处理 83 DisplayToast("选中了第"+Integer.toString(position+1)+"项"); 84 } 85 }); 86 } 87 /* 显示Toast */ 88 public void DisplayToast(String str) 89 { 90 Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); 91 } 92 }
1 错误信息 2 3 07-31 03:43:17.938: D/ddm-heap(225): Got feature list request 4 07-31 03:43:18.057: I/dalvikvm(225): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init> 5 07-31 03:43:18.057: W/dalvikvm(225): VFY: unable to resolve virtual method 364: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable; 6 07-31 03:43:18.057: D/dalvikvm(225): VFY: replacing opcode 0x6e at 0x008b 7 07-31 03:43:18.057: D/dalvikvm(225): Making a copy of Landroid/support/v7/internal/widget/ActionBarView;.<init> code (1001 bytes) 8 07-31 03:43:18.066: I/dalvikvm(225): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init> 9 07-31 03:43:18.066: W/dalvikvm(225): VFY: unable to resolve virtual method 360: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable; 10 07-31 03:43:18.066: D/dalvikvm(225): VFY: replacing opcode 0x6e at 0x0099 11 07-31 03:43:18.126: I/dalvikvm(225): Could not find method android.view.ViewGroup.onConfigurationChanged, referenced from method android.support.v7.internal.widget.AbsActionBarView.onConfigurationChanged 12 07-31 03:43:18.126: W/dalvikvm(225): VFY: unable to resolve virtual method 8729: Landroid/view/ViewGroup;.onConfigurationChanged (Landroid/content/res/Configuration;)V 13 07-31 03:43:18.126: D/dalvikvm(225): VFY: replacing opcode 0x6f at 0x0007 14 07-31 03:43:18.126: D/dalvikvm(225): Making a copy of Landroid/support/v7/internal/widget/AbsActionBarView;.onConfigurationChanged code (148 bytes) 15 07-31 03:43:18.146: I/dalvikvm(225): Could not find method android.widget.FrameLayout.onConfigurationChanged, referenced from method android.support.v7.internal.widget.ActionBarView$HomeView.onConfigurationChanged 16 07-31 03:43:18.146: W/dalvikvm(225): VFY: unable to resolve virtual method 9031: Landroid/widget/FrameLayout;.onConfigurationChanged (Landroid/content/res/Configuration;)V 17 07-31 03:43:18.146: D/dalvikvm(225): VFY: replacing opcode 0x6f at 0x0000 18 07-31 03:43:18.146: D/dalvikvm(225): Making a copy of Landroid/support/v7/internal/widget/ActionBarView$HomeView;.onConfigurationChanged code (44 bytes) 19 07-31 03:43:18.227: D/AndroidRuntime(225): Shutting down VM 20 07-31 03:43:18.227: W/dalvikvm(225): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 21 07-31 03:43:18.227: E/AndroidRuntime(225): Uncaught handler: thread main exiting due to uncaught exception 22 07-31 03:43:18.236: E/AndroidRuntime(225): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listview_1/com.example.listview_1.MainActivity}: java.lang.IllegalArgumentException: column 'data1' does not exist 23 07-31 03:43:18.236: E/AndroidRuntime(225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 24 07-31 03:43:18.236: E/AndroidRuntime(225): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 25 07-31 03:43:18.236: E/AndroidRuntime(225): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 26 07-31 03:43:18.236: E/AndroidRuntime(225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 27 07-31 03:43:18.236: E/AndroidRuntime(225): at android.os.Handler.dispatchMessage(Handler.java:99) 28 07-31 03:43:18.236: E/AndroidRuntime(225): at android.os.Looper.loop(Looper.java:123) 29 07-31 03:43:18.236: E/AndroidRuntime(225): at android.app.ActivityThread.main(ActivityThread.java:4363) 30 07-31 03:43:18.236: E/AndroidRuntime(225): at java.lang.reflect.Method.invokeNative(Native Method) 31 07-31 03:43:18.236: E/AndroidRuntime(225): at java.lang.reflect.Method.invoke(Method.java:521) 32 07-31 03:43:18.236: E/AndroidRuntime(225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 33 07-31 03:43:18.236: E/AndroidRuntime(225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 34 07-31 03:43:18.236: E/AndroidRuntime(225): at dalvik.system.NativeStart.main(Native Method) 35 07-31 03:43:18.236: E/AndroidRuntime(225): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist 36 07-31 03:43:18.236: E/AndroidRuntime(225): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314) 37 07-31 03:43:18.236: E/AndroidRuntime(225): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99) 38 07-31 03:43:18.236: E/AndroidRuntime(225): at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:339) 39 07-31 03:43:18.236: E/AndroidRuntime(225): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:91) 40 07-31 03:43:18.236: E/AndroidRuntime(225): at com.example.listview_1.MainActivity.onCreate(MainActivity.java:56) 41 07-31 03:43:18.236: E/AndroidRuntime(225): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 42 07-31 03:43:18.236: E/AndroidRuntime(225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 43 07-31 03:43:18.236: E/AndroidRuntime(225): ... 11 more 44 07-31 03:43:18.256: I/dalvikvm(225): threadid=7: reacting to signal 3 45 07-31 03:43:18.256: E/dalvikvm(225): Unable to open stack trace file '/data/anr/traces.txt': Permission denied 46 07-31 03:48:18.270: I/Process(225): Sending signal. PID: 225 SIG: 9
错误提示:意外停止,请重试
java.lang.IllegalArgumentException: column 'data1' does not exist
这么明显的错误提示啊
@abu7252:
自己调试下,其实主要是Cursor cur = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 这查询到的内容和下面要用的不一样。
用这个 Cursor c = getContentResolver().query(Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI, "*"), new String[] {
PhoneLookup._ID,
PhoneLookup.NUMBER,
PhoneLookup.DISPLAY_NAME,
PhoneLookup.TYPE, PhoneLookup.LABEL }, null, null, sortOrder);