我已对代码进行了更改,因为我在this.setContentView(sv);
的开头给出了onCreate()
,但是只有在执行try catch之后才会加载视图。是否真的可以在执行try catch之前显示视图?
public class viewstoryActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Create a new layout to display the view */
ScrollView sv = new ScrollView(this);
LinearLayout layout1 = new LinearLayout(this);
layout1.setOrientation(LinearLayout.VERTICAL);
this.setContentView(sv);
/** Create a new textview array to display the results */
try {
LinearLayout layout[];
TextView name[];
ImageView website[];
TextView category[];
URL url = new URL("http://gdata.youtube.com/feeds/api/videos?start-index=1&max-results=25&vq=itsaperfectstory&orderby=relevance");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
NodeList nodeList = doc.getElementsByTagName("entry");
/** Assign textview array lenght by arraylist size */
layout = new LinearLayout[nodeList.getLength()];
name = new TextView[nodeList.getLength()];
website = new ImageView[nodeList.getLength()];
category = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
layout[i]= new LinearLayout(this);
layout[i].setOrientation(LinearLayout.VERTICAL);
layout[i].setBackgroundResource(R.drawable.allicon_bg);
layout[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,150));
name[i] = new TextView(this);
website[i] = new ImageView(this);
category[i] = new TextView(this);
name[i].setPadding(50, 20,0,0);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("title");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
name[i].setText(((Node) nameList.item(0)).getNodeValue());
NodeList websiteList = fstElmnt.getElementsByTagName("media:content");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
final String url1 = websiteElement.getAttribute("url");
NodeList websiteList1 = fstElmnt.getElementsByTagName("media:thumbnail");
Element websiteElement1 = (Element) websiteList1.item(1);
String test=websiteElement1.getAttribute("url");
Bitmap bm = null;
URL aURL = new URL(test);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close(); // TODO: handle exception
website[i].setImageBitmap(bm);
website[i].setMaxWidth(50);
website[i].setPadding(380, 0, 0, 0);
layout[i].addView(name[i]);
layout[i].addView(website[i]);
layout1.addView(layout[i]);
layout[i].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bundle bundle = new Bundle();
// add data to bundle
bundle.putString("url", url1);
Intent intent=new Intent(viewstoryActivity.this, VidActivity.class);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
overridePendingTransition (R.anim.right_slide_out, R.anim.right_slide_out);
}
});
}
} catch (Exception e) {
System.out.println("XML Parsing Exception = " + e);
}
/** Set the layout view to display */
sv.addView(layout1);
}
}
答案 0 :(得分:2)
在分析了您的onCreate()
后,很明显您的活动需要一段时间才能展示。您的所有UI都是动态设计和添加的,必须(根据您的代码)等待数据加载并进行解析和组织。换句话说,您通过数据处理阻止了UI。
考虑将数据加载移至AsyncTask
。还可以考虑在数据之前设计/加载UI 。这将允许您的用户看到某些内容,即使它不完整。您始终可以在onCreate()
之后添加视图或更改显示属性。它也会减少对UI滞后的感知。
答案 1 :(得分:0)
建议将big try块移到另一个线程,不要阻止ui。
答案 2 :(得分:0)
正如其他人所说,将你的工作转移到AsyncTask中,这样它就不会阻塞主线程。如果你有兴趣了解到究竟有什么需要,那么我建议花点时间阅读TraceView - 这是一个很好的分析工具。