gpt4 book ai didi

java - 启动电子邮件 Intent 时出现 ClassCastException

转载 作者:搜寻专家 更新时间:2023-11-01 08:56:54 24 4
gpt4 key购买 nike

我正在尝试使用电子邮件 Intent 发送电子邮件。这是我的代码:

/** 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();
}
}

当我按下“发送”按钮并激活 cont_sendEmail 时,应用程序崩溃了。这是我的日志:

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)

(如何粘贴 logcat 使其可读?)

可能是什么问题?

最佳答案

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

您显然正在尝试将 TextView 转换为无法工作的 EditText。

我看到该错误的两个可能原因:

  • 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".

关于java - 启动电子邮件 Intent 时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18288959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com