gpt4 book ai didi

android - 从 edittext android 获取字符串是错误的

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

我是初学者 android。我在从编辑 View 中获取文本字符串时遇到问题。请帮我解决它。非常感谢。评论 Activity .java

public class CommentActivity extends Fragment  {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
TextView cmt;
ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.comment_layout, container, false);
Button send = (Button) v.findViewById(R.id.send_comment);
cmt = (TextView) v.findViewById(R.id.text_comment);
ListView lst = (ListView) v.findViewById(R.id.list_comments);
String[] values = new String[] {};
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, values);
lst.setAdapter(adapter);
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
CharSequence content = cmt.getText();
cmt.setText("");
adapter.add((String) content);
adapter.notifyDataSetChanged();
Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT).show();
}
});
return v;
}
}

它的错误:

12-11 16:39:10.244: E/PlayerDetailActivity(25813): Uncaught exception
12-11 16:39:10.244: E/PlayerDetailActivity(25813): java.lang.ClassCastException: android.text.SpannableStringBuilder
cannot be cast to java.lang.String
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at RanhTV.Controller.CommentActivity$1.onClick(CommentActivity.java:36)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at android.view.View.performClick(View.java:4424)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at android.view.View$PerformClick.run(View.java:18383)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at android.os.Handler.handleCallback(Handler.java:733)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at android.os.Handler.dispatchMessage(Handler.java:95)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at android.os.Looper.loop(Looper.java:137)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at java.lang.reflect.Method.invoke(Method.java:515)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-11 16:39:10.244: E/PlayerDetailActivity(25813): at dalvik.system.NativeStart.main(Native Method)

最佳答案

随便放

adapter.add(String.valueOf(cmt.getText());

关于android - 从 edittext android 获取字符串是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20515761/

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