そのため、NewsParserにAsyncTaskを実装しようとしていますが、取得したと思いましたが、アクティビティを実行できません。LogCatで次のエラーが発生します。
12-11 16:15:24.572: E/AndroidRuntime(992): FATAL EXCEPTION: main
12-11 16:15:24.572: E/AndroidRuntime(992): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.igestao/com.example.igestao.Noticias}: java.lang.NullPointerException
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.os.Looper.loop(Looper.java:137)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-11 16:15:24.572: E/AndroidRuntime(992): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 16:15:24.572: E/AndroidRuntime(992): at java.lang.reflect.Method.invoke(Method.java:511)
12-11 16:15:24.572: E/AndroidRuntime(992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-11 16:15:24.572: E/AndroidRuntime(992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-11 16:15:24.572: E/AndroidRuntime(992): at dalvik.system.NativeStart.main(Native Method)
12-11 16:15:24.572: E/AndroidRuntime(992): Caused by: java.lang.NullPointerException
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.AlertDialog.<init>(AlertDialog.java:98)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.ProgressDialog.show(ProgressDialog.java:110)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.ProgressDialog.show(ProgressDialog.java:99)
12-11 16:15:24.572: E/AndroidRuntime(992): at com.example.igestao.Noticias$DownloadFilesTask.onPreExecute(Noticias.java:73)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.os.AsyncTask.execute(AsyncTask.java:511)
12-11 16:15:24.572: E/AndroidRuntime(992): at com.example.igestao.Noticias.onCreate(Noticias.java:58)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.Activity.performCreate(Activity.java:4465)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-11 16:15:24.572: E/AndroidRuntime(992): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-11 16:15:24.572: E/AndroidRuntime(992): ... 11 more
これは私が私のMainActivityで得たものです:
public class Noticias extends ListActivity {
static final String URL = "http://loc.grupolusofona.pt/index.php/?format=feed";
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_DESC = "description";
static final String KEY_LINK = "link";
static final String KEY_PUBDATE = "pubDate";
ProgressDialog progressDialog;
ArrayList<HashMap<String, Spanned>> menuItems = new ArrayList<HashMap<String, Spanned>>();
private DownloadFilesTask download;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_noticias);
download = new DownloadFilesTask(null);
download.execute();
}
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
private Context context;
public DownloadFilesTask (Context context){
this.context = context;
}
protected void onPreExecute() {
progressDialog = ProgressDialog.show(context, "Loading1", "Loading2", true);
}
protected Long doInBackground(URL... urls) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
String data[] = new String[nl.getLength()];
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, Spanned> map = new HashMap<String, Spanned>();
Element e = (Element) nl.item(i);
data[i] = getTagValue("pubDate", e);
SimpleDateFormat sdf = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss z", Locale.UK);
Date date = null;
try {
date = sdf.parse(data[i]);
String timeOfDay = new SimpleDateFormat("HH:mm").format(date);
java.sql.Timestamp timeStampDate = new Timestamp(date.getTime());
java.sql.Timestamp timeStampNow = new Timestamp((new java.util.Date()).getTime());
long secondDiff = timeStampNow.getTime() / 1000 - timeStampDate.getTime() / 1000;
int minuteDiff = (int) (secondDiff / 60);
int hourDiff = (int) (secondDiff / 3600);
int dayDiff = daysBetween(date, new Date());
if (dayDiff > 0) {
System.out.println("Há " + dayDiff + " dia(s), às " + timeOfDay);
data[i] = ("Há " + dayDiff + " dia(s), às " + timeOfDay);
} else if (hourDiff > 0) {
System.out.println("Há " + hourDiff + " hora(s), às " + timeOfDay);
data[i] = ("Há " + hourDiff + " hora(s), às " + timeOfDay);
} else if (minuteDiff > 0) {
System.out.println("Há " + minuteDiff + " minuto(s), às " + timeOfDay);
data[i] = ("Há " + minuteDiff + " minuto(s), às " + timeOfDay);
} else if (secondDiff > 0) {
System.out.println("Há " + secondDiff + " segundo(s), às " + timeOfDay);
data[i] = ("Há " + secondDiff + " segundo(s), às " + timeOfDay);
}
} catch (ParseException e1) {
e1.printStackTrace();
} catch (java.text.ParseException e1) {
e1.printStackTrace();
}
map.put(KEY_TITLE, Html.fromHtml(parser.getValue(e, KEY_TITLE)));
map.put(KEY_DESC, Html.fromHtml(parser.getValue(e, KEY_DESC)));
map.put(KEY_PUBDATE, Html.fromHtml(data[i]));
map.put(KEY_LINK, Html.fromHtml(parser.getValue(e, KEY_LINK)));
menuItems.add(map);
}//endFOR
return null;
}
protected void onPostExecute(Long result) {
progressDialog.dismiss();
adapter_listview();
//onClick gets URL from ListView and opens it
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String link = ((TextView) view.findViewById(R.id.link)).getText().toString();
Intent in = new Intent(Intent.ACTION_VIEW);
in.setData(Uri.parse(link));
startActivity(in);
}
});
}
}
private static String getTagValue(String sTag, Element e) {
NodeList nlList = e.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
public static int daysBetween(Date startDate, Date endDate) {
int daysBetween = 0;
while (startDate.before(endDate)) {
startDate.setTime(startDate.getTime() + 86400000);
daysBetween++;
}
return daysBetween;
}
public void adapter_listview(){
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.linhafeed,
new String[] { KEY_TITLE, KEY_DESC, KEY_PUBDATE, KEY_LINK },
new int[] { R.id.title, R.id.desc, R.id.pub, R.id.link });
setListAdapter(adapter);
}
}
誰かが私が間違っていることを教えてもらえますか?AsyncTaskのチュートリアルを受講しましたが、ここでは効果がありません。御時間ありがとうございます。