我制作了一个Android应用程序,使用所有联系人的电子邮件创建列表,当您点击联系人时,会向他发送电子邮件。
问题在于,当应用程序必须接收联系人列表时,我的手机会崩溃,但在日食的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
答案 0 :(得分:0)
contactList.add(contactName+multiMail+" - "+emailIdOfContact.substring(0,10)+"...");
substring()调用发生异常。由于您对值进行了硬编码,因此我假设emailIdOfContact
短于10个字符,因此错误。你是否真的想在没有预先检查的情况下对其进行硬编码?您可能在模拟器中有工作字符串,但在实际手机上却没有。
答案 1 :(得分:0)
有类似的问题:
通过onActivity Result返回:
将数组声明为静态synchronizedList,现在它可以在手机和模拟器上运行。
使用这个......
private static List array_list_name = Collections.synchronizedList(new ArrayList())
officialDocs:HERE