我是android新手.我正在试着测试rss阅读器,我收到了这个错误消息
由: android.app.SuperNotCalledException:活动引起
Error logcat在这里:
11-27 07:08:04.192: E/AndroidRuntime(1471): FATAL EXCEPTION: AsyncTask #1
11-27 07:08:04.192: E/AndroidRuntime(1471): java.lang.RuntimeException: An error occured while executing doInBackground()
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.os.AsyncTask$3.done(AsyncTask.java:278)
11-27 07:08:04.192: E/AndroidRuntime(1471): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-27 07:08:04.192: E/AndroidRuntime(1471): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-27 07:08:04.192: E/AndroidRuntime(1471): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-27 07:08:04.192: E/AndroidRuntime(1471): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-27 07:08:04.192: E/AndroidRuntime(1471): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-27 07:08:04.192: E/AndroidRuntime(1471): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-27 07:08:04.192: E/AndroidRuntime(1471): at java.lang.Thread.run(Thread.java:856)
11-27 07:08:04.192: E/AndroidRuntime(1471): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:3939)
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:701)
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.view.View.requestLayout(View.java:12555)
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.view.View.requestLayout(View.java:12555)
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.view.View.requestLayout(View.java:12555)
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.view.View.requestLayout(View.java:12555)
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.view.View.requestLayout(View.java:12555)
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.widget.AbsListView.requestLayout(AbsListView.java:1690)
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.widget.ListView.setAdapter(ListView.java:488)
11-27 07:08:04.192: E/AndroidRuntime(1471): at com.example.testrss.ConnectToServer.doInBackground(ConnectToServer.java:78)
11-27 07:08:04.192: E/AndroidRuntime(1471): at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-27 07:08:04.192: E/AndroidRuntime(1471): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)以下是NPRNewsDetails.java代码:
package com.example.testrss;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class NPRNewsDetails extends Activity {
// a main category subject has already been selected by the user
// (data <"urlCaption", "urlAddress"> comes in a bundle sent
// by main, access web-feed and show corresponding headlines
ArrayList<SingleNewsItem> newsList = new ArrayList<SingleNewsItem>();
ArrayAdapter<String> aa;
ListView myListView;
String urlAddress = "";
String urlCaption = "";
SingleNewsItem selectedNewsItem;
Context context = getApplication();
ConnectToServer cNTS;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = (ListView) this.findViewById(R.id.myListView);
// create a local Intent handler (needed to process input parameters)
Intent myLocalIntent = getIntent();
// grab the data bundle with all the pieces sent to us
// it contains 1. url-address and 2. caption-text
Bundle myBundle = myLocalIntent.getExtras();
urlAddress = myBundle.getString("urlAddress");
urlCaption = myBundle.getString("urlCaption");
System.out.println("urlAddress ####" + urlAddress); // value from
// MainActivity
// http://www.npr.org/rss/rss.php?id=1007
System.out.println("urlCaption####" + urlCaption); // Test RSS
// top caption for this screen
String todayStr = MainActivity.niceDate();
this.setTitle("NPR -" + urlCaption + " \t" + todayStr);
// clicking a line shows more about selected news item
myListView = (ListView) this.findViewById(R.id.myListView);
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> _av, View _v, int _index,
long _id) {
// TODO Auto-generated method stub
System.out.println("item click index"+_index);
selectedNewsItem = newsList.get(_index);
showNiceDialogBox(selectedNewsItem, context);
}
});
}// onCreate
@Override
protected void onResume() {
super.onResume();
cNTS = new ConnectToServer(NPRNewsDetails.this,
myListView, newsList);
Log.i("onResum", "I'm Resume");
cNTS.execute(urlAddress);
Log.i("onResum", "I'm Resume");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
}
public void showNiceDialogBox(SingleNewsItem selectedNewsItem,
Context context) {
// assemble a nice looking dialog box
try {
final Uri myLink = Uri.parse(selectedNewsItem.getLink());
AlertDialog.Builder myBuilder = new AlertDialog.Builder(this);
myBuilder
.setIcon(R.drawable.ic_launcher)
.setTitle(urlCaption)
.setMessage(
selectedNewsItem.getTitle() + "\n\n"
+ selectedNewsItem.getDescription() + "\n")
.setPositiveButton("Close", null)
.setNegativeButton("More", new OnClickListener() {
public void onClick(DialogInterface dialog, int whichOne) {
// use native web browsing
Intent webIntent = new Intent(Intent.ACTION_VIEW,
myLink);
startActivity(webIntent);
}
})// setNegativeButton
.create();
myBuilder.show();
} catch (Exception e) {
e.printStackTrace();
}
}// showNiceDialogBox
}这是ConnectToServer.java代码:
package com.example.testrss;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ConnectToServer extends AsyncTask
{
ListView myListView;
Context context;
ArrayList<SingleNewsItem> newsList = new ArrayList<SingleNewsItem>();
public ConnectToServer(Context context, ListView myListView, ArrayList<SingleNewsItem> newsList)
{
this.context = context;
this.myListView = myListView;
this.newsList.addAll(newsList);
}
@Override
protected Object doInBackground(Object... params)
{
try
{
String urlAddress = (String) params[0];
URL url = new URL(urlAddress);
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
// NodeListnl= docEle.getElementsByTagName("entry");
NodeList nl = docEle.getElementsByTagName("item");
if ((nl != null) && (nl.getLength() > 0)) {
for (int i = 0; i < nl.getLength(); i++) {
dissectNode(nl, i);
}// for
}// if
}// if
int layoutID = R.layout.my_simple_list_item;
ArrayAdapter<SingleNewsItem> aaNews = new ArrayAdapter<SingleNewsItem>(this.context, layoutID, newsList);
myListView.setAdapter(aaNews);
}
catch (MalformedURLException e) {
System.out.println("Malformed error");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IO error");
e.printStackTrace();
} catch (ParserConfigurationException e) {
System.out.println("Parser error");
e.printStackTrace();
} catch (SAXException e) {
System.out.println("SAX Error");
e.printStackTrace();
}
return null;
}
public void dissectNode(NodeList nl, int i) {
try {
Element entry = (Element) nl.item(i);
Element title = (Element) entry.getElementsByTagName("title").item(
0);
Element description = (Element) entry.getElementsByTagName(
"description").item(0);
Element pubDate = (Element) entry.getElementsByTagName("pubDate")
.item(0);
Element link = (Element) entry.getElementsByTagName("link").item(0);
String titleValue = title.getFirstChild().getNodeValue();
String descriptionValue = description.getFirstChild()
.getNodeValue();
String dateValue = pubDate.getFirstChild().getNodeValue();
String linkValue = link.getFirstChild().getNodeValue();
SingleNewsItem singleItem = new SingleNewsItem(dateValue,
titleValue, descriptionValue, linkValue);
newsList.add(singleItem);
} catch (DOMException e) {
e.printStackTrace();
}
}// dissectNode
@Override
protected void onPostExecute(Object result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}发布于 2013-11-27 03:28:30
在onResume()方法中,需要确保调用super.onResume()
这通常是一个很好的实践,让它成为您的onResume()方法的第一行,这样您就知道它将被调用。
https://stackoverflow.com/questions/20233335
复制相似问题