我通常可以处理logcat上出现的错误,但是这个logcat错误让我觉得非常糟糕,错误导致我的应用程序崩溃,启动onPreExecute方法并且列表显示为加载,但是列表永远不会出现和崩溃。我已经在Asynctask中有很多代码了。我很感谢你的时间,我对Android编程很陌生:
11-30 11:48:56.126: E/AndroidRuntime(1045): FATAL EXCEPTION: main
11-30 11:48:56.126: E/AndroidRuntime(1045): Process: com.example.clinicbooker, PID: 1045
11-30 11:48:56.126: E/AndroidRuntime(1045): java.lang.NullPointerException
11-30 11:48:56.126: E/AndroidRuntime(1045): at com.example.clinicbooker.LazyAdapter.getView(LazyAdapter.java:54)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.widget.ListView.onMeasure(ListView.java:1175)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.View.measure(View.java:16458)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.View.measure(View.java:16458)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.View.measure(View.java:16458)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-30 11:48:56.126: E/AndroidRuntime(1045): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.View.measure(View.java:16458)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-30 11:48:56.126: E/AndroidRuntime(1045): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.View.measure(View.java:16458)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.os.Handler.handleCallback(Handler.java:733)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.os.Handler.dispatchMessage(Handler.java:95)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.os.Looper.loop(Looper.java:137)
11-30 11:48:56.126: E/AndroidRuntime(1045): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-30 11:48:56.126: E/AndroidRuntime(1045): at java.lang.reflect.Method.invokeNative(Native Method)
11-30 11:48:56.126: E/AndroidRuntime(1045): at java.lang.reflect.Method.invoke(Method.java:515)
11-30 11:48:56.126: E/AndroidRuntime(1045): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-30 11:48:56.126: E/AndroidRuntime(1045): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-30 11:48:56.126: E/AndroidRuntime(1045): at dalvik.system.NativeStart.main(Native Method)
更具体地说,这个logcat在第54行的Adapter类中指向一个错误:那就是TextView id =(TextView)vi.findViewById(R.id.id);
适配器类如下所示:
public class LazyAdapter extends BaseAdapter {
Context context;
HashMap<String, String> resultp = new HashMap<String, String>();
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyAdapter(Context contextActivity , ArrayList<HashMap<String, String>> d) {
this.context=contextActivity;
data=d;
imageLoader=new ImageLoader(context);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView=inflater.inflate(R.layout.book_list_row,parent,false);
resultp=data.get(position);
TextView id=(TextView)vi.findViewById(R.id.id); //NPE ERROR POINTS HERE
TextView title = (TextView)vi.findViewById(R.id.menu_name);
TextView description = (TextView)vi.findViewById(R.id.address);
TextView bookingDate = (TextView)vi.findViewById(R.id.book_date);
TextView bookingTime = (TextView)vi.findViewById(R.id.book_time);
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
// Setting all values in listview
id.setText(resultp.get(BookScreen.KEY_ID));
title.setText(resultp.get(BookScreen.KEY_TITLE));
description.setText(resultp.get(BookScreen.KEY_ADDRESS));
bookingDate.setText(resultp.get(BookScreen.KEY_DATE));
bookingTime.setText(resultp.get(BookScreen.KEY_TIME));
imageLoader.DisplayImage(resultp.get(BookScreen.KEY_THUMB_URL), thumb_image);
itemView.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
resultp=data.get(position);
Intent intent=new Intent(context,SingleItemView.class);
intent.putExtra("id",resultp.get(BookScreen.KEY_ID));
intent.putExtra("title",resultp.get(BookScreen.KEY_TITLE));
intent.putExtra("address",resultp.get(BookScreen.KEY_ADDRESS));
intent.putExtra("date",resultp.get(BookScreen.KEY_DATE));
intent.putExtra("time",resultp.get(BookScreen.KEY_TIME));
intent.putExtra("thumbnail",resultp.get(BookScreen.KEY_THUMB_URL));
context.startActivity(intent);
}
});
return vi;
}
}
这些错误意味着什么:AbsListView,ListView,LinearLayout,FrameLayout。我试图解析的xml文件与我的代码不兼容吗?
我的主要活动:
public class BookScreen extends Activity {
//array
ArrayList<HashMap<String, String>> songsList;
ProgressDialog mProgressDialog;
// All static variables
static final String URL = "https://dl.dropboxusercontent.com/u/42241589/test.xml";
// XML node keys
static final String KEY_CLINIC = "clinic"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_TIME = "time";
static final String KEY_ADDRESS = "address";
static final String KEY_THUMB_URL = "thumbnail";
ListView list;
LazyAdapter adapter;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_main);
new DownloadXML().execute();
}
private class DownloadXML extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(BookScreen.this);
mProgressDialog.setTitle("List is loading");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
protected Void doInBackground(Void... params) {
songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
try {
NodeList nl = doc.getElementsByTagName(KEY_CLINIC); //define parent node
// looping through all song nodes <song>
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_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_TIME, parser.getValue(e, KEY_TIME));
map.put(KEY_ADDRESS, parser.getValue(e, KEY_ADDRESS));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args) {
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter = new LazyAdapter(BookScreen.this, songsList);
list.setAdapter(adapter);
//Close dialog
mProgressDialog.dismiss();
}
}
}
XML Folder book_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
book_list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
/>
</LinearLayout>
<!-- Menu name -->
<TextView
android:id="@+id/menu_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="@string/book_name"
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<!-- Description -->
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/thumbnail"
android:layout_below="@id/menu_name"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="@string/book_address"
android:textColor="#343434"
android:textSize="10sp"
tools:ignore="SmallSp" />
<!-- Price -->
<TextView
android:id="@+id/book_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/menu_name"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="@string/book_date"
android:textColor="#10bcc9"
android:textSize="10sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
<TextView
android:id="@+id/book_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/book_date"
android:layout_centerVertical="true"
android:gravity="right"
android:text="@string/book_time"
android:textColor="#10bcc9"
android:textSize="10sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
<TextView
android:id="@+id/id"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
> </TextView>
</RelativeLayout>
我的ImageLoader
public class ImageLoader {
MemoryCache memoryCache=new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
public ImageLoader(Context context){
fileCache=new FileCache(context);
executorService=Executors.newFixedThreadPool(5);
}
final int stub_id = R.drawable.no_image;
public void DisplayImage(String url, ImageView imageView)
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
if(bitmap!=null)
imageView.setImageBitmap(bitmap);
else
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView)
{
PhotoToLoad p=new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
private Bitmap getBitmap(String url)
{
File f=fileCache.getFile(url);
//from SD cache
Bitmap b = decodeFile(f);
if(b!=null)
return b;
//from web
try {
Bitmap bitmap=null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
ex.printStackTrace();
return null;
}
}
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
//Task for the queue
private class PhotoToLoad
{
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i){
url=u;
imageView=i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad=photoToLoad;
}
@Override
public void run() {
if(imageViewReused(photoToLoad))
return;
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
Activity a=(Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
boolean imageViewReused(PhotoToLoad photoToLoad){
String tag=imageViews.get(photoToLoad.imageView);
if(tag==null || !tag.equals(photoToLoad.url))
return true;
return false;
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
public void run()
{
if(imageViewReused(photoToLoad))
return;
if(bitmap!=null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
}
答案 0 :(得分:2)
将getView
更改为
convertView = inflater.inflate(R.layout.book_list_row,parent,false);
并使用
TextView id=(TextView) convertView.findViewById(R.id.id);
//similarly for other views
并在convertView
getView
还使用ViewHolder
static class ViewHolder
{
TextView id,title,description,bookingDate,bookingTime;
ImageView thumb_image;
}
然后更改getView
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.book_list_row, parent, false);
holder = new ViewHolder();
holder.id=(TextView)convertView.findViewById(R.id.id);
holder.title = (TextView)convertView.findViewById(R.id.menu_name);
holder.description = (TextView)convertView.findViewById(R.id.address);
holder.bookingDate = (TextView)convertView.findViewById(R.id.book_date);
holder.bookingTime = (TextView)convertView.findViewById(R.id.book_time);
holder.thumb_image=(ImageView)convertView.findViewById(R.id.list_image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.id.setText(resultp.get(BookScreen.KEY_ID));
holder.title.setText(resultp.get(BookScreen.KEY_TITLE));
holder.description.setText(resultp.get(BookScreen.KEY_ADDRESS));
holder.bookingDate.setText(resultp.get(BookScreen.KEY_DATE));
holder. bookingTime.setText(resultp.get(BookScreen.KEY_TIME));
hodler.imageLoader.DisplayImage(resultp.get(BookScreen.KEY_THUMB_URL), thumb_image);
convertView.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
resultp=data.get(position);
Intent intent=new Intent(context,SingleItemView.class);
intent.putExtra("id",resultp.get(BookScreen.KEY_ID));
intent.putExtra("title",resultp.get(BookScreen.KEY_TITLE));
intent.putExtra("address",resultp.get(BookScreen.KEY_ADDRESS));
intent.putExtra("date",resultp.get(BookScreen.KEY_DATE));
intent.putExtra("time",resultp.get(BookScreen.KEY_TIME));
intent.putExtra("thumbnail",resultp.get(BookScreen.KEY_THUMB_URL));
context.startActivity(intent);
}
});
return convertView;
}
答案 1 :(得分:0)
我相信getView()
提供View
类型的参数。您应该只在整个getView中使用它。
您只在getView()中使用三个View对象。它变得如此令人困惑
请仅使用convertView。