我有一个我在一个活动中解析过的Rss Feed。每个项目都附有一个单独的URL。当在列表视图中单击某个项目时,它将打开另一个活动,显示我从正在解析的xml中定义的特定文本。我在该视图中有一个按钮,我想从该项目的xml文件中分配url。我唯一无法弄清楚的是如何将url信息串到该按钮。这是我正在使用的代码。
xml解析活动
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
static final String URL = "http://www.cpcofc.org/devoapp.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "item";
static final String KEY_NAME = "title";
static final String KEY_COST = "description";
static final String KEY_DESC = "description";
static final String KEY_GUID = "guid";
static final String KEY_LINK = "link";
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
map.put(KEY_GUID, parser.getValue(e, KEY_GUID));
map.put(KEY_LINK, parser.getValue(e,KEY_LINK));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_DESC, KEY_NAME, KEY_COST, KEY_GUID}, new int[] {
R.id.desciption, R.id.name, R.id.cost});
setListAdapter(adapter);
Collections.reverse(menuItems);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
Uri uriUrl = Uri.parse(menuItems.get(position).get(KEY_GUID));
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
in.putExtra(KEY_GUID, uriUrl);
startActivity(in);
}
});
}
}
具有按钮以查看其中的网址的第二个活动
public class SingleMenuItemActivity extends Activity {
// XML node keys
static final String KEY_NAME = "title";
static final String KEY_COST = "description";
static final String KEY_DESC = "description";
static final String KEY_GUID = "guid";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
final Intent in = getIntent();
// Get XML values from previous intent
String name = in.getStringExtra(KEY_NAME);
String cost = in.getStringExtra(KEY_DESC);
String description = in.getStringExtra(KEY_DESC);
String uriUrl = in.getStringExtra(KEY_GUID);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.cost_label);
TextView lblDesc = (TextView) findViewById(R.id.description_label);
lblName.setText(name);
lblCost.setText(cost);
lblDesc.setText(description);
Button devo = (Button) findViewById(R.id.button1);
devo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
}
我认为我的问题在于代码的这一部分
Button devo = (Button) findViewById(R.id.button1);
devo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
但我不确定该放什么
String url = "what do i put here???";
将上一屏幕中点击的项目中的网址字符串
这是第一个将xml解析为listview
的屏幕
单击列表视图中的项目时会发生以下情况
log cat
01-13 15:14:31.818: E/AndroidRuntime(8665): FATAL EXCEPTION: main
01-13 15:14:31.818: E/AndroidRuntime(8665): java.lang.NullPointerException: uriString
01-13 15:14:31.818: E/AndroidRuntime(8665): at android.net.Uri$StringUri.<init>(Uri.java:420)
01-13 15:14:31.818: E/AndroidRuntime(8665): at android.net.Uri$StringUri.<init>(Uri.java:410)
01-13 15:14:31.818: E/AndroidRuntime(8665): at android.net.Uri.parse(Uri.java:382)
01-13 15:14:31.818: E/AndroidRuntime(8665): at com.androidhive.xmlparsing.SingleMenuItemActivity$1.onClick(SingleMenuItemActivity.java:51)
01-13 15:14:31.818: E/AndroidRuntime(8665): at android.view.View.performClick(View.java:2532)
01-13 15:14:31.818: E/AndroidRuntime(8665): at android.view.View$PerformClick.run(View.java:9293)
01-13 15:14:31.818: E/AndroidRuntime(8665): at android.os.Handler.handleCallback(Handler.java:587)
01-13 15:14:31.818: E/AndroidRuntime(8665): at android.os.Handler.dispatchMessage(Handler.java:92)
01-13 15:14:31.818: E/AndroidRuntime(8665): at android.os.Looper.loop(Looper.java:150)
01-13 15:14:31.818: E/AndroidRuntime(8665): at android.app.ActivityThread.main(ActivityThread.java:4263)
01-13 15:14:31.818: E/AndroidRuntime(8665): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 15:14:31.818: E/AndroidRuntime(8665): at java.lang.reflect.Method.invoke(Method.java:507)
01-13 15:14:31.818: E/AndroidRuntime(8665): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-13 15:14:31.818: E/AndroidRuntime(8665): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-13 15:14:31.818: E/AndroidRuntime(8665): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您正在尝试将Uri对象传递给Bundle,String表示就足够了 EDIT 2p>
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
Uri uriUrl = Uri.parse(menuItems.get(position).get(KEY_GUID));
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
// **NEED TO PASS STRING OBJECT NOT URI OBJECT**
in.putExtra(KEY_GUID, uriUrl.toString());
startActivity(in);
}
});
编辑:
public class SingleMenuItemActivity extends Activity {
// XML node keys
static final String KEY_NAME = "title";
static final String KEY_COST = "description";
static final String KEY_DESC = "description";
static final String KEY_GUID = "guid";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
final Intent in = getIntent();
// Get XML values from previous intent
String name = in.getStringExtra(KEY_NAME);
String cost = in.getStringExtra(KEY_DESC);
String description = in.getStringExtra(KEY_DESC);
final String uriUrl = in.getStringExtra(KEY_GUID);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.cost_label);
TextView lblDesc = (TextView) findViewById(R.id.description_label);
lblName.setText(name);
lblCost.setText(cost);
lblDesc.setText(description);
Button devo = (Button) findViewById(R.id.button1);
devo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(uriUrl));
startActivity(i);
}
});
}
}
或强>
public class SingleMenuItemActivity extends Activity {
// XML node keys
static final String KEY_NAME = "title";
static final String KEY_COST = "description";
static final String KEY_DESC = "description";
static final String KEY_GUID = "guid";
private String url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
final Intent in = getIntent();
// Get XML values from previous intent
String name = in.getStringExtra(KEY_NAME);
String cost = in.getStringExtra(KEY_DESC);
String description = in.getStringExtra(KEY_DESC);
url= in.getStringExtra(KEY_GUID);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.cost_label);
TextView lblDesc = (TextView) findViewById(R.id.description_label);
lblName.setText(name);
lblCost.setText(cost);
lblDesc.setText(description);
Button devo = (Button) findViewById(R.id.button1);
devo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}
}