简体   繁体   中英

ClassCastException when starting E-Mail Intent

I'm trying to send an email using email intent. This is my code:

/** Called when the user clicks the send button */  
public void cont_sendEmail(View view) {
        final EditText nick = (EditText) findViewById(R.id.contNick);
        final EditText feas = (EditText) findViewById(R.id.contFeas);
        final EditText tip = (EditText) findViewById(R.id.contTip);
        String totalNick = nick.getText().toString();
        String totalFeas = feas.getText().toString();
        String totalTip = tip.getText().toString();
        String totalText = totalNick.concat(totalFeas);


    // Do something in response to button
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"sando@live.se"});
    i.putExtra(Intent.EXTRA_SUBJECT, "New contribution!");
    i.putExtra(Intent.EXTRA_TEXT   , totalText);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    try {
       startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
      Toast.makeText(ContributeActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
}

When I press the "send" button and activates cont_sendEmail, the app crashes. This is my logcat:

08-17 14:29:16.445: E/AndroidRuntime(12649): FATAL EXCEPTION: main
08-17 14:29:16.445: E/AndroidRuntime(12649): java.lang.IllegalStateException: Could not execute method of the activity
08-17 14:29:16.445: E/AndroidRuntime(12649):    at android.view.View$1.onClick(View.java:3691)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at android.view.View.performClick(View.java:4211)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at android.view.View$PerformClick.run(View.java:17267)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at android.os.Handler.handleCallback(Handler.java:615)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at android.os.Looper.loop(Looper.java:137)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at java.lang.reflect.Method.invokeNative(Native Method)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at java.lang.reflect.Method.invoke(Method.java:511)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at dalvik.system.NativeStart.main(Native Method)
08-17 14:29:16.445: E/AndroidRuntime(12649): Caused by: java.lang.reflect.InvocationTargetException
08-17 14:29:16.445: E/AndroidRuntime(12649):    at java.lang.reflect.Method.invokeNative(Native Method)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at java.lang.reflect.Method.invoke(Method.java:511)
08-17 14:29:16.445: E/AndroidRuntime(12649):    at android.view.View$1.onClick(View.java:3686)
08-17 14:29:16.445: E/AndroidRuntime(12649):    ... 11 more
08-17 14:29:16.445: E/AndroidRuntime(12649): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-17 14:29:16.445: E/AndroidRuntime(12649):    at com.sandtdevelopment.getrich.ContributeActivity.cont_sendEmail(ContributeActivity.java:18)

(How do I paste the logcat to be readable?)

What can be the problem?

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText

You are obviously trying to cast a TextView to an EditText which cannot work.

I see two possible reasons for that error:

  • Sometimes whean your are doing heavy changes on your layout using the Layout Creator the IDE can get a little confused and mix up things. Please try CLEANING your project .

  • You are actually making the misstake of doing the wrong Cast. In this case, check your code and see if you are accidentially casting a TextView to an EditText (it is possible that you simply mixed up some View IDs ). It could for example be the case that one of your referenced IDs above is not an EditText. Please check "R.id.contNick", "R.id.contFeas" and "R.id.contTip".

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText ... at com.sandtdevelopment.getrich.ContributeActivity.cont_sendEmail(ContributeActivity.java:18)

You're trying to cast a TextView to an Editview on line 18

It seems you are having an error with the casting of your EditText instances at the top of your class. Did you accidentally define one of the three as a TextView in your XML? The ID that you are using to refer to it could perhaps also be wrong. If this is the case and you fix it, your error should go away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM