开发者

List Activity Null Pointer Exception. Force Close on Emulator Start Up

Here is the code for my 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(开发者_C百科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);
      }
    });

}

I keep getting the null pointer exception and can't find a solution. Any help?

EDIT!

The arraylist messages is modified in the method load messages. The LogCat message at the top is just: "09-28 01:45:06.726: ERROR/AndroidRuntime(755): java.lang.NullPointerException"

The full error is: 09-28 01:45:06.726: ERROR/AndroidRuntime(755): FATAL EXCEPTION: main 09-28 01:45:06.726: ERROR/AndroidRuntime(755): java.lang.NullPointerException 09-28 01:45:06.726: ERROR/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: ERROR/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: ERROR/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: ERROR/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: ERROR/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: ERROR/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: ERROR/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 (server)' ~ Consumer closed input channel or an error occurred. events=0x8 09-28 01:45:08.636: ERROR/InputDispatcher(72): channel '4093eaf0 cc.rss/cc.rss.CornellRSS (server)' ~ Channel is unrecoverably broken and will be disposed!

Sorry for not making this available sooner.


On the String[] titles = new String[messages.size()]; you need to actually put some values on the titles. The null pointer is probably happening when the ArrayAdapter tries to access your titles and just finds a null pointer.

If that doesn't work them please update your question with the logcat message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜