簡體   English   中英

Android應用無法正常運行

[英]Android app doesn't work correctly

當我的簡單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)

也許您的錯誤可能與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);
    }
}

再見

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM