以下代码解析来自xml Web服务的数据,并应将所选项目的数据从列表视图传递到下一个活动。
代码运行良好,直到我点击列表项将数据传递到下一个活动,然后应用程序崩溃。
我已经尝试了许多修复来解决问题,比如删除setcontentView但仍然相同。
public class LastActivity extends ListActivity {
/** Called when the activity is first created. */
ListView lv;
static String UName="name";
static String UniUrl="description";
public String val;
static List<String> links;
static List<String> names;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv=(ListView) findViewById(R.id.list1);
links=new ArrayList<String>();
names=new ArrayList<String>();
try{
URL url=new URL("http://api.androidhive.info/pizza/?format=xml");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase(UName)) {
if (insideItem)
names.add(xpp.nextText()); //extract the headline
// val=xpp.nextText();
} else if (xpp.getName().equalsIgnoreCase(UniUrl)) {
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
}catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e)
{
e.printStackTrace();
}
/*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
setListAdapter(adapter);*/
lv = getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, names);
setListAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
String bb=parent.getItemAtPosition(position).toString();
Intent i = new Intent(getApplicationContext(), Details.class);
// String x=edt.getText().toString();
i.putExtra("name",bb);
// starting new activity
startActivity(i);
}
});
}
private InputStream getInputStream(URL url) {
// TODO Auto-generated method stub
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
}
//detail activity:
public class Details extends LastActivity{
LastActivity last=new LastActivity();
TextView tv;
TextView url;
String read;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
tv=(TextView) findViewById(R.id.text);
//url=(TextView) findViewById(R.id.link);
Intent intent=getIntent();
// receiving data
String name = intent.getStringExtra("name");
//String path=intent.getStringExtra("url");
//Log.e("Second Screen", name + ".");
//tv.setText(name);
// url.setText(path);
}
}
答案 0 :(得分:0)
在详细信息活动中扩展Activity
而不是LastActivity
public class Details extends Activity{
//LastActivity last=new LastActivity();
.....