无法显示在聊天的朋友的名字 [英] unable to show friends' name on chat
本文介绍了无法显示在聊天的朋友的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有问题,表现出对我的聊天的朋友的名字。该应用程序强制关闭每次运行显示朋友的名字。如果我评论了一部分朋友的名字,该消息可能会出现罚款。但是当我添加标签的名称,它得到了错误并强制关闭。我真的需要帮助,请人帮我。谢谢
json的一部分
如果(!content.equals(空)){
尝试{
JSONArray jArr =新JSONArray(内容);
//字符串消息=;
的for(int i = 0; I< jArr.length();我++){
的JSONObject jObj = jArr.getJSONObject(ⅰ);
字符串名称= jObj.getString(名字);
字符串消息= jObj.getString(信息);
friendLabel.setText(名称);
showMessage(消息,FALSE);
}
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
其他{
Toast.makeText(ChatRoom.this,错误,Toast.LENGTH_LONG).show();
}
全code
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.chatpage);
messagesContainer =(ViewGroup中)findViewById(R.id.messagesContainer);
scrollContainer =(滚动型)findViewById(R.id.scrollContainer);
按钮sendMessageButton =(按钮)findViewById(R.id.sendButton);
束束= this.getIntent()getExtras()。
/ *最后弦乐paramnama = bundle.getString(非农产品市场准入); * /
=的MessageText(的EditText)findViewById(R.id.messageEdit);
meLabel =(TextView中)findViewById(R.id.meLabel);
friendLabel =(TextView中)findViewById(R.id.friendLabel);
meLabel.setText(我);
最后的字符串参数1 = bundle.getString(keyCourseId);
最后的字符串参数2 = bundle.getString(keyUserId);
最后的字符串参数3 = bundle.getString(keyChatsId);
串param4 = bundle.getString(keyMessagesId);
sendMessageButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
//用于发送一部分已经工作正常
});
HttpURLConnection的连接;
网址URL = NULL;
尝试{
linkurl =新Koneksi(本);
SERVER_URL = linkurl.getUrl();
SERVER_URL + =/mobile/ChatRoom.php?idu="+param2+"&idch="+param3;
URL =新的URL(SERVER_URL);
连接=(HttpURLConnection类)url.openConnection();
connection.setDoOutput(真正的);
connection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
connection.setRequestMethod(POST);
新ReceivedTask()执行()。
}
赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
公共类ReceivedTask扩展的AsyncTask<字符串,字符串,字符串> {
@覆盖
在preExecute保护无效(){
}
@覆盖
保护字符串doInBackground(字符串...为arg0){
// TODO自动生成方法存根
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httpPost =新HttpPost(SERVER_URL);
ArrayList的<的NameValuePair>参数=新的ArrayList<的NameValuePair>();
//添加参数
httpPost.setEntity(新UrlEn codedFormEntity(参数));
HTT presponse HTT prespose = httpClient.execute(httpPost);
HttpEntity httpEntity = HTT prespose.getEntity();
//读取内容
InputStream的时间= httpEntity.getContent();
的BufferedReader读=新的BufferedReader(新的InputStreamReader(在));
字符串的内容=;
串线=;
而((行= read.readLine())!= NULL){
内容+ =行;
}
Log.d(ADBUG,内容:+内容);
// JSON
如果(!content.equals(空)){
尝试{
JSONArray jArr =新JSONArray(内容);
//字符串消息=;
的for(int i = 0; I< jArr.length();我++){
的JSONObject jObj = jArr.getJSONObject(ⅰ);
字符串消息= jObj.getString(信息);
showMessage(消息,FALSE);
}
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
其他{
Toast.makeText(ChatRoom.this,错误,Toast.LENGTH_LONG).show();
}
}赶上(ClientProtocolException E){
// TODO自动生成的catch块
e.printStackTrace();}
赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
返回null;
}
}
公共无效showMessage(字符串消息,布尔leftSide){
最后的TextView TextView的=新的TextView(ChatRoom.this);
textView.setTextColor(Color.BLACK);
textView.setText(消息);
INT bgRes = R.drawable.left_message_bg;
LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
如果(!leftSide){
bgRes = R.drawable.right_message_bg;
params.gravity = Gravity.RIGHT;
}
textView.setLayoutParams(PARAMS);
textView.setBackgroundResource(bgRes);
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
messagesContainer.addView(TextView的);
//滚动到底
如果(scrollContainer.getChildAt(0)!= NULL){
scrollContainer.scrollTo(scrollContainer.getScrollX(),scrollContainer.getChildAt(0).getHeight());
}
scrollContainer.fullScroll(View.FOCUS_DOWN);
}
});
}
LogCat中
06-22 17:16:06.688:D / ADBUG(698):内容: [{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"testing聊天},
06-22 17:16:06.787:W / dalvikvm(698):主题ID = 11:线程退出与未捕获的异常(组= 0x40015560)
06-22 17:16:06.797:E / AndroidRuntime(698):致命异常:AsyncTask的#2
06-22 17:16:06.797:E / AndroidRuntime(698):java.lang.RuntimeException的:一个错误而执行doInBackground发生()
06-22 17:16:06.797:E / AndroidRuntime(698):在android.os.AsyncTask $ 3.done(AsyncTask.java:200)
06-22 17:16:06.797:E / AndroidRuntime(698):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:274)
06-22 17:16:06.797:E / AndroidRuntime(698):在java.util.concurrent.FutureTask.setException(FutureTask.java:125)
06-22 17:16:06.797:E / AndroidRuntime(698):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:308)
06-22 17:16:06.797:E / AndroidRuntime(698):在java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-22 17:16:06.797:E / AndroidRuntime(698):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-22 17:16:06.797:E / AndroidRuntime(698):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:581)
06-22 17:16:06.797:E / AndroidRuntime(698):在java.lang.Thread.run(Thread.java:1019)
06-22 17:16:06.797:E / AndroidRuntime(698):android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原始线程所致。
06-22 17:16:06.797:E / AndroidRuntime(698):在android.view.ViewRoot.checkThread(ViewRoot.java:2932)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.view.ViewRoot.requestLayout(ViewRoot.java:629)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.widget.ScrollView.requestLayout(ScrollView.java:1291)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.widget.TextView.checkForRelayout(TextView.java:5521)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.widget.TextView.setText(TextView.java:2724)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.widget.TextView.setText(TextView.java:2592)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.widget.TextView.setText(TextView.java:2567)
06-22 17:16:06.797:E / AndroidRuntime(698):在mobile.chat.ChatRoom $ ReceivedTask.doInBackground(ChatRoom.java:199)
06-22 17:16:06.797:E / AndroidRuntime(698):在mobile.chat.ChatRoom $ ReceivedTask.doInBackground(ChatRoom.java:1)
06-22 17:16:06.797:E / AndroidRuntime(698):在android.os.AsyncTask $ 2.call(AsyncTask.java:185)
06-22 17:16:06.797:E / AndroidRuntime(698):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:306)
06-22 17:16:06.797:E / AndroidRuntime(698):4 ...更多
解决方案
会使人的名字变为动态
公共无效showPerson(字符串friendname,布尔leftSide){
最后的TextView TextView的=新的TextView(ChatRoom.this);
textView.setTextColor(Color.BLACK);
textView.setTextSize(12);
textView.setText(friendname);
INT bgName = R.id.meLabel;
LinearLayout.LayoutParams PARAMS =新LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
如果(!leftSide){
bgName = R.id.friendLabel;
params.gravity = Gravity.RIGHT;
}
textView.setLayoutParams(PARAMS);
textView.setId(bgName);
runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
messagesContainer.addView(TextView的);
//滚动到底
如果(scrollContainer.getChildAt(0)!= NULL){
scrollContainer.scrollTo(scrollContainer.getScrollX(),scrollContainer.getChildAt(0).getHeight());
}
scrollContainer.fullScroll(View.FOCUS_DOWN);
}
});
}
所以JSON一部分变成了这个样子
如果(!content.equals(空)){
尝试{
JSONArray jArr =新JSONArray(内容);
//字符串消息=;
的for(int i = 0; I< jArr.length();我++){
的JSONObject jObj = jArr.getJSONObject(ⅰ);
字符串名称= jObj.getString(名字);
字符串消息= jObj.getString(信息);
showPerson(姓名,假);
showMessage(消息,FALSE);
}
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
I have problem to show the friend name on my chat. the apps force close every time run to show the friend's name. if i comment the part for friend's name, the messages can appear fine. but when i add the label for name, it got error and force close. i really need help, anyone please help me. thank you
json part
if(!content.equals("null")){
try{
JSONArray jArr = new JSONArray(content);
// String messages="";
for(int i=0; i < jArr.length() ; i++){
JSONObject jObj = jArr.getJSONObject(i);
String name = jObj.getString("firstname");
String message = jObj.getString("message");
friendLabel.setText(name);
showMessage(message, false);
}
}catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
Toast.makeText(ChatRoom.this, "Error", Toast.LENGTH_LONG).show();
}
whole code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chatpage);
messagesContainer = (ViewGroup) findViewById(R.id.messagesContainer);
scrollContainer = (ScrollView) findViewById(R.id.scrollContainer);
Button sendMessageButton = (Button) findViewById(R.id.sendButton);
Bundle bundle = this.getIntent().getExtras();
/*final String paramnama = bundle.getString("nama");*/
messageText = (EditText) findViewById(R.id.messageEdit);
meLabel = (TextView) findViewById(R.id.meLabel);
friendLabel = (TextView) findViewById(R.id.friendLabel);
meLabel.setText("me");
final String param1 = bundle.getString("keyCourseId");
final String param2 = bundle.getString("keyUserId");
final String param3 = bundle.getString("keyChatsId");
String param4 = bundle.getString("keyMessagesId");
sendMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//for send part already works fine
});
HttpURLConnection connection;
URL url = null;
try{
linkurl = new Koneksi(this);
SERVER_URL = linkurl.getUrl();
SERVER_URL += "/mobile/ChatRoom.php?idu="+param2+"&idch="+param3;
url = new URL(SERVER_URL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
new ReceivedTask().execute();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class ReceivedTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(SERVER_URL);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
//add parameter
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpRespose = httpClient.execute(httpPost);
HttpEntity httpEntity = httpRespose.getEntity();
//read content
InputStream in = httpEntity.getContent();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
String content = "";
String line = "";
while((line = read.readLine())!=null){
content += line;
}
Log.d("ADBUG", "content: "+content);
//json
if(!content.equals("null")){
try{
JSONArray jArr = new JSONArray(content);
// String messages="";
for(int i=0; i < jArr.length() ; i++){
JSONObject jObj = jArr.getJSONObject(i);
String message = jObj.getString("message");
showMessage(message, false);
}
}catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
Toast.makeText(ChatRoom.this, "Error", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
public void showMessage(String message, boolean leftSide) {
final TextView textView = new TextView(ChatRoom.this);
textView.setTextColor(Color.BLACK);
textView.setText(message);
int bgRes = R.drawable.left_message_bg;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
if (!leftSide) {
bgRes = R.drawable.right_message_bg;
params.gravity = Gravity.RIGHT;
}
textView.setLayoutParams(params);
textView.setBackgroundResource(bgRes);
runOnUiThread(new Runnable() {
@Override
public void run() {
messagesContainer.addView(textView);
// Scroll to bottom
if (scrollContainer.getChildAt(0) != null) {
scrollContainer.scrollTo(scrollContainer.getScrollX(), scrollContainer.getChildAt(0).getHeight());
}
scrollContainer.fullScroll(View.FOCUS_DOWN);
}
});
}
LogCat
06-22 17:16:06.688: D/ADBUG(698): content: [{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"testing chat"},
06-22 17:16:06.787: W/dalvikvm(698): threadid=11: thread exiting with uncaught exception (group=0x40015560)
06-22 17:16:06.797: E/AndroidRuntime(698): FATAL EXCEPTION: AsyncTask #2
06-22 17:16:06.797: E/AndroidRuntime(698): java.lang.RuntimeException: An error occured while executing doInBackground()
06-22 17:16:06.797: E/AndroidRuntime(698): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-22 17:16:06.797: E/AndroidRuntime(698): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
06-22 17:16:06.797: E/AndroidRuntime(698): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
06-22 17:16:06.797: E/AndroidRuntime(698): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
06-22 17:16:06.797: E/AndroidRuntime(698): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-22 17:16:06.797: E/AndroidRuntime(698): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-22 17:16:06.797: E/AndroidRuntime(698): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-22 17:16:06.797: E/AndroidRuntime(698): at java.lang.Thread.run(Thread.java:1019)
06-22 17:16:06.797: E/AndroidRuntime(698): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-22 17:16:06.797: E/AndroidRuntime(698): at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:257)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.widget.ScrollView.requestLayout(ScrollView.java:1291)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.view.View.requestLayout(View.java:8267)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.widget.TextView.checkForRelayout(TextView.java:5521)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.widget.TextView.setText(TextView.java:2724)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.widget.TextView.setText(TextView.java:2592)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.widget.TextView.setText(TextView.java:2567)
06-22 17:16:06.797: E/AndroidRuntime(698): at mobile.chat.ChatRoom$ReceivedTask.doInBackground(ChatRoom.java:199)
06-22 17:16:06.797: E/AndroidRuntime(698): at mobile.chat.ChatRoom$ReceivedTask.doInBackground(ChatRoom.java:1)
06-22 17:16:06.797: E/AndroidRuntime(698): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-22 17:16:06.797: E/AndroidRuntime(698): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-22 17:16:06.797: E/AndroidRuntime(698): ... 4 more
解决方案
make the person name become dynamic
public void showPerson(String friendname, boolean leftSide) {
final TextView textView = new TextView(ChatRoom.this);
textView.setTextColor(Color.BLACK);
textView.setTextSize(12);
textView.setText(friendname);
int bgName = R.id.meLabel;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
if (!leftSide) {
bgName = R.id.friendLabel;
params.gravity = Gravity.RIGHT;
}
textView.setLayoutParams(params);
textView.setId(bgName);
runOnUiThread(new Runnable() {
@Override
public void run() {
messagesContainer.addView(textView);
// Scroll to bottom
if (scrollContainer.getChildAt(0) != null) {
scrollContainer.scrollTo(scrollContainer.getScrollX(), scrollContainer.getChildAt(0).getHeight());
}
scrollContainer.fullScroll(View.FOCUS_DOWN);
}
});
}
so the json part become like this
if(!content.equals("null")){
try{
JSONArray jArr = new JSONArray(content);
// String messages="";
for(int i=0; i < jArr.length() ; i++){
JSONObject jObj = jArr.getJSONObject(i);
String name = jObj.getString("firstname");
String message = jObj.getString("message");
showPerson(name, false);
showMessage(message, false);
}
}catch(JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这篇关于无法显示在聊天的朋友的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文