应用程序无法在模拟器上工作

时间:2013-02-05 06:37:30

标签: android

我是Android应用程序开发的初学者。我正在尝试创建一个从手机访问联系人的应用程序。我的控制台显示应用程序的成功安装,但它没有在模拟器中启动。可以有人请告诉我可能是什么原因?

import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity    { 
ListView lvItem;
private Button btnAdd;
String displayName="", emailAddress="", phoneNumber="";
ArrayList<String> contactlist=new ArrayList<String>();
ArrayAdapter<String> itemAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   lvItem = (ListView)this.findViewById(R.id.listView_items);  
   btnAdd = (Button)this.findViewById(R.id.btnAddItem);
   itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
   lvItem.setAdapter(itemAdapter);
   btnAdd.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
           readContacts();
       }
   });
}

private void readContacts()
{
    ContentResolver cr =getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while (cursor.moveToNext()) 
    {
        displayName="";emailAddress=""; phoneNumber="";
        displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));       
        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
        while (emails.moveToNext()) 
        { 
            emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
            break;
        }
        emails.close();
        if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
        {
            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
            while (pCur.moveToNext()) 
            {
                 phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                break;
            }
            pCur.close();
        }
            contactlist.add("DisplayName: "+displayName+", PhoneNumber: "+phoneNumber+", EmailAddress: "+ emailAddress+"\n");
            itemAdapter.notifyDataSetChanged();
    }
    cursor.close(); 
}

}

的manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.contacts"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk
           android:minSdkVersion="8"
           android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
               android:name="com.example.contacts.MainActivity"
               android:label="@string/app_name" >
               <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
            </activity>
        </application>
</manifest>

3 个答案:

答案 0 :(得分:1)

在实际手机上试用,模拟器上没有任何联系人

答案 1 :(得分:0)

可能的原因是您尚未在清单文件中的活动中添加此代码

                

            <category android:name="android.intent.category.LAUNCHER" />

所以您的应用程序已成功安装但由于上面的代码而无法启动。添加上面的代码并且它可以正常运行...

答案 2 :(得分:0)

仔细检查您的清单文件活动名称和访问联系人的权限。