0

我创建了一个 Android 应用程序,它创建了一个包含所有联系人电子邮件的列表,当您单击联系人时,将电子邮件发送给他。

问题是当应用程序必须接收联系人列表时,在我的手机上崩溃,但在 eclipse 的 ADV 上完美运行。

主类:

package com.example.mail;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

public static final int R_CODE_CONTACT_LIST =10;

Intent intent;
EditText txtTo,txtSubject,txtMsg;
ImageButton btnSend,btnContacts;    
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    txtTo=(EditText)findViewById(R.id.txtto);
    txtSubject=(EditText)findViewById(R.id.txtsubject);
    txtMsg=(EditText)findViewById(R.id.txtmessage);
    btnSend=(ImageButton)findViewById(R.id.btnsend);
    btnContacts=(ImageButton)findViewById(R.id.btncontacts);

    btnSend.setOnClickListener(this);
    btnContacts.setOnClickListener(this);
}


@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;
}

@Override
public void onClick(View view) {

    switch (view.getId()){

    case R.id.btnsend:
        if (allTextWritten()){
            Intent intent = new Intent(Intent.ACTION_SENDTO); // it's not ACTION_SEND
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_SUBJECT, txtSubject.getText().toString());
            intent.putExtra(Intent.EXTRA_TEXT, txtMsg.getText().toString());
            intent.setData(Uri.parse("mailto:"+txtTo.getText().toString())); // or just "mailto:" for blank
            //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
            startActivity(intent);

        }
        else{
             Toast.makeText(MainActivity.this, "Please, fill all fields.", Toast.LENGTH_SHORT).show();
        }

        break;
    case R.id.btncontacts:
        intent =new Intent(MainActivity.this,ContactsList.class);
        startActivityForResult(intent,R_CODE_CONTACT_LIST);
        break;
    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    try{
        Log.d("request",""+requestCode);
        Log.d("result",""+resultCode);
        if (requestCode==R_CODE_CONTACT_LIST)
          txtTo.setText(data.getStringExtra("email"));
        else
          txtTo.setText("");
    }
    catch (Exception e){
        //Toast.makeText(this, "list closed",Toast.LENGTH_SHORT).show();  //Control line
    }

}



private boolean allTextWritten(){
    boolean isWritten=true;
    //if all text boxes are filled returns true 
    return isWritten & !("".equals(txtTo.getText().toString()))
                     &     !("".equals(txtSubject.getText().toString()))
                     & !("".equals(txtMsg.getText().toString()));       
}

}

列表类:

package com.example.mail;

import java.util.ArrayList;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class ContactsList extends ListActivity {
private final int   REQUEST_CODE=10;
//private final int     FAIL_CODE=-1;
private boolean destroy=true;

protected ArrayList<String> contactList =new ArrayList<String>();
protected ArrayList<String> mail =new ArrayList<String>();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_lista);
    getContactEmails(this);

    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, contactList));
}

public void onPause(){
    super.onPause();
    if (destroy){
        Log.d("destroy",""+destroy);
        Intent intent=new Intent();
        intent.putExtra("email","");
        setResult(REQUEST_CODE,intent);
        destroy=false;
    }
}

public void onListItemClick(ListView parent, View v, int position, long id){
    Intent intent=new Intent();
    intent.putExtra("email",mail.get(position));
    setResult(REQUEST_CODE,intent);
    destroy=false;
    finish();
   }

private void getContactEmails(Context context) {
    String emailIdOfContact = null;
    int emailType = Email.TYPE_WORK;
    String contactName = null;

        ContentResolver cr = context.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);

        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                int newMail=1;
                String id = cur.getString(cur.getColumnIndex(BaseColumns._ID));
                contactName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                // Log.d("mail","....contact name....." +contactName);
                cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,  ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[] { id }, null);

                Cursor emails = cr.query(Email.CONTENT_URI, null,Email.CONTACT_ID + " = " + id, null, null);
                while (emails.moveToNext()) {
                    String multiMail="";
                    emailIdOfContact = emails.getString(emails.getColumnIndex(Email.DATA));
                    if (newMail!=1)
                        multiMail=""+newMail;
                    contactList.add(contactName+multiMail+" - "+emailIdOfContact.substring(0,10)+"...");
                    mail.add(emailIdOfContact);
                    // Log.d("mail","...COntact Name ...." + contactName + "...contact Number..."+ emailIdOfContact);
                    emailType = emails.getInt(emails.getColumnIndex(Phone.TYPE));
                    newMail++;

                }
                emails.close();

            }
        }// end of contact name cursor
        cur.close();
}

}

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mail"
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.mail.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>
    <activity 
        android:name="com.example.mail.ContactsList"
        android:theme="@style/GeneralBackgroudStyle"/>
</application>

有人有什么想法吗?谢谢大家。


编辑Logcat 异常:

04-09 09:47:06.340: E/AndroidRuntime(18915): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mail/com.example.mail.ContactsList}: java.lang.StringIndexOutOfBoundsException
04-09 09:47:06.340: E/AndroidRuntime(18915):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at android.os.Looper.loop(Looper.java:130)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at android.app.ActivityThread.main(ActivityThread.java:3691)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at java.lang.reflect.Method.invokeNative(Native Method)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at java.lang.reflect.Method.invoke(Method.java:507)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at dalvik.system.NativeStart.main(Native Method)
04-09 09:47:06.340: E/AndroidRuntime(18915): Caused by: java.lang.StringIndexOutOfBoundsException
04-09 09:47:06.340: E/AndroidRuntime(18915):    at java.lang.String.substring(String.java:1651)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at com.example.mail.ContactsList.getContactEmails(ContactsList.java:82)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at com.example.mail.ContactsList.onCreate(ContactsList.java:34)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
04-09 09:47:06.340: E/AndroidRuntime(18915):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-09 09:47:06.340: E/AndroidRuntime(18915):    ... 11 more
4

2 回答 2

0
contactList.add(contactName+multiMail+" - "+emailIdOfContact.substring(0,10)+"...");

异常发生在 substring() 调用上。由于您对这些值进行了硬编码,因此我假设它emailIdOfContact少于 10 个字符,因此会出现错误。您真的想在不事先检查的情况下对其进行硬编码吗?您可能在模拟器中有工作字符串,但在您的实际手机上没有。

于 2013-04-09T08:56:12.410 回答
0

有一个类似的问题:

通过 onActivity 结果返回后:

  • 在手机上:ArrayList 数据丢失,ArrayList.size 始终为 0;
  • 模拟器:ArrayList 数据存在且正常

将数组声明为静态同步列表,现在它适用于手机和模拟器。

用这个.....

私有静态列表 array_list_name = Collections.synchronizedList(new ArrayList())

官方文档:这里

于 2014-11-06T12:06:29.990 回答