我为列表视图构建了一个BaseAdapter扩展类。
我有一个TextView字段,我想通过代码设置。
此字段表示用户的Facebook状态。
此字段有时会以希伯来语获取文本,然后由于stackoverflow错误而崩溃。
重要的是,当字段获得的文本是英文时,这不会发生,
当我试图调试它以找到问题时,我在活动中使用了带有希伯来字符串的setText(而不是BaseAdapter)并且它工作正常。
这是我的班级:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.WhosAround.R;
import com.WhosAround.AppVariables;
import com.WhosAround.Facebook.FacebookUser;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.FacebookError;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class FriendsFriendsTabList extends BaseAdapter {
private static LayoutInflater inflater = null;
private AppVariables app;
private final FacebookUser[] chatList;
public FriendsFriendsTabList(Activity activity) {
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
app = (AppVariables) activity.getApplicationContext();
chatList = app.makeApplicationFacebookUsersArray();
}
public int getCount() {
return chatList.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(
R.layout.activity_friends_friends_tab_list_row, null);
if (position % 2 == 0)
vi.setBackgroundResource(R.color.list_background_light);
else
vi.setBackgroundResource(R.color.list_background_dark);
TextView name = (TextView) vi
.findViewById(R.id.list_friends_friends_tab_name);
TextView status = (TextView) vi
.findViewById(R.id.list_friends_friends_tab_status);
ImageView profilePicture = (ImageView) vi
.findViewById(R.id.list_profile_picture);
loadStatus(position, status);
return vi;
}
private void loadStatus(final int position, final TextView status) {
if (chatList[position].getStatus() != null)
status.setText(chatList[position].getStatus());
else {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
try {
String currentStatus = (String) message.obj;
String utf8Status;
utf8Status = new String(currentStatus.getBytes(), "UTF-8");
chatList[position].setStatus(currentStatus);
Log.d("status", chatList[position].getStatus());
//this is the line that causes the error
status.setText(utf8Status);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread() {
@Override
public void run() {
String fqlQuery = "SELECT message FROM status WHERE uid="
+ Integer.toString(chatList[position].getId())
+ " LIMIT 1";
Bundle fqlQueryParams = new Bundle();
fqlQueryParams.putString("method", "fql.query");
fqlQueryParams.putString("query", fqlQuery);
app.getFacebookManager().getFacebookRunner().request(null, fqlQueryParams, new RequestListener() {
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
Log.e("Facebook User Status", e.toString());
}
@Override
public void onIOException(IOException e, Object state) {
Log.e("Facebook User Status", e.toString());
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
Log.e("Facebook User Status", e.toString());
}
@Override
public void onFacebookError(FacebookError e, Object state) {
Log.e("Facebook User Status", e.toString());
}
@Override
public void onComplete(String response, Object state) {
Log.d("Facebook Response", response);
JSONArray statusResults = app.convertToJSONArray(response);
try {
JSONObject statusObject = statusResults.getJSONObject(0);
String currentStatus = statusObject.getString("message");
Message message = handler.obtainMessage(1, currentStatus);
handler.sendMessage(message);
} catch (JSONException e) {
Log.e("Facebook User Status", e.toString());
}
}
});
}
};
thread.start();
}
}
}
我知道代码有点质量,我应该使用asynctask而不是线程和处理程序,但这只是一个测试,我想看看它是否有效......
堆栈跟踪:
06-20 19:30:15.180: E/AndroidRuntime(3863): FATAL EXCEPTION: main
06-20 19:30:15.180: E/AndroidRuntime(3863): java.lang.StackOverflowError
06-20 19:30:15.180: E/AndroidRuntime(3863): at com.ibm.icu4jni.util.LocaleData.get(LocaleData.java:96)
06-20 19:30:15.180: E/AndroidRuntime(3863): at java.util.Formatter.format(Formatter.java:1061)
06-20 19:30:15.180: E/AndroidRuntime(3863): at java.util.Formatter.format(Formatter.java:1031)
06-20 19:30:15.180: E/AndroidRuntime(3863): at java.lang.String.format(String.java:2183)
06-20 19:30:15.180: E/AndroidRuntime(3863): at java.lang.String.format(String.java:2157)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.text.Styled.drawDirectionalRun(Styled.java:266)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.text.Styled.drawText(Styled.java:362)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.text.Layout.drawText(Layout.java:1546)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.text.Layout.draw(Layout.java:380)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.widget.TextView.onDraw(TextView.java:4417)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.View.draw(View.java:6933)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.View.draw(View.java:6936)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.widget.AbsListView.dispatchDraw(AbsListView.java:1648)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.widget.ListView.dispatchDraw(ListView.java:3217)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.View.draw(View.java:6936)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.widget.AbsListView.draw(AbsListView.java:3030)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.View.draw(View.java:6936)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.widget.FrameLayout.draw(FrameLayout.java:357)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.View.draw(View.java:6936)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.widget.FrameLayout.draw(FrameLayout.java:357)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.View.draw(View.java:6936)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.widget.FrameLayout.draw(FrameLayout.java:357)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.View.draw(View.java:6936)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.widget.FrameLayout.draw(FrameLayout.java:357)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.View.draw(View.java:6936)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.widget.FrameLayout.draw(FrameLayout.java:357)
06-20 19:30:15.180: E/AndroidRuntime(3863): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1917)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewRoot.draw(ViewRoot.java:1530)
06-20 19:30:15.180: E/AndroidRuntime(3863): at android.view.ViewRoot.perfor
答案 0 :(得分:0)
你确定你的
status.setText(utf8Status)
在UI线程上运行?它可能不是因为Threaded Handler。您需要尝试将其作为POST运行。 但是,这通常会导致不同的错误消息。
请指出96行代码的哪一部分?导致错误的那个?