gpt4 book ai didi

java - Android ListView 点击时抛出 NullPointer 异常

转载 作者:行者123 更新时间:2023-12-01 15:18:15 26 4
gpt4 key购买 nike

我有以下代码,它可以正确地将系列名称添加到 aName 并链接到 aLink,以便两个 ArrayList 都保存值。运行时,ListView 正确显示 aName,但是当单击 ListView 中的一项时,应用程序会引发 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)

最佳答案

您正在扩展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>

在使用布局的 Activity 中:

在类中:公共(public)类MyActivity扩展ListActivity

获取 ListView :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

关于java - Android ListView 点击时抛出 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11334417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com