以下是我的ListActivity的代码:
public class CornellRSS extends ListActivity {
private final static String rssUrl = "http://news.cornellcollege.edu/rss";
RSSParser parser;
ArrayList<Message> messages;
private ProgressDialog pd;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
messages = new ArrayList<Message>();
parser = new RSSParser(rssUrl);
loadMessages();
String[] titles = new String[messages.size()];
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, titles));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
String keyword = (String) ((TextView) view).getText();
Message target = getMessage(keyword);
Bundle b = new Bundle();
b.putString(RSSHandler.TITLE, target.getTitle());
b.putString(RSSHandler.DESCRIPTION, target.getTitle());
b.putString(RSSHandler.LINK, target.getTitle());
b.putString(RSSHandler.PUB_DATE, target.getTitle());
Intent intent = new Intent(view.getContext(), RSSFeed.class);
intent.putExtras(b);
startActivity(intent);
}
});
}
我一直得到空指针异常,无法找到解决方案。有什么帮助吗?
EDIT!
在方法加载消息中修改了arraylist消息。 顶部的LogCat消息只是:“09-28 01:45:06.726:ERROR / AndroidRuntime(755):java.lang.NullPointerException”
完整错误是:09-28 01:45:06.726:ERROR / AndroidRuntime(755): 致命异性:主09-28 01:45:06.726:错误/ AndroidRuntime(755): java.lang.NullPointerException 09-28 01:45:06.726: 错误/ AndroidRuntime(755):at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355) 09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.widget.AbsListView.obtainView(AbsListView.java:1430)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.widget.ListView.makeAndAddView(ListView.java:1745)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.widget.ListView.fillDown(ListView.java:670)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.widget.ListView.fillFromTop(ListView.java:727)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.widget.ListView.layoutChildren(ListView.java:1598)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.widget.AbsListView.onLayout(AbsListView.java:1260)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.view.View.layout(View.java:7175)09-28 01:45:06.726: 错误/ AndroidRuntime(755):at android.widget.FrameLayout.onLayout(FrameLayout.java:338)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.view.View.layout(View.java:7175)09-28 01:45:06.726: 错误/ AndroidRuntime(755):at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254) 09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130) 09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.view.View.layout(View.java:7175)09-28 01:45:06.726: 错误/ AndroidRuntime(755):at android.widget.FrameLayout.onLayout(FrameLayout.java:338)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.view.View.layout(View.java:7175)09-28 01:45:06.726: 错误/ AndroidRuntime(755):at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.os.Handler.dispatchMessage(Handler.java:99)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at android.os.Looper.loop(Looper.java:123)09-28 01:45:06.726: 错误/ AndroidRuntime(755):at android.app.ActivityThread.main(ActivityThread.java:3683)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at java.lang.reflect.Method.invokeNative(Native Method)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at java.lang.reflect.Method.invoke(Method.java:507)09-28 01:45:06.726: 错误/ AndroidRuntime(755):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 09-28 01:45:06.726:ERROR / AndroidRuntime(755):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)09-28 01:45:06.726:ERROR / AndroidRuntime(755):at dalvik.system.NativeStart.main(Native Method)09-28 01:45:08.624: ERROR / InputDispatcher(72):channel'4093eaf0 cc.rss / cc.rss.CornellRSS (服务器)'〜消费者关闭输入通道或发生错误。 events = 0x8 09-28 01:45:08.636:ERROR / InputDispatcher(72):channel '4093eaf0 cc.rss / cc.rss.CornellRSS(服务器)'〜频道是 不可挽回的破碎,将被处置!
很抱歉没有尽快提供此服务。
答案 0 :(得分:0)
在String[] titles = new String[messages.size()];
上你需要在标题上加上一些值。当ArrayAdapter尝试访问您的标题并且只是找到一个空指针时,可能会发生空指针。
如果这不起作用,请使用logcat消息更新您的问题。