androidManifestFile中的ActivityNotFoundException

时间:2016-04-18 12:22:11

标签: java android eclipse

我正在开发一个用户注册帐户的Android应用程序。当用户注册时,应该直接向用户gmail发送电子邮件,不应该有用户交互。请帮帮我。

SignUpActivity.java

public class SignUp extends Activity  {

    EditText uname,email,phone;
    Button createAccount;
    SQLiteDatabase ldh;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_up);

        final LogindatabaseHandler ldh=new LogindatabaseHandler(this);
        //ldh=ldh.open();

        uname=(EditText)findViewById(R.id.editText1);
        email=(EditText)findViewById(R.id.editText2);
        phone=(EditText)findViewById(R.id.editText3);

        createAccount=(Button)findViewById(R.id.button1);

        createAccount.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                 String username=uname.getText().toString();
                 String emailid=email.getText().toString();
                 String phoneno=phone.getText().toString();

                if(username.equals("") || emailid.equals("") || phoneno.equals(""))
                {
                    Toast.makeText(getApplicationContext(),"Information should not be blank", Toast.LENGTH_LONG).show();
                }
                else if(!isValidPhoneno(phoneno) || !isValidEmail(emailid))
                {
                    Toast.makeText(getApplicationContext(), "Invalid input", Toast.LENGTH_LONG).show();
                }
                else
                {
                    OTP otp=new OTP();
                    String otpPass=otp.nextPassword();

                    int check=ldh.insertEntry(new Login(username, emailid,phoneno, otpPass.toString(),false));

                    if(check==1)
                    {
                       Toast.makeText(getApplicationContext(), "Account created successfully",Toast.LENGTH_LONG).show();

                       Intent gmailIntent=new Intent();
                       gmailIntent.setClassName("com.google.android.gm", "com.android.mail.compose.ComposeActivity");
                       gmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailid});
                        gmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "hello");
                        gmailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"hi" );
                       startActivity(gmailIntent);
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "Already registered",Toast.LENGTH_LONG).show();
                        Intent ma=new Intent(SignUp.this,MainActivity.class);
                        startActivity(ma);
                    }
                    finish();
                }               
            }
        });
    }

以下是我用于验证的方法:

    private boolean isValidPhoneno(String phone)
    {
        String PHONE_PATTERN="[7-9]{1}[0-9]{9}";

        Pattern pattern=Pattern.compile(PHONE_PATTERN);
        Matcher matcher=pattern.matcher(phone);
        return matcher.matches();
    }

    private boolean isValidEmail(String email)
    {
        String EMAIL_PATTERN="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }


    protected void onDestroy() 
    {
        super.onDestroy();

        if(ldh!=null)
        {
            ldh.close();
        }
     }


}

我得到以下异常:

 04-18 17:16:06.110: E/AndroidRuntime(21847): FATAL EXCEPTION: main
    04-18 17:16:06.110: E/AndroidRuntime(21847): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.google.android.gm/com.android.mail.compose.ComposeActivity}; have you declared this activity in your AndroidManifest.xml?
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at android.app.Activity.startActivityForResult(Activity.java:2817)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at android.app.Activity.startActivity(Activity.java:2923)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at com.securityapp.SignUp$1.onClick(SignUp.java:98)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at android.view.View.performClick(View.java:2408)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at android.view.View$PerformClick.run(View.java:8816)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at android.os.Handler.handleCallback(Handler.java:587)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at android.os.Handler.dispatchMessage(Handler.java:92)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at android.os.Looper.loop(Looper.java:123)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at android.app.ActivityThread.main(ActivityThread.java:4627)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at java.lang.reflect.Method.invokeNative(Native Method)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at java.lang.reflect.Method.invoke(Method.java:521)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    04-18 17:16:06.110: E/AndroidRuntime(21847):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

你正在做什么

您正在打开一个包含预先填写的地址,主题和消息的电子邮件客户端,并允许用户发送电子邮件,在发送之前对其进行更改或完全取消。

问题

Intent gmailIntent=new Intent();
gmailIntent.setClassName("com.google.android.gm", "com.android.mail.compose.ComposeActivity");

您假设用户已安装Gmail并强制用户使用Gmail撰写电子邮件。您不能依赖于明确命名活动,因为Google可以移动它/重命名它/随心所欲地执行任何操作并且您的应用程序中断。不要这样做。

解决方案

让系统选择一个电子邮件客户端。

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822"); // Limit to email apps.

现在,系统将提示用户从已安装的应用程序中选择一个电子邮件客户端,或者如果用户选择了一个,则启动默认客户端。如果没有电子邮件客户端,应用程序将崩溃。

解决方案第2部分

查看是否有任何应用可以在触发之前处理该意图。

if (getPackageManager().resolveActivity(intent, 0) != null) {
    // There is an email client, start it.
    startActivity(intent);
} else {
    // There are no email clients, show a toast or something.
}

现在,如果没有安装电子邮件客户端,应用程序不会崩溃。

你想做什么

  

电子邮件应该直接使用gmail发送,不应该有用户交互。

您需要使用Gmail API。

请参阅https://developers.google.com/gmail/api/quickstart/androidhttps://developers.google.com/gmail/api/guides/sending#creating_messages

如果您在使用此问题后遇到更多问题,请发布另一个问题。

答案 1 :(得分:0)

请添加try / catch周边

try{
     Intent gmailIntent=new Intent();
                                gmailIntent.setClassName("com.google.android.gm", "com.android.mail.compose.ComposeActivity");

                                gmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailid});
                                gmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "hello");
                                gmailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"hi" );
                                startActivity(gmailIntent);
}catch(ActivityNotFoundException e){
Toast.makeText(this, "google mail app is not installed", Toast.LENGTH_SHORT).show();
}

您正在运行此代码,其中设备不包含Google邮件应用程序。所以最好使用try / catch&在包含google mail app的设备上运行此代码。

答案 2 :(得分:0)

建议不要使用包名作为未记录的方法。如果包名称在某一天发生变化,代码将失败。

尝试使用此代码

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
 "mailto", "abc@gmail.com", null));
 emailIntent.putExtra(Intent.EXTRA_SUBJECT, "This is my subject text");
 context.startActivity(Intent.createChooser(emailIntent, null));

参考:http://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO