我正在使用Parse,并且我跟随tutorial创建了包含图片和文字的自定义listview
。我能够在listview中检索图像和文本。但是,当我单击listview项时,它会抛出空指针异常,而不是重定向到新活动并显示特定结果。我仍然不确定我可能在哪里出错。
异常消息:
FATAL EXCEPTION:
java.lang.RuntimeException: Unable to instantiate activityComponentInfo{com.androidbegin.parselistviewimgtxt/com.androidbegin.parselistviewimgtxt.SingleItemView}: java.lang.NullPointerException.
Caused by: java.lang.NullPointerException at com.androidbegin.parselistviewimgtxt.FileCache.<init>(FileCache.java:18)
at com.androidbegin.parselistviewimgtxt.ImageLoader.<init>(ImageLoader.java:35)
07-20 01:54:24.114: E/AndroidRuntime(1313): at com.androidbegin.parselistviewimgtxt.SingleItemView.<init>(SingleItemView.java:16)
以下是代码:
MainActivity.java
public class MainActivity extends Activity {
// Declare Variables
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ListViewAdapter adapter;
private List<WorldPopulation> worldpopulationlist = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
// Execute RemoteDataTask AsyncTask
new RemoteDataTask().execute();
}
// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
worldpopulationlist = new ArrayList<WorldPopulation>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"Country");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("ranknum");
ob = query.find();
for (ParseObject country : ob) {
// Locate images in flag column
ParseFile image = (ParseFile) country.get("flag");
WorldPopulation map = new WorldPopulation();
map.setRank((String) country.get("rank"));
map.setCountry((String) country.get("country"));
map.setPopulation((String) country.get("population"));
map.setFlag(image.getUrl());
worldpopulationlist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this,
worldpopulationlist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
SingleItemView.java
public class SingleItemView extends Activity {
// Declare Variables
String rank;
String country;
String population;
String flag;
String position;
ImageLoader imageLoader = new ImageLoader(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from singleitemview.xml
setContentView(R.layout.singleitemview);
// Locate the TextViews in singleitemview.xml
TextView txtrank = (TextView) findViewById(R.id.rank);
TextView txtcountry = (TextView) findViewById(R.id.country);
TextView txtpopulation = (TextView) findViewById(R.id.population);
// Locate the ImageView in singleitemview.xml
ImageView imgflag = (ImageView) findViewById(R.id.flag);
Intent i = getIntent();
// Get the result of rank
rank = i.getStringExtra("rank");
// Get the result of country
country = i.getStringExtra("country");
// Get the result of population
population = i.getStringExtra("population");
// Get the result of flag
flag = i.getStringExtra("flag");
// Set results to the TextViews
txtrank.setText(rank);
txtcountry.setText(country);
txtpopulation.setText(population);
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(flag, imgflag);
}
ListViewAdapter.java
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ImageLoader imageLoader;
private List<WorldPopulation> worldpopulationlist = null;
private ArrayList<WorldPopulation> arraylist;
public ListViewAdapter(Context context,
List<WorldPopulation> worldpopulationlist) {
this.context = context;
this.worldpopulationlist = worldpopulationlist;
inflater = LayoutInflater.from(context);
this.arraylist = new ArrayList<WorldPopulation>();
this.arraylist.addAll(worldpopulationlist);
imageLoader = new ImageLoader(context);
}
public class ViewHolder {
TextView rank;
TextView country;
TextView population;
ImageView flag;
}
@Override
public int getCount() {
return worldpopulationlist.size();
}
@Override
public Object getItem(int position) {
return worldpopulationlist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.listview_item, null);
// Locate the TextViews in listview_item.xml
holder.rank = (TextView) view.findViewById(R.id.rank);
holder.country = (TextView) view.findViewById(R.id.country);
holder.population = (TextView) view.findViewById(R.id.population);
// Locate the ImageView in listview_item.xml
holder.flag = (ImageView) view.findViewById(R.id.flag);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// Set the results into TextViews
holder.rank.setText(worldpopulationlist.get(position).getRank());
holder.country.setText(worldpopulationlist.get(position).getCountry());
holder.population.setText(worldpopulationlist.get(position)
.getPopulation());
// Set the results into ImageView
imageLoader.DisplayImage(worldpopulationlist.get(position).getFlag(),
holder.flag);
// Listen for ListView Item Click
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Send single item click data to SingleItemView Class
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.putExtra("rank",
(worldpopulationlist.get(position).getRank()));
// Pass all data country
intent.putExtra("country",
(worldpopulationlist.get(position).getCountry()));
// Pass all data population
intent.putExtra("population",
(worldpopulationlist.get(position).getPopulation()));
// Pass all data flag
intent.putExtra("flag",
(worldpopulationlist.get(position).getFlag()));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return view;
}
我试过
if (getIntent().getStringExtra("rank") != null && (getIntent().getStringExtra("country") != null)&& (getIntent().getStringExtra("population") != null)&& (getIntent().getStringExtra("flag") != null)){
rank = bundle.getString("rank");
// Get the result of country
country = bundle.getString("country");
// Get the result of population
population = bundle.getString("population");
// Get the result of flag
flag = bundle.getString("flag");
}
答案 0 :(得分:0)
我认为您应该将ImageLoader imageLoader
中的SingleItemView
初始化放到上面使用它onCreate
之前的imageLoader.DisplayImage(flag, imgflag);
方法中。
可能看起来像这样
public class SingleItemView extends Activity {
// Declare Variables
String rank;
String country;
String population;
String flag;
String position;
ImageLoader imageLoader;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from singleitemview.xml
setContentView(R.layout.singleitemview);
// Locate the TextViews in singleitemview.xml
TextView txtrank = (TextView) findViewById(R.id.rank);
TextView txtcountry = (TextView) findViewById(R.id.country);
TextView txtpopulation = (TextView) findViewById(R.id.population);
// Locate the ImageView in singleitemview.xml
ImageView imgflag = (ImageView) findViewById(R.id.flag);
Intent i = getIntent();
// Get the result of rank
rank = i.getStringExtra("rank");
// Get the result of country
country = i.getStringExtra("country");
// Get the result of population
population = i.getStringExtra("population");
// Get the result of flag
flag = i.getStringExtra("flag");
// Set results to the TextViews
txtrank.setText(rank);
txtcountry.setText(country);
txtpopulation.setText(population);
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader = new ImageLoader(this);
imageLoader.DisplayImage(flag, imgflag);
}
在onCreate
之前Activity
可能没有足够的信息ImageLoader
的构造函数需要。