我是初学者。我有从编辑视图中获取文本字符串的问题。请帮我修理一下。非常感谢。 CommentActivity.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)
答案 0 :(得分:5)
刚刚放
adapter.add(String.valueOf(cmt.getText());
答案 1 :(得分:0)
String content = cmt.getText().toString();
cmt.setText("");
adapter.add(content);
答案 2 :(得分:0)
您可以尝试如下:
adapter.add(content.toString());
答案 3 :(得分:0)
试试这个:
StringBuilder sb = new StringBuilder(content.length());
sb.append(content);
adapter.add(sb.toString());
答案 4 :(得分:-1)
应该是这样的,
String content = cmt.getText().toString();
adapter.add(content);