我是初学者的android。我有问题要从编辑视图中获取文本字符串。请帮我修复它。非常感谢。 CommentActivity.java从edittext获取字符串android是错误
public class CommentActivity extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
TextView cmt;
ArrayAdapter 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(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)
+0
代替铸造用toString方法在数组列表中添加字符串:'adapter.add(content.toString() );' –