新手按《Android应用开发揭秘》中的代码,由于版本不同做了一些修改。
可以运行不了,直接报应用异常。请各位高手帮忙看看是什么原因,多谢!package leo.zheng.LayoutUsage;import android.app.Activity;
import android.os.Bundle;
import android.provider.Contacts;import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.database.Cursor;import android.view.View;
import android.util.Log;public class LayoutUsage extends Activity {
LinearLayout linearLayout;
ListView lv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setBackgroundColor(android.graphics.Color.BLACK);
        
        lv = new ListView(this);
        
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
         LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.addView(lv,param);
        setContentView(linearLayout);
        
        Cursor cur = getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);
        startManagingCursor(cur);
        
        ListAdapter adapter = new SimpleCursorAdapter(this,
         android.R.layout.simple_list_item_2,
         cur,
         new String[] { Contacts.People.NAME, Contacts.People.NUMBER},
         new int[] { android.R.id.text1, android.R.id.text2 });
        lv.setAdapter(adapter);
        
        lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
         {
         @Override
         public void onItemSelected(AdapterView<?> arg0,View arg1,int arg2,long arg3)
         {
         Log.v("Adapter", "Item selected");
         }
         public void onNothingSelected(AdapterView<?> arg0)
         {
         }
}
        );
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
         {
     @Override
         public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3)
         {
         }
         }
        );
    }
}

解决方案 »

  1.   

    还不太会用编译环境,试着找下 LS 所说的 logcat
      

  2.   

    这是 logcat 信息,基本上可以找到问题的原因: 
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    02-24 11:10:16.911: DEBUG/AndroidRuntime(749): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
    02-24 11:10:16.921: DEBUG/AndroidRuntime(749): CheckJNI is ON
    02-24 11:10:17.221: DEBUG/AndroidRuntime(749): --- registering native functions ---
    02-24 11:10:18.170: INFO/ActivityManager(567): Starting activity: Intent { flags=0x10000000 comp={leo.zheng.LayoutUsage/leo.zheng.LayoutUsage.LayoutUsage} }
    02-24 11:10:18.180: WARN/ActivityManager(567): Permission Denial: starting Intent { flags=0x10000000 comp={leo.zheng.LayoutUsage/leo.zheng.LayoutUsage.LayoutUsage} } from null (pid=-1, uid=-1) requires android.permission.READ_CONTACTS
    // Permission Denial 是最明示的原因。
    02-24 11:10:18.201: DEBUG/AndroidRuntime(749): Shutting down VM
    02-24 11:10:18.211: WARN/dalvikvm(749): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
    02-24 11:10:18.211: ERROR/AndroidRuntime(749): Uncaught handler: thread main exiting due to uncaught exception
    02-24 11:10:18.221: ERROR/AndroidRuntime(749): *** EXCEPTION IN SYSTEM PROCESS.  System will crash.
    02-24 11:10:18.270: ERROR/AndroidRuntime(749): java.lang.SecurityException: Permission Denial: starting Intent { flags=0x10000000 comp={leo.zheng.LayoutUsage/leo.zheng.LayoutUsage.LayoutUsage} } from null (pid=-1, uid=-1) requires android.permission.READ_CONTACTS
    02-24 11:10:18.270: ERROR/AndroidRuntime(749):     at android.os.Parcel.readException(Parcel.java:1234)
    02-24 11:10:18.270: ERROR/AndroidRuntime(749):     at android.os.Parcel.readException(Parcel.java:1222)
    02-24 11:10:18.270: ERROR/AndroidRuntime(749):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1046)
    02-24 11:10:18.270: ERROR/AndroidRuntime(749):     at com.android.commands.am.Am.runStart(Am.java:199)
    02-24 11:10:18.270: ERROR/AndroidRuntime(749):     at com.android.commands.am.Am.run(Am.java:73)
    02-24 11:10:18.270: ERROR/AndroidRuntime(749):     at com.android.commands.am.Am.main(Am.java:51)
    02-24 11:10:18.270: ERROR/AndroidRuntime(749):     at com.android.internal.os.RuntimeInit.finishInit(Native Method)
    02-24 11:10:18.270: ERROR/AndroidRuntime(749):     at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:186)
    02-24 11:10:18.270: ERROR/AndroidRuntime(749):     at dalvik.system.NativeStart.main(Native Method)
    02-24 11:10:18.291: ERROR/JavaBinder(749): Unknown binder error code. 0xfffffff7
    02-24 11:10:18.291: ERROR/AndroidRuntime(749): Crash logging skipped, no checkin service
    02-24 11:10:18.291: INFO/Process(749): Sending signal. PID: 749 SIG: 9
      

  3.   

    // android.permission.READ_CONTACTS 权限我已经设了啊,但设置的方法不对!
    偶是通过 AndroidManifest.xml 的可视化编辑界面,在 Application 界面的 Permission 编辑框中设定的。
    设定后在 AndroidManifest.xml 中生成如下的东东:
     android:permission="android.permission.READ_CONTACTS"
     自动生成前是:
     <application android:icon="@drawable/icon" android:label="@string/app_name">
     自动生成后是:
     <application android:icon="@drawable/icon" android:label="@string/app_name" android:permission="android.permission.READ_CONTACTS">谁能告诉我这两种有什么区别?
    或者说第一种(错误的哪种)是什么作用?