0
我需要找到一種方法來將ListView addapter設置爲null。我想這樣做的原因是我在Tab中有4個子活動,並且這些活動中的每一個都是延遲加載列表。的OnDestroy活動的方法,我這樣做:Android set listview addapter null exception
@Override
public void onDestroy()
{
adapter.imageLoader.stopThread();
listView.setAdapter(null);
super.onDestroy();
}
但是當我這樣做,我得到這個異常:
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): FATAL EXCEPTION: main
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): java.lang.RuntimeException: Unable to destroy activity {com.stampii.stampii/com.stampii.stampii.mystampii.OwnedStampii}: java.lang.NullPointerException
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3874)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3784)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.app.LocalActivityManager.performDestroy(LocalActivityManager.java:355)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.app.LocalActivityManager.destroyActivity(LocalActivityManager.java:381)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at com.stampii.stampii.TabGroupActivity.finishFromChild(TabGroupActivity.java:46)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.app.Activity.finish(Activity.java:3290)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at com.stampii.stampii.mystampii.OwnedStampii$1.onClick(OwnedStampii.java:38)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.view.View.performClick(View.java:2408)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.view.View$PerformClick.run(View.java:8817)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.os.Handler.handleCallback(Handler.java:587)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.os.Handler.dispatchMessage(Handler.java:92)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.os.Looper.loop(Looper.java:144)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.app.ActivityThread.main(ActivityThread.java:4937)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at java.lang.reflect.Method.invoke(Method.java:521)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at dalvik.system.NativeStart.main(Native Method)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): Caused by: java.lang.NullPointerException
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at com.stampii.stampii.mystampii.OwnedStampii.onDestroy(OwnedStampii.java:102)
08-13 15:29:43.115: ERROR/AndroidRuntime(10381): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3861)
在線路102我有這樣的:listView.setAdapter(null);
如果我刪除代碼我得到:
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): FATAL EXCEPTION: main
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at java.util.Vector.elementAt(Vector.java:329)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at java.util.Vector.get(Vector.java:443)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at com.stampii.stampii.tableview.ImageLoader$PhotosQueue.Clean(ImageLoader.java:126)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at com.stampii.stampii.tableview.ImageLoader.queuePhoto(ImageLoader.java:59)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at com.stampii.stampii.tableview.ImageLoader.DisplayImage(ImageLoader.java:51)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at com.stampii.stampii.tableview.LazyAdapter.getView(LazyAdapter.java:62)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.ListView.onMeasure(ListView.java:1109)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.LinearLayout.measureVertical(LinearLayout.java:521)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3140)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.View.measure(View.java:8172)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewRoot.performTraversals(ViewRoot.java:805)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.os.Looper.loop(Looper.java:144)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at android.app.ActivityThread.main(ActivityThread.java:4937)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at java.lang.reflect.Method.invoke(Method.java:521)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-13 15:33:36.545: ERROR/AndroidRuntime(10511): at dalvik.system.NativeStart.main(Native Method)
這裏是ImageLoader.class:
package com.stampii.stampii.tableview;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Stack;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.stampii.stampii.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;
public class ImageLoader extends Activity {
//the simplest in-memory cache implementation. This should be replaced with something like SoftReference or BitmapOptions.inPurgeable(since 1.6)
private HashMap<String, Bitmap> cache=new HashMap<String, Bitmap>();
private File cacheDir;
private AssetManager mAssetManager;
public ImageLoader(Context context){
//Make the background thead low priority. This way it will not affect the UI performance
photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
mAssetManager = context.getAssets();
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
else
cacheDir=context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
}
final int stub_id=R.drawable.default_img;
public void DisplayImage(String url, Activity activity, ImageView imageView)
{
if(cache.containsKey(url))
imageView.setImageBitmap(cache.get(url));
else
{
queuePhoto(url, activity, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, Activity activity, ImageView imageView)
{
//This ImageView may be used for other images before. So there may be some old tasks in the queue. We need to discard them.
photosQueue.Clean(imageView);
PhotoToLoad p=new PhotoToLoad(url, imageView);
synchronized(photosQueue.photosToLoad){
photosQueue.photosToLoad.push(p);
photosQueue.photosToLoad.notifyAll();
}
//start thread if it's not started yet
if(photoLoaderThread.getState()==Thread.State.NEW)
photoLoaderThread.start();
}
private Bitmap getBitmap(String src) {
Bitmap myBitmap = null;
//Decryption
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec("abcde".getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec("fedcba".getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
InputStream input = mAssetManager.open(src);
CipherInputStream cis = new CipherInputStream(input, cipher);
myBitmap = BitmapFactory.decodeStream(cis);
}
catch(Exception e){
e.printStackTrace();
Log.v("ERROR","Error : "+e);
}
return myBitmap;
}
//Task for the queue
private class PhotoToLoad
{
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i){
url=u;
imageView=i;
}
}
PhotosQueue photosQueue=new PhotosQueue();
public void stopThread()
{
photoLoaderThread.interrupt();
}
//stores list of photos to download
class PhotosQueue
{
private Stack<PhotoToLoad> photosToLoad=new Stack<PhotoToLoad>();
//removes all instances of this ImageView
public void Clean(ImageView image)
{
for(int j=0 ;j<photosToLoad.size();){
if(photosToLoad.get(j).imageView==image)
photosToLoad.remove(j);
else
++j;
}
}
}
class PhotosLoader extends Thread {
public void run() {
try {
while(true)
{
//thread waits until there are any images to load in the queue
if(photosQueue.photosToLoad.size()==0)
synchronized(photosQueue.photosToLoad){
photosQueue.photosToLoad.wait();
}
if(photosQueue.photosToLoad.size()!=0)
{
PhotoToLoad photoToLoad;
synchronized(photosQueue.photosToLoad){
photoToLoad=photosQueue.photosToLoad.pop();
}
Bitmap bmp=getBitmap(photoToLoad.url);
cache.put(photoToLoad.url, bmp);
Object tag=photoToLoad.imageView.getTag();
if(tag!=null && ((String)tag).equals(photoToLoad.url)){
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad.imageView);
Activity a=(Activity)photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
if(Thread.interrupted())
break;
}
} catch (InterruptedException e) {
//allow thread to exit
}
}
}
PhotosLoader photoLoaderThread=new PhotosLoader();
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
Bitmap bitmap;
ImageView imageView;
public BitmapDisplayer(Bitmap b, ImageView i){bitmap=b;imageView=i;}
public void run()
{
if(bitmap!=null)
imageView.setImageBitmap(bitmap);
else
imageView.setImageResource(stub_id);
}
}
public void clearCache() {
//clear memory cache
long size=0;
cache.clear();
//clear SD cache
File[] files=cacheDir.listFiles();
for(File f:files){
size=size+f.length();
f.delete();
}
}
}
任何想法如何解決呢?
剛用ImageLoader.class的代碼更新了我的問題。 –
不是100%肯定的,但據我所見,你沒有'onCreate()'函數或任何你爲此活動設置佈局的位置(通過'setContentView()')。但你必須設置一個。異常基本告訴你的是,listview不能測量它應該有多大,因爲它不能確定其他UI組件的大小以及它們消耗多少空間。因爲沒有。 – 2011-08-13 12:49:14
其實這不是一個活動,我忘了刪除延伸活動,這就是爲什麼我沒有onCreate方法。基本上,我每次得到的第二個例外,當我改變像20或30次之間的4個活動之間懶惰list.After我刪除擴展活動,我沒有得到。所以現在沒關係。感謝notice.And如何設置列表視圖的大小? –