我正在尝试从狗品种网站解析数据,但我在此活动中遇到运行时错误,这是我的第一个活动。有人知道我做错了什么或者能说出我的错误吗?
谢谢
Unable to start activity ComponentInfo{com.example.shannon.popular/com.example.shannon.popular.DogsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ' com.example.shannon.popular.Breed$Name com.example.shannon.popular.Breed.getName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
Dog Activity.class
public class DogsActivity extends Activity {
private Breed breed;
private Dogs dogs;
private DogAdapter dogAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dogs = new Dogs();
Intent intent = getIntent();
breed = (Breed) intent.getSerializableExtra("breed");
Breed.Name breedName = breed.getName();
String url = breed.getURL();
//todo remove hard code page number
if (breedName == Breed.Name.MOST_POPULAR) {
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url + "list_1_2.html");
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url + "list_1_3.html");
} else if (breedName == Breed.Name.HERDING_BREED) {
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url + "list_11_2.html");
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url + "list_11_3.html");
} else if (breedName == Breed.Name.WORKING_BREED) {
//todo remove hard code urls
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
"http://www.dogbreedslist.info/all-dog-breeds/Boxer.html#.Vh0cnRNViko");
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
"http://www.dogbreedslist.info/all-dog-breeds/Chinook-dog.html#.Vh0cvBNViko");
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
"http://www.dogbreedslist.info/all-dog-breeds/Rottweiler.html#.Vh0c0xNViko");
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
"http://www.dogbreedslist.info/all-dog-breeds/Kuvasz.html#.Vh0c6BNViko");
new RetrieveDogsTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
"http://www.dogbreedslist.info/all-dog-breeds/Argentine-Dogo.html#.Vh0dARNViko");
} else {
new RetrieveDogsTask().execute(url);
}
setContentView(R.layout.activity_dogs);
GridView gridView = (GridView) findViewById(R.id.dogsGridView);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int thumbnail_width;
int thumbnail_height;
if (breedName == Breed.Name.MOST_POPULAR) {
thumbnail_width = (int) getResources().getDimension(R.dimen.first_team_thumbnail_width);
thumbnail_height = (int) getResources().getDimension(R.dimen.first_team_thumbnail_height);
gridView.setColumnWidth(thumbnail_width);
int n = width / thumbnail_width * height / thumbnail_height;
dogs.fillEmptyData(n);
} else if (breedName ==Breed.Name.WORKING_BREED) {
gridView.setColumnWidth((int) getResources().getDimension(R.dimen.coach_thumbnail_width));
} else {
thumbnail_width = (int) getResources().getDimension(R.dimen.reserve_thumbnail_box_width);
thumbnail_height = (int) getResources().getDimension(R.dimen.reserve_thumbnail_height);
gridView.setColumnWidth(thumbnail_width);
int n = width / thumbnail_width * height / thumbnail_height;
dogs.fillEmptyData(n);
}
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Dog dog = dogs.get(position);
Toast.makeText(getApplicationContext(), dog.getName(),
Toast.LENGTH_SHORT).show();
Intent dogProfileIntent = new Intent(DogsActivity.this,
DogProfileActivity.class);
Bundle dogBundle = new Bundle();
dogBundle.putSerializable("dog", dog);
dogProfileIntent.putExtras(dogBundle);
startActivity(dogProfileIntent);
}
});
dogAdapter = new DogAdapter
(DogsActivity.this, R.layout.activity_dogs, dogs, breedName);
gridView.setAdapter(dogAdapter);
setTitle(breed.getNameString(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.breeds, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.website) {
Intent intent = new Intent(DogsActivity.this,
WebViewActivity.class);
intent.putExtra("url", breed.getURL());
intent.putExtra("title", breed.getNameString(this));
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
private class RetrieveDogsTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... urls) {
for (String url : urls) {
Parser parser = new Parser(url, DogsActivity.this);
Breed.Name breedName = breed.getName();
if (breedName == Breed.Name.WORKING_BREED) {
dogs.add(parser.parseProfile(new Dog(url, breedName)));
} else {
dogs.addAll(parser.parseDogsPage(breedName, DogsActivity.this));
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dogAdapter.notifyDataSetChanged();
}
}
这是我的数据:
package com.example.shannon.popular;
import java.io.Serializable;
import java.util.ArrayList;
class Dog implements Serializable {
private String name;
private String origin;
private String lifeSpan;
private String sizeType;
private String otherNames;
private ArrayList<Detail> details;
private String thumbnailURL;
private String mainImageURL;
private String url;
private String articleText = "";
private Breed.Name breed;
//Like a lock in multi threading
private boolean basicDataReady = false;
private boolean detailDataReady = false;
Dog(String name, String sizeType, String thumbnailURL, String url, Breed.Name breed) {
this.name = name;
this.sizeType = sizeType;
this.thumbnailURL = thumbnailURL;
this.url = url;
this.breed = breed;
basicDataReady = true;
}
Dog(String name, String origin, String lifeSpan, String url, String thumbnailURL, Breed.Name breed) {
this.name = name;
this.origin = origin;
this.lifeSpan = lifeSpan;
this.url = url;
this.thumbnailURL = thumbnailURL;
this.breed = breed;
basicDataReady = true;
}
Dog(String name, String url, String thumbnailURL, Breed.Name breed) {
this.name = name;
this.url = url;
this.thumbnailURL = thumbnailURL;
this.breed = breed;
basicDataReady = true;
}
Dog(String url, Breed.Name breed) {
this.url = url;
this.breed = breed;
}
Dog() {
}
public String getArticleText() {
return articleText;
}
public void setArticleText(String articleText) {
this.articleText = articleText;
}
public String getOrigin() {
return origin;
}
public String getLifeSpan() {
return lifeSpan;
}
public String getSizeType() {
return sizeType;
}
public boolean isBasicDataReady() {
return basicDataReady;
}
public void setBasicDataReady(boolean basicDataReady) {
this.basicDataReady = basicDataReady;
}
public boolean isDetailDataReady() {
return detailDataReady;
}
public void setDetailDataReady(boolean detailDataReady) {
this.detailDataReady = detailDataReady;
}
public Breed.Name getBreed() {
return breed;
}
public void setBreed(Breed.Name breed) {
this.breed = breed;
}
public String getMainImageURL() {
return mainImageURL;
}
public void setMainImageURL(String mainImageURL) {
this.mainImageURL = mainImageURL;
}
public ArrayList<Detail> getDetails() {
return details;
}
public void setDetails(ArrayList<Detail> details) {
this.details = details;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOtherNames() {
return otherNames;
}
public void setOtherNames(String otherNames) {
this.otherNames = otherNames;
}
public String getThumbnailURL() {
return thumbnailURL;
}
public void setThumbnailURL(String thumbnailURL) {
this.thumbnailURL = thumbnailURL;
}
public String getUrl() {
return url;
}
static class Detail implements Serializable {
private final String key;
private final String value;
public Detail(String key, String value) {
this.key = key;
this.value = value;
}
public String getValue() {
return value;
}
public String getKey() {
return key;
}
}
}