java.lang.RuntimeException:使用jsoup执行doInBackground()时发生错误

时间:2014-08-04 14:35:34

标签: android android-asynctask jsoup

我是Android开发的初学者,我收到此错误,导致我的Asynctask无法使用UI工作:

java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)

我无法理解为什么。我正在使用Android Studio。在构建它以供发布之前,我的应用程序工作正常但现在它崩溃了。这是我的班级:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_my, null);
    lv = (ListView) view.findViewById(R.id.listView1);
    adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.product_name, titleList);
    new NewThread().execute();
    return view;
}
public class NewThread extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... arg) {
        Document doc;
        try {
            doc = Jsoup.connect("http://ot76.ru/mob/list.php?vt=" + (pageNumber + 1) + "&nl=0").get();
            title = doc.select("a");
            for (Element titles : title) {
                titleList.add(titles.html().replace("&nbsp;", "(") + ")    ");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                String str = lv.getItemAtPosition(position).toString();
                Intent intent = new Intent(getActivity().getApplicationContext(), Rasp.class);
                intent.putExtra("url", "http://ot76.ru/mob/getroutestr.php?vt=" + (pageNumber + 1) + "&nl=0&nmar=" + str.substring(0, str.indexOf(' ')).replace("к", "k").replace("а", "a").replace("б", "b").replace("с", "c").replace("д", "d"));
               intent.putExtra("stops", lv.getItemAtPosition(position).toString().substring(str.indexOf(' '), str.length() - 1));
                startActivity(intent);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:3)

问题解决了。我只需要添加

-keep public class org.jsoup.** { public *; }

到我的proguard-android.txt文件。

答案 1 :(得分:1)

从您的代码中可以看出,您正在使用 null (ArrayList)填充adapter。所以,你得到NPE

在填写adapter's object之后,您应该在 onPostExecute(...)中为ListView创建titleList,否则它将为空。在 onCreateView(...) titleList null

onCreateView(...)中的

创建titleList's对象。

titleList = new ArrayList<String>();

onCreateView(...)

中删除此行
adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.product_name, titleList);
onPostExecute(...)

填写 web service

中的值之后填充ListView
if(titleList != null) {
 adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.product_name, titleList);
 lv.setAdapter(adapter);
}

编辑: 尝试创建custom adapter类并尝试调试doc不是 null