问题描述
我有3个textviews我试图更新( nameTv,contentTv,publishedTv
)从JSON响应获得的数据。当我执行下面的textviews显示不更新code - 当我在 TextView的nameTv =(TextView中)findViewById(R.id.name)设置一个断点;
它似乎永远不会达到它,我不能确定这是为什么。
JAVA
公共类播放器扩展YouTubeBaseActivity工具
YouTubePlayer.OnInitializedListener {
公共静态最后弦乐API_KEY =XXXXXXXXXXXXXXXXXXXXXXXXXXX;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.player);
字符串标题= getIntent()getStringExtra(标题)。
字符串上传= getIntent()getStringExtra(上传)。
串观看次数= getIntent()getStringExtra(观看次数)。
TextView的titleTv =(TextView中)findViewById(R.id.titleTv);
TextView的uploaderTv =(TextView中)findViewById(R.id.uploaderTv);
TextView的viewCountTv =(TextView中)findViewById(R.id.viewCountTv);
titleTv.setText(职称);
uploaderTv.setText(本经+上传+|);
viewCountTv.setText(观看次数+意见);
YouTubePlayerView youTubePlayerView =(YouTubePlayerView)findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY,这一点);
GetYouTubeUserCommentsTask任务=新GetYouTubeUserCommentsTask(空,
观看次数);
task.execute();
}
@覆盖
公共无效onInitializationFailure(供应商提供,
YouTubeInitializationResult结果){
Toast.makeText(getApplicationContext(),onInitializationFailure(),
Toast.LENGTH_LONG).show();
}
@覆盖
公共无效onInitializationSuccess(供应商提供,
YouTubePlayer球员,布尔wasRestored){
如果(!wasRestored){
字符串VIDEO_ID = getIntent()getStringExtra(ID)。
player.loadVideo(VIDEO_ID);
}
}
公共final类GetYouTubeUserCommentsTask扩展
AsyncTask的<虚空,虚空,虚空> {
公共静态最终字符串库=CommentsLibrary;
私人最终处理程序的replyTo;
私人最终字符串用户名;
字符串VIDEO_ID = getIntent()getStringExtra(ID)。
公共GetYouTubeUserCommentsTask(处理器的replyTo,字符串username){
this.replyTo =的replyTo;
this.username =用户名;
}
@覆盖
保护无效doInBackground(虚空......为arg0){
尝试 {
HttpClient的客户端=新DefaultHttpClient();
HttpUriRequest请求=新HTTPGET(
http://gdata.youtube.com/feeds/api/videos/
+ VIDEO_ID
+/评论ν= 2及?中高音= JSON&安培;启动索引= 1&安培;最大-结果= 50安培; prettyprint =真);
HTT presponse响应= client.execute(要求);
字符串jsonString = StreamUtils.convertToString(响应
。.getEntity()的getContent());
JSONObject的JSON =新的JSONObject(jsonString);
JSONArray jsonArray = json.getJSONObject(饲料)。getJSONArray(
条目);
名单<意见>评论=新的ArrayList<意见>();
的for(int i = 0; I< jsonArray.length();我++){
的JSONObject的JSONObject = jsonArray.getJSONObject(我);
字符串名称= jsonObject.optString(名,设置defaultValue);
字符串的内容= jsonObject.optString(内容,设置defaultValue);
字符串公布= jsonObject.optString(发布,设置defaultValue);
comments.add(新评论(名称,内容,出版));
}
CommentsLibrary LIB =新CommentsLibrary(jsonString,jsonString,jsonString);
捆绑数据=新包();
data.putSerializable(图书馆,LIB);
消息味精= Message.obtain();
msg.setData(数据);
replyTo.sendMessage(MSG);
}赶上(ClientProtocolException E){
Log.e(Feck,E);
}赶上(IOException异常E){
Log.e(Feck,E);
}赶上(JSONException E){
Log.e(Feck,E);
}
返回null;
}
@覆盖
保护无效onPostExecute(无效的结果){
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());
}
}
}
CommentsLibrary.java
公共类CommentsLibrary实现Serializable {
//注释的所有者的用户名
私有静态字符串名称;
//注释
私有静态字符串的内容;
//日期注释出版
私有静态字符串出版;
公共CommentsLibrary(字符串名称,字符串内容,字符串出版){
this.name =名称;
this.content =内容;
this.published =出版;
}
/ **
返回:用户名
* /
公共静态字符串的getName(){
返回名称;
}
/ **
返回:视频
* /
公共静态字符串的getContent(){
返回的内容;
}
/ **
返回:视频
* /
公共静态字符串出版文章(){
返回出版;
}
}
logcat的:
12-11 14:23:14.301:E / AndroidRuntime(2654):致命异常:AsyncTask的#2
12-11 14:23:14.301:E / AndroidRuntime(2654):java.lang.RuntimeException的:一个错误而执行doInBackground发生()
12-11 14:23:14.301:E / AndroidRuntime(2654):在android.os.AsyncTask $ 3.done(AsyncTask.java:299)
12-11 14:23:14.301:E / AndroidRuntime(2654):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273)
12-11 14:23:14.301:E / AndroidRuntime(2654):在java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-11 14:23:14.301:E / AndroidRuntime(2654):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307)
12-11 14:23:14.301:E / AndroidRuntime(2654):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-11 14:23:14.301:E / AndroidRuntime(2654):在android.os.AsyncTask $ SerialExecutor $ 1.运行(AsyncTask.java:230)
12-11 14:23:14.301:E / AndroidRuntime(2654):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-11 14:23:14.301:E / AndroidRuntime(2654):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
12-11 14:23:14.301:E / AndroidRuntime(2654):在java.lang.Thread.run(Thread.java:856)
12-11 14:23:14.301:E / AndroidRuntime(2654):由:显示java.lang.NullPointerException
12-11 14:23:14.301:E / AndroidRuntime(2654):在com.idg.omv.ui.phone.Player $ GetYouTubeUserCommentsTask.doInBackground(Player.java:134)
12-11 14:23:14.301:E / AndroidRuntime(2654):在com.idg.omv.ui.phone.Player $ GetYouTubeUserCommentsTask.doInBackground(Player.java:1)
12-11 14:23:14.301:E / AndroidRuntime(2654):在android.os.AsyncTask $ 2.call(AsyncTask.java:287)
12-11 14:23:14.301:E / AndroidRuntime(2654):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
12-11 14:23:14.301:E / AndroidRuntime(2654):... 5个
12-11 14:23:23.981:I /编舞(2654):跳过576帧!该应用程序可能会做它的主线程的工作太多了。
12-11 14:23:24.621:E / ActivityThread(2654):活动com.idg.omv.ui.phone.Player渗漏ServiceConnection com.google.android.youtube.player.internal.r$e@41b1e948这是原来这里的约束
12-11 14:23:24.621:E / ActivityThread(2654):android.app.ServiceConnectionLeaked:活动com.idg.omv.ui.phone.Player渗漏ServiceConnection com.google.android.youtube.player.internal.r $机智小41b1e948最初这里必然
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.LoadedApk $ ServiceDispatcher< INIT>(LoadedApk.java:966)
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:860)
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.ContextImpl.bindService(ContextImpl.java:1364)
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.ContextImpl.bindService(ContextImpl.java:1356)
12-11 14:23:24.621:E / ActivityThread(2654):在android.content.ContextWrapper.bindService(ContextWrapper.java:401)
12-11 14:23:24.621:E / ActivityThread(2654):在com.google.android.youtube.player.internal.re(来源不明)
12-11 14:23:24.621:E / ActivityThread(2654):在com.google.android.youtube.player.YouTubePlayerView.a(来源不明)
12-11 14:23:24.621:E / ActivityThread(2654):在com.google.android.youtube.player.YouTubeBaseActivity $ AA(来源不明)
12-11 14:23:24.621:E / ActivityThread(2654):在com.google.android.youtube.player.YouTubePlayerView.initialize(来源不明)
12-11 14:23:24.621:E / ActivityThread(2654):在com.idg.omv.ui.phone.Player.onCreate(Player.java:57)
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.Activity.performCreate(Activity.java:5206)
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.ActivityThread.access $ 700(ActivityThread.java:140)
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1237)
12-11 14:23:24.621:E / ActivityThread(2654):在android.os.Handler.dispatchMessage(Handler.java:99)
12-11 14:23:24.621:E / ActivityThread(2654):在android.os.Looper.loop(Looper.java:137)
12-11 14:23:24.621:E / ActivityThread(2654):在android.app.ActivityThread.main(ActivityThread.java:4921)
12-11 14:23:24.621:E / ActivityThread(2654):在java.lang.reflect.Method.invokeNative(本机方法)
12-11 14:23:24.621:E / ActivityThread(2654):在java.lang.reflect.Method.invoke(Method.java:511)
12-11 14:23:24.621:E / ActivityThread(2654):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1027)
12-11 14:23:24.621:E / ActivityThread(2654):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
12-11 14:23:24.621:E / ActivityThread(2654):在dalvik.system.NativeStart.main(本机方法)
12-11 14:23:24.876:D / dalvikvm(2654):GC_CONCURRENT释放1495K,免费18%25511K / 30983K,暂停12毫秒+ 3ms的,总过48ms
12-11 14:23:24.876:D / dalvikvm(2654):WAIT_FOR_CONCURRENT_GC受阻18毫秒
编辑:在回应@Raghunandan
如果我理解正确,我需要通过处理器的replyTo:
GetYouTubeUserCommentsTask任务=新GetYouTubeUserCommentsTask(处理器的replyTo,字符串username);
然后:
公共GetYouTubeUserCommentsTask(处理器的replyTo,字符串username){
this.replyTo =的replyTo; //的replyTo为null
this.username =用户名;
}
这看起来正确的给你 - 如果不是请让我知道
GetYouTubeUserCommentsTask任务=新GetYouTubeUserCommentsTask(空,
观看次数); //传递null。
和你有
公共GetYouTubeUserCommentsTask(处理器的replyTo,字符串username){
this.replyTo =的replyTo; //的replyTo为null
this.username =用户名;
}
的replyTo
为空。您需要初始化处理程序的replyTo
I have 3 textviews I'm attempting to update (nameTv, contentTv, publishedTv
) with data obtained from a JSON response. When I execute the code shown below the textviews do not update - and when I set a breakpoint at TextView nameTv = (TextView) findViewById(R.id.name);
it never seems to reach it and I'm unsure why.
JAVA
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());
}
}
}
CommentsLibrary.java
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;
}
}
LOGCAT:
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
EDIT: In Response to @Raghunandan
If I understand you correctly I need to pass the handler replyTo:
GetYouTubeUserCommentsTask task = new GetYouTubeUserCommentsTask(Handler replyTo, String username);
Then:
public GetYouTubeUserCommentsTask(Handler replyTo, String username) {
this.replyTo = replyTo; // replyTo is null
this.username = username;
}
Does this seem correct to you - if not please let me know
GetYouTubeUserCommentsTask task = new GetYouTubeUserCommentsTask(null,
viewCount); // passing null.
And you have
public GetYouTubeUserCommentsTask(Handler replyTo, String username) {
this.replyTo = replyTo; // replyTo is null
this.username = username;
}
replyTo
is null. You need to Initialize the handler replyTo
这篇关于TextView中不会更新与JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!