我有3个文本视图我正在尝试使用从JSON响应中获取的数据更新(nameTv, contentTv, publishedTv
)。当我执行下面显示的代码时,textviews不会更新 - 当我在TextView nameTv = (TextView) findViewById(R.id.name);
处设置断点时,它似乎永远不会达到它并且我不确定为什么。
public class Player extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
String title = getIntent().getStringExtra("title");
String uploader = getIntent().getStringExtra("uploader");
String viewCount = getIntent().getStringExtra("viewCount");
TextView titleTv = (TextView) findViewById(R.id.titleTv);
TextView uploaderTv = (TextView) findViewById(R.id.uploaderTv);
TextView viewCountTv = (TextView) findViewById(R.id.viewCountTv);
titleTv.setText(title);
uploaderTv.setText("by" + uploader + " |");
viewCountTv.setText(viewCount + " views");
YouTubePlayerView youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
GetYouTubeUserCommentsTask task = new GetYouTubeUserCommentsTask(null,
viewCount);
task.execute();
}
@Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
Toast.makeText(getApplicationContext(), "onInitializationFailure()",
Toast.LENGTH_LONG).show();
}
@Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
String video_id = getIntent().getStringExtra("id");
player.loadVideo(video_id);
}
}
public final class GetYouTubeUserCommentsTask extends
AsyncTask<Void, Void, Void> {
public static final String LIBRARY = "CommentsLibrary";
private final Handler replyTo;
private final String username;
String video_id = getIntent().getStringExtra("id");
public GetYouTubeUserCommentsTask(Handler replyTo, String username) {
this.replyTo = replyTo;
this.username = username;
}
@Override
protected Void doInBackground(Void... arg0) {
try {
HttpClient client = new DefaultHttpClient();
HttpUriRequest request = new HttpGet(
"http://gdata.youtube.com/feeds/api/videos/"
+ video_id
+ "/comments?v=2&alt=json&start-index=1&max-results=50&prettyprint=true");
HttpResponse response = client.execute(request);
String jsonString = StreamUtils.convertToString(response
.getEntity().getContent());
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject("feed").getJSONArray(
"entry");
List<Comments> comments = new ArrayList<Comments>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.optString("name","defaultValue");
String content = jsonObject.optString("content","defaultValue");
String published = jsonObject.optString("published","defaultValue");
comments.add(new Comments(name, content, published));
}
CommentsLibrary lib = new CommentsLibrary(jsonString, jsonString, jsonString);
Bundle data = new Bundle();
data.putSerializable(LIBRARY, lib);
Message msg = Message.obtain();
msg.setData(data);
replyTo.sendMessage(msg);
} catch (ClientProtocolException e) {
Log.e("Feck", e);
} catch (IOException e) {
Log.e("Feck", e);
} catch (JSONException e) {
Log.e("Feck", e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
TextView nameTv = (TextView) findViewById(R.id.name);
nameTv.setText(com.idg.omv.domain.CommentsLibrary.getName());
TextView contentTv = (TextView) findViewById(R.id.content);
contentTv.setText(com.idg.omv.domain.CommentsLibrary.getContent());
TextView publishedTv = (TextView) findViewById(R.id.published);
publishedTv.setText(com.idg.omv.domain.CommentsLibrary.getPublished());
}
}
}
public class CommentsLibrary implements Serializable{
// The username of the owner of the comment
private static String name;
// The comment
private static String content;
// The date the comment was published
private static String published;
public CommentsLibrary(String name, String content, String published) {
this.name = name;
this.content = content;
this.published = published;
}
/**
* @return the user name
*/
public static String getName() {
return name;
}
/**
* @return the videos
*/
public static String getContent() {
return content;
}
/**
* @return the videos
*/
public static String getPublished() {
return published;
}
}
12-11 14:23:14.301: E/AndroidRuntime(2654): FATAL EXCEPTION: AsyncTask #2
12-11 14:23:14.301: E/AndroidRuntime(2654): java.lang.RuntimeException: An error occured while executing doInBackground()
12-11 14:23:14.301: E/AndroidRuntime(2654): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-11 14:23:14.301: E/AndroidRuntime(2654): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-11 14:23:14.301: E/AndroidRuntime(2654): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-11 14:23:14.301: E/AndroidRuntime(2654): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-11 14:23:14.301: E/AndroidRuntime(2654): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-11 14:23:14.301: E/AndroidRuntime(2654): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-11 14:23:14.301: E/AndroidRuntime(2654): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-11 14:23:14.301: E/AndroidRuntime(2654): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-11 14:23:14.301: E/AndroidRuntime(2654): at java.lang.Thread.run(Thread.java:856)
12-11 14:23:14.301: E/AndroidRuntime(2654): Caused by: java.lang.NullPointerException
12-11 14:23:14.301: E/AndroidRuntime(2654): at com.idg.omv.ui.phone.Player$GetYouTubeUserCommentsTask.doInBackground(Player.java:134)
12-11 14:23:14.301: E/AndroidRuntime(2654): at com.idg.omv.ui.phone.Player$GetYouTubeUserCommentsTask.doInBackground(Player.java:1)
12-11 14:23:14.301: E/AndroidRuntime(2654): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-11 14:23:14.301: E/AndroidRuntime(2654): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-11 14:23:14.301: E/AndroidRuntime(2654): ... 5 more
12-11 14:23:23.981: I/Choreographer(2654): Skipped 576 frames! The application may be doing too much work on its main thread.
12-11 14:23:24.621: E/ActivityThread(2654): Activity com.idg.omv.ui.phone.Player has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@41b1e948 that was originally bound here
12-11 14:23:24.621: E/ActivityThread(2654): android.app.ServiceConnectionLeaked: Activity com.idg.omv.ui.phone.Player has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@41b1e948 that was originally bound here
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:966)
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:860)
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.ContextImpl.bindService(ContextImpl.java:1364)
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.ContextImpl.bindService(ContextImpl.java:1356)
12-11 14:23:24.621: E/ActivityThread(2654): at android.content.ContextWrapper.bindService(ContextWrapper.java:401)
12-11 14:23:24.621: E/ActivityThread(2654): at com.google.android.youtube.player.internal.r.e(Unknown Source)
12-11 14:23:24.621: E/ActivityThread(2654): at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source)
12-11 14:23:24.621: E/ActivityThread(2654): at com.google.android.youtube.player.YouTubeBaseActivity$a.a(Unknown Source)
12-11 14:23:24.621: E/ActivityThread(2654): at com.google.android.youtube.player.YouTubePlayerView.initialize(Unknown Source)
12-11 14:23:24.621: E/ActivityThread(2654): at com.idg.omv.ui.phone.Player.onCreate(Player.java:57)
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.Activity.performCreate(Activity.java:5206)
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.ActivityThread.access$700(ActivityThread.java:140)
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
12-11 14:23:24.621: E/ActivityThread(2654): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 14:23:24.621: E/ActivityThread(2654): at android.os.Looper.loop(Looper.java:137)
12-11 14:23:24.621: E/ActivityThread(2654): at android.app.ActivityThread.main(ActivityThread.java:4921)
12-11 14:23:24.621: E/ActivityThread(2654): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 14:23:24.621: E/ActivityThread(2654): at java.lang.reflect.Method.invoke(Method.java:511)
12-11 14:23:24.621: E/ActivityThread(2654): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
12-11 14:23:24.621: E/ActivityThread(2654): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
12-11 14:23:24.621: E/ActivityThread(2654): at dalvik.system.NativeStart.main(Native Method)
12-11 14:23:24.876: D/dalvikvm(2654): GC_CONCURRENT freed 1495K, 18% free 25511K/30983K, paused 12ms+3ms, total 48ms
12-11 14:23:24.876: D/dalvikvm(2654): WAIT_FOR_CONCURRENT_GC blocked 18ms
如果我理解正确,我需要传递处理程序replyTo:
GetYouTubeUserCommentsTask task = new GetYouTubeUserCommentsTask(Handler replyTo, String username);
然后:
public GetYouTubeUserCommentsTask(Handler replyTo, String username) {
this.replyTo = replyTo; // replyTo is null
this.username = username;
}
这对你来说是否正确 - 如果没有,请告诉我
答案 0 :(得分:1)
GetYouTubeUserCommentsTask task = new GetYouTubeUserCommentsTask(null,
viewCount); // passing null.
你有
public GetYouTubeUserCommentsTask(Handler replyTo, String username) {
this.replyTo = replyTo; // replyTo is null
this.username = username;
}
replyTo
为空。您需要初始化处理程序replyTo