1

您好我正在尝试实现此代码以显示ListView一页的所有href,但我有这个错误......有人可以帮助我吗?

private final String TAG="links";

private ArrayAdapter<String> mAdapter = null;
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;

@SuppressLint( "NewApi")
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Show the Up button in the action bar.
    //  getActionBar().setDisplayHomeAsUpEnabled(true);
    //}

    Intent intent = getIntent();
    String url_link =intent.getStringExtra(MainActivity.EXTRA_INPUTURL);
    String keyword = intent.getStringExtra(MainActivity.EXTRA_INPUTKEY);


    mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1);
    final ListView list=(ListView) findViewById(android.R.id.list);
    list.setAdapter(mAdapter);

    //get the juice
    keyword.toLowerCase();

    Elements elem=getHtmlCode(url_link,keyword);
    for(Element link:elem){
        if(link.text().toLowerCase().contains(keyword)) 
            mAdapter.add(link.attr("abs:href"));        
            mAdapter.notifyDataSetChanged();
    }
}

    // Get the message from the intent


    // Structure your code. If it's a larger block that does one thing,
    // extract it into a method.
    private Elements getHtmlCode(String url,String key){
        try {
            if (!url.startsWith("http://") && !url.startsWith("https://"))
                       url = "http://" + url;
            //Document doc = Jsoup.connect(url).userAgent("Internet").data("name", "jsoup").get();
            Document doc = Jsoup.connect(url).get();
            Elements content = doc.select(key);
            return content;
        } catch (IOException e) {
            // Never e.printStackTrace(), it cuts off after some lines and you'll
            // lose information that's very useful for debugging. Always use proper
            // logging, like Android's Log class, check out
            // http://developer.android.com/tools/debugging/debugging-log.html
            Log.e(TAG, "Failed to load HTML code", e);
            // Also tell the user that something went wrong (keep it simple,
            // no stacktraces):

            Toast.makeText(this, "Failed to load HTML code",
                    Toast.LENGTH_SHORT).show();
        }
        return null;
    }

--------堆栈树--------

08-08 21:39:48.349: E/AndroidRuntime(18716): FATAL EXCEPTION: main
08-08 21:39:48.349: E/AndroidRuntime(18716): java.lang.ExceptionInInitializerError
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.nodes.Document$OutputSettings.<init>(Document.java:210)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.nodes.Document.<init>(Document.java:18)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.parser.TreeBuilder.initialiseParse(TreeBuilder.java:27)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.parser.TreeBuilder.parse(TreeBuilder.java:40)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.parser.HtmlTreeBuilder.parse(HtmlTreeBuilder.java:37)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.parser.Parser.parseInput(Parser.java:30)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.helper.DataUtil.parseByteData(DataUtil.java:102)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.helper.HttpConnection$Response.parse(HttpConnection.java:498)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:154)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at com.newsfinder.DisplayActivity.getHtmlCode(DisplayActivity.java:71)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at com.newsfinder.DisplayActivity.onCreate(DisplayActivity.java:53)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at android.os.Looper.loop(Looper.java:130)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at android.app.ActivityThread.main(ActivityThread.java:3687)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at java.lang.reflect.Method.invokeNative(Native Method)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at java.lang.reflect.Method.invoke(Method.java:507)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at dalvik.system.NativeStart.main(Native Method)
08-08 21:39:48.349: E/AndroidRuntime(18716): Caused by: java.lang.ExceptionInInitializerError
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.nodes.Entities$EscapeMode.<clinit>(Entities.java:20)
08-08 21:39:48.349: E/AndroidRuntime(18716):    ... 24 more
08-08 21:39:48.349: E/AndroidRuntime(18716): Caused by: java.lang.NullPointerException
08-08 21:39:48.349: E/AndroidRuntime(18716):    at java.util.Properties.load(Properties.java:264)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.nodes.Entities.loadEntities(Entities.java:151)
08-08 21:39:48.349: E/AndroidRuntime(18716):    at org.jsoup.nodes.Entities.<clinit>(Entities.java:135)
08-08 21:39:48.349: E/AndroidRuntime(18716):    ... 25 more
4

0 回答 0