Android应用无法正常运行

时间:2014-02-18 20:37:22

标签: java android intellij-idea

当我的简单android java解析应用程序无法正常工作时遇到错误。它只显示“错误”文本(捕获异常)而不是页面的单个元素。过滤器是正常的,因为应用程序正在其他模拟器上工作,但现在我不记得该AVD的规格。

package com.example.untitled5;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    static final String BLOG_URL = "[here is correct link]";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // set layout view
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // process
        try {
            ((TextView)findViewById(R.id.tv)).setText(getBlogStats());
        } catch (Exception ex) {
            ((TextView)findViewById(R.id.tv)).setText("Error");
        }
    }
    protected String getBlogStats() throws Exception {
        String result = "";
        // get html document structure
        Document document = Jsoup.connect(BLOG_URL).get();
        // selector query
        //while (!document.)
        Elements nodeBlogStats = document.select(".ttElement");
        // check results
        if(nodeBlogStats.size() > 0) {
            // get value
            result = nodeBlogStats.get(0).text();
        }

        // return
        return result;
    }
}

AndroidManifest:          

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="19"/>
    <permission android:name="android.permission.INTERNET"/>
    <application
        android:label="@string/app_name">
        <activity
            android:name="MyActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
    >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

logcat的:

02-18 20:34:38.021       91-130/system_process I/ActivityManager﹕ Displayed com.example.untitled5/.MyActivity: +2s702ms
02-18 20:34:43.221      390-390/com.android.defcontainer D/dalvikvm﹕ GC_EXPLICIT freed 6K, 54% free 2537K/5511K, external 1625K/2137K, paused 46ms
02-18 20:34:47.537       91-204/system_process D/SntpClient﹕ request time failed: java.net.SocketException: Address family not supported by protocol

更新:使用提示稍微修改代码

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    static final String BLOG_URL = "link";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // set layout view
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // async task start
        new FillText().execute();
    }
    private class FillText extends AsyncTask <Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            String result = "";

            // get html document structure
            Document document = null;
            try {
                document = Jsoup.connect(BLOG_URL).get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // selector query
            Elements nodeBlogStats = document.select(".ttElement");
            // check results
            if(nodeBlogStats.size() > 0) {
                // get value
                result = nodeBlogStats.get(0).text();
            }

            // return
            return result;
        }
        @Override
        protected void onPostExecute(String myText){
            super.onPostExecute(myText);
            ((TextView)findViewById(R.id.tv)).setText(doInBackground());
        }
    }
}

现在我收到了下一个错误:

02-18 23:17:26.371      688-697/com.example.untitled5 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:200)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
            at java.util.concurrent.FutureTask.run(FutureTask.java:138)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
            at java.lang.Thread.run(Thread.java:1019)
     Caused by: java.lang.NullPointerException
            at com.example.untitled5.MyActivity$FillText.doInBackground(MyActivity.java:40)
            at com.example.untitled5.MyActivity$FillText.doInBackground(MyActivity.java:26)

1 个答案:

答案 0 :(得分:1)

也许你的错误可能与getBlogStats()

中json请求内的延迟有关

您应该使用AsyncTask来填充该textview ......就像这样

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ....

    // AsyncTask start
    new FillText().execute();

    ..

}

private class FillText extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        String result = "";
        // get html document structure
        Document document = Jsoup.connect(BLOG_URL).get();
        // selector query
        //while (!document.)
        Elements nodeBlogStats = document.select(".ttElement");
        // check results
        if(nodeBlogStats.size() > 0) {
            // get value
            result = nodeBlogStats.get(0).text();
        }

        // return
        return result;
    }

    @Override
    protected void onPostExecute(String myText) {
        super.onPostExecute(myText);

        if(!myText.equals(""))
            ((TextView)findViewById(R.id.tv)).setText(myText);
    }
}

再见