Google Android 开发入门与实战 源代码调试与排错

第12章 RSS阅读器

现象描述:

源代码运行后效果如图:

但是当点击任意一个ListItem后,本来应该打开浏览此Item详细内容的Activity,可是程序崩溃了,如图:

原因分析:

单步调试后,发现程序死在文件ActivityShowDescription.java的onCreate函数中:

 

 1 public void onCreate(Bundle icicle) {
 2       ...
 3       if (startingIntent != null) {
 4          Bundle bundle = startingIntent
 5                .getBundleExtra("android.intent.extra.rssItem");
 6          if (bundle == null) {
 7             content = "不好意思程序出错啦";
 8          } else {
 9             content = bundle.getString("title") + "\n\n"
10                   + bundle.getString("pubdate") + "\n\n"
11                   + bundle.getString("description").replace('\n', ' ')
12                   + "\n\n详细信息请访问以下网址:\n" + bundle.getString("link");
13          }
14       } else {
15          content = "不好意思程序出错啦";
16  
17       }
18       ...
19    }

 

 

并且,是由于bundle.getString("descrition").replace('\n', ' ')这一处空指针导常导致,由于此时bundle.getString("description")返回为null,再调用replace方法肯定会使程序崩溃。

查看源代码,在文件ActivityMain.java中定义了获取的RSS内容地址如下:

public final String RSS_URL = "http://feed.feedsky.com/yeeyan_top";

使用Fiddler,访问此地址后并查看返回结果:

仔细一看,发现,在item节点中确实没有description这一子节点,这就导致类RSSHandler.java文件分析返回的FeedItem中getDescription函数会返回null,从而出现上面描述的错误现象。

解决方法:

1 在类ActivityShowDescription的onCreate函数中做判空处理

2 修复RSSHandler类,使之设置正确的description,如把content节点内容设置为description

   

第14章 Android的豆瓣网(Web 2.0)移动客户端开发

现象描述:

当第一次使用时,如下图,点击"登陆"按钮后,程序崩溃。

查看DDMS LogCat窗口消息,发现以下Exception记录。

04-11 23:09:23.413: E/AndroidRuntime(5989): FATAL EXCEPTION: main

04-10 23:28:44.894: E/AndroidRuntime(15118): java.lang.NoClassDefFoundError: com.google.gdata.client.douban.DoubanService

NoClassDefFoundError,此错误相关资料,请看以下两个链接:

http://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html

http://javarevisited.blogspot.com/2011/07/classnotfoundexception-vs.html

看完上面两个链接内容后,又回去看LogCat消息,在这个错误上面发现如下Log:

04-11 23:09:19.585: W/dalvikvm(5989): Unable to resolve superclass of Lcom/google/gdata/client/douban/DoubanService; (56)

04-11 23:09:19.585: W/dalvikvm(5989): Link of class 'Lcom/google/gdata/client/douban/DoubanService;' failed

04-11 23:09:19.585: E/dalvikvm(5989): Could not find class 'com.google.gdata.client.douban.DoubanService', referenced from method com.douban.android.ActivityAuth.getRequestUrl

这就终于明白了,在关于NoClassDefFoundError的第一个参考链接中的以下描述:

4) Because NoClassDefFoundError is a sub class of java.lang.LinkageError it can also come if one of it dependency like native library may not available.

查看一下DoubanService类的父类为com.google.gdata.client.Service,这个类是来自于文件夹lib下的包gdata-client-1.0.jar。换句话说,我们的APK在手机中运行找不到gdata-client-1.0.jar这个jar包。

在Google中搜索关键字:Unable to resolve superclass of,得到博客园的一篇文章的链接:

http://www.cnblogs.com/vaiyanzi/archive/2012/08/09/2629950.html

按照这个文章说明,在工程下新建文件夹libs,并把第三方jar包拷贝到此文件夹,编译运行,OK了。    

Android开发中如果将ADT 升级到17以上(包括17),如果你的项目中引用了其它第三方的jar包,在运行是就会报下面的错误。这是因为在ADT17以后第三方包统一要放在一个libs的文件夹中,如果你的第三方包(jar)没有放在这里,编译时不会将其拷贝到.dex文件中。

此时看一下工程中的bin文件夹内容,如下图:

未新建libs文件夹前,bin文件夹内容,如下图:

如果在Console窗口中看到如下输出:

[2013-04-11 23:26:32 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/google/gdata/client/douban/DoubanQuery;

[2013-04-11 23:26:32 - douban_android] Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/google/gdata/client/douban/DoubanQuery;

请在libs中把lib.jar文件删除。

此项目源码调试待续……

posted @ 2013-04-08 22:53  weichen2046  阅读(544)  评论(0编辑  收藏  举报