Android ListView在点击时抛出NullPointer异常

时间:2012-07-04 19:17:14

标签: java android android-listview

我有以下代码,它正确地将系列名称添加到aName并链接到aLink,因此两个ArrayLists都包含值。运行时ListView正确显示aName,但是当单击listview中的某个项时,应用程序将抛出NullPointer exeption。

    public class MainActivity extends ListActivity {
ArrayAdapter<String> adapter;
ArrayList aName;
ArrayList aLink;
ListView view;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    view = getListView();
    view.setTextFilterEnabled(true);
    //CONNECT CODE//
    Document main = Jsoup.parse("<html> </html>");
    try {
        main = Jsoup.connect("http://www.animeseason.com/anime-list/").get();
    } catch (IOException e) {
        AlertDialog alertDialog;
        alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Connection Error");
        alertDialog.setMessage("Could not Connect.");
        alertDialog.show();
        e.printStackTrace();
    }
    //PARSE HTML CODE//
    aName = new ArrayList();
    aLink = new ArrayList();
    main = Jsoup.parse(main.getElementsByClass("series_col").toString());
    Elements seriesL = main.getElementsByTag("li");
    for(Element anime: seriesL){
        Elements link = anime.getElementsByTag("a");
        aName.add(link.text());
        aLink.add(link.attr("href"));
    }
    //SET VIEW//
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, aName);
    setListAdapter(adapter);
    //ONCLICK
    view.setOnItemClickListener(
        new OnItemClickListener(){
        public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
             Intent myIntent = new Intent(view.getContext(), EpisodeListActivity.class);
             String name = (String) aName.get(position).toString();
             String link = (String) aLink.get(position).toString();
             myIntent.putExtra("Anime", name);
             myIntent.putExtra("Link", link);
             startActivity(myIntent);
        }
    });

}

这是日志 -

    07-04 15:08:15.382: W/dalvikvm(3489): threadid=1: thread exiting with uncaught                 exception (group=0x40a3c1f8)
    07-04 15:08:15.406: E/AndroidRuntime(3489): FATAL EXCEPTION: main
    07-04 15:08:15.406: E/AndroidRuntime(3489): java.lang.NullPointerException
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.AbsListView.obtainView(AbsListView.java:2035)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.ListView.makeAndAddView(ListView.java:1772)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.ListView.fillDown(ListView.java:672)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.ListView.fillFromTop(ListView.java:732)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.ListView.layoutChildren(ListView.java:1625)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.AbsListView.onLayout(AbsListView.java:1865)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.view.View.layout(View.java:11278)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.view.View.layout(View.java:11278)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.view.View.layout(View.java:11278)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.view.View.layout(View.java:11278)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.os.Looper.loop(Looper.java:137)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at android.app.ActivityThread.main(ActivityThread.java:4424)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
    07-04 15:08:15.406: E/AndroidRuntime(3489):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

您正在扩展ListActivity,覆盖Activity中的onListItemClick()方法而不是您在onCreate()

中执行的view.setOnItemClickListener

编辑:

布局xml中的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</LinearLayout>

在您使用布局的活动中:

课堂上的

:公共课MyActivity extends ListActivity

获取列表视图:ListView list = getListView();

设置列表适配器:setListAdapter(adapter);

MyActivity类中的

覆盖onListItemClick:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//do your thing 
}

文档:http://developer.android.com/reference/android/app/ListActivity.html

答案 1 :(得分:0)

您还可以创建&#34;普通&#34;活动而不是ListActivity。

public class MainActivity extends Activity {
...

将xml布局中的ListViews ID更改为&#34; @ + id / list&#34;。

<ListView
    android:id="@+id/list"
...

或者在onCreate方法中创建列表视图:

ListView listView = new ListView(this);
setContentView(listView);

获取ListView,您可以使用OnItemClickListener。

ListView listView = (ListView) findViewById(R.id.list); // only needed if you have a xml layout
listView.setAdapter(adapter);
listView.setOnItemClickListener( ...