gpt4 book ai didi

java - Android 应用程序无法正常工作

转载 作者:行者123 更新时间:2023-11-30 03:05:39 24 4
gpt4 key购买 nike

当我的简单 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;
}
}

安卓 list :

    <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>

主.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>

日志:

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);
}
}

再见

关于java - Android 应用程序无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864763/

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