0

我有以下代码正确地将系列名称添加到 aName 并链接到 aLink 以便两个 ArrayLists 都保存值。运行 ListView 时正确显示 aName,但是当单击列表视图中的一项时,应用程序会抛出 NullPointer 异常。

    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)
4

2 回答 2

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

于 2012-07-04T19:19:53.853 回答
0

您还可以创建一个“普通”活动而不是 ListActivity。

public class MainActivity extends Activity {
...

将 xml 布局中的 ListViews id 更改为“@+id/list”。

<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( ...
于 2012-11-05T12:29:58.057 回答