我成功地解析并从xml文档中检索了我需要的所有数据。我有两个活动,一个活动显示主要数据,另一个活动显示recyclerView单击的项目的额外数据。我试图通过主活动的意图传递详细活动中所需的所有数据。但是我在模型类中缺少一个方法,该方法可以帮助我从列表中检索对象本身,以便将其发送到putExtra()中。那么该方法应该是什么样? ** logcat在详细信息活动中给我这个错误:“试图在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'”。
这里是我的主要活动
public class MainActivity extends AppCompatActivity implements AdsAdapterOnClickHandler {
public static final String URL = "MYURL;
public static List<AdEntry> entryList = new ArrayList<>();
// Declaration
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting the recyclerView and the adapter
loadPage();
}
public void loadPage() {
new DownloadXmlTask().execute(URL);
System.out.print("Result" + "Succeed");
}
private class DownloadXmlTask extends AsyncTask<String, Void, List<AdEntry>> {
@Override
protected List<AdEntry> doInBackground(String... urls) {
try {
return loadXmlFromNetwork(urls[0]);
} catch (IOException e) {
return null;
} catch (XmlPullParserException e) {
return null;
}
}
@Override
protected void onPostExecute(List<AdEntry> adEntries) {
Toast.makeText(MainActivity.this, "Successfully Processed", Toast.LENGTH_SHORT).show();
mAdsAdapter.setAdData(adEntries);
super.onPostExecute(adEntries);
}
}
public List<AdEntry> loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
InputStream stream = null;
try {
stream = downloadUrl(urlString);
entryList = parse(stream);
} finally {
if (stream != null)
stream.close();
}
return entryList;
}
public InputStream downloadUrl(String urlString) throws IOException {
// Open HttpURLConnection
}
@Override
public void onClick(View view, int position) {
final AdEntry entry = entryList.get(position);
Class destinationClass = DetailActivity.class;
Intent intentToStartDetailActivity = new Intent(this, destinationClass);
intentToStartDetailActivity.putExtra("Product Name", entry.productName);
intentToStartDetailActivity.putExtra("Product Thumbnail", entry.productThumbnail);
intentToStartDetailActivity.putExtra("Product Description", entry.productDescription);
intentToStartDetailActivity.putExtra("Product AppId", entry.appId);
intentToStartDetailActivity.putExtra("Product CallToAction", entry.callToAction);
intentToStartDetailActivity.putExtra("Product Link", entry.proxyLink);
intentToStartDetailActivity.putExtra("Product ImageRating", entry.averageRatingImageURL);
intentToStartDetailActivity.putExtra("Product Category", entry.productCategory);
intentToStartDetailActivity.putExtra("Product Rating", entry.productRating);
intentToStartDetailActivity.putExtra("Product NumberOfRating", entry.numberOfRatings);
startActivity(intentToStartDetailActivity);
}
}
这是我的DetailActivity:
public class DetailActivity extends AppCompatActivity {
// Views Declaration
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// findViewsById
retrieveData();
}
public void retrieveData() {
String productName = getIntent().getStringExtra("Product Name");
String productThumbnail = getIntent().getStringExtra("Product Thumbnail");
String productDescription = getIntent().getStringExtra("Product Description");
String productAppId = getIntent().getStringExtra("Product AppId");
String productCall = getIntent().getStringExtra("Product CallToAction");
String productLink = getIntent().getStringExtra("Product Link");
String productImageRating = getIntent().getStringExtra("Product ImageRating");
String productCategory = getIntent().getStringExtra("Product Category");
String productNumberOfRatings = getIntent().getStringExtra("Product NumberOfRating");
Picasso.get().load(productThumbnail).into(thumbnail_imageView);
name_textView.setText(productName);
description_textView.setText(productDescription);
numberOfRating_textView.setText(productNumberOfRatings);
link_textView.setText(productLink);
category_textView.setText(productCategory);
rating_textView.setText(productImageRating);
}
}
这是我的ModelClass:
public class AdEntry {
private List<AdEntry> entries;
public String appId;
public String averageRatingImageURL;
public String callToAction;
public String productCategory;
public String proxyLink;
public String productDescription;
public String numberOfRatings;
public String productName;
public String productThumbnail;
public String productRating;
public AdEntry() {
}
public AdEntry(String appId, String averageRatingImageURL, String callToAction, String productCategory, String proxyLink,
String productDescription, String numberOfRatings, String productName, String productThumbnail, String productRating) {
this.appId = appId;
this.averageRatingImageURL = averageRatingImageURL;
this.callToAction = callToAction;
this.productCategory = productCategory;
this.proxyLink = proxyLink;
this.productDescription = productDescription;
this.numberOfRatings = numberOfRatings;
this.productName = productName;
this.productThumbnail = productThumbnail;
this.productRating = productRating;
}
public String getAppId() {
return appId;
}
public String getAverageRatingImageURL() {
return averageRatingImageURL;
}
public String getCallToAction() {
return callToAction;
}
public String getProductCategory() {
return productCategory;
}
public String getProxyLink() {
return proxyLink;
}
public String getProductDescription() {
return productDescription;
}
public String getNumberOfRatings() {
return numberOfRatings;
}
public String getProductName() {
return productName;
}
public String getProductThumbnail() {
return productThumbnail;
}
public String getProductRating() {
return productRating;
}
}
这是我在XMLPUllParser类中检索列表的部分:
private static List readAds(XmlPullParser parser) throws XmlPullParserException, IOException {
List<AdEntry> entries = new ArrayList();
parser.require(XmlPullParser.START_TAG, ns, "ads");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG)
continue;
String name = parser.getName();
if (name.equals("ad"))
entries.add(readEntry(parser));
else
skip(parser);
}
System.out.println("entries" + entries);
return entries;
}
private static AdEntry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "ad");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG)
continue;
String name = parser.getName();
switch (name) {
case "appId":
appId = readAppId(parser);
System.out.println("appId" + appId);
break;
case "averageRatingImageURL":
averageRatingImageURL = readRatingImage(parser);
System.out.println("rating" + averageRatingImageURL);
break;
case "callToAction":
callToAction = readCallToAction(parser);
System.out.println("callToAction" + callToAction);
break;
case "categoryName":
categoryName = readCategoryName(parser);
System.out.println("categoryName" + categoryName);
break;
case "clickProxyURL":
clickProxyURL = readProxyLink(parser);
System.out.println("clickProxyURL" + clickProxyURL);
break;
case "productDescription":
productDescription = readProductDescription(parser);
System.out.println("productDescription" + productDescription);
break;
case "numberOfRatings":
numberOfRatings = readNumberOfRatings(parser);
System.out.println("numberOfRatings" + numberOfRatings);
break;
case "productName":
productName = readNameProduct(parser);
System.out.println("productName" + productName);
break;
case "productThumbnail":
productThumbnail = readThumbnail(parser);
System.out.println("productThumbnail" + productThumbnail);
break;
case "rating":
rating = readRating(parser);
System.out.println("rating" + rating);
break;
default:
skip(parser);
break;
}
}
return new AdEntry(appId, averageRatingImageURL, callToAction, categoryName, clickProxyURL, productDescription,
numberOfRatings, productName, productThumbnail, rating);
}