我有一个ArtistResourceImageAdapter类,它扩展了ArtistCoverFlowAdapter 我得到一个零点异常 在其中一个字符串中(应该保存图像的路径) 字符串是“res” 当我在构造函数中检查它似乎不是null(我使用logcat检查) 但这个方法似乎是空的
@override
protected Bitmap createBitmap( int position) {
if(res[position]==null)
..........
..............
..................
}
现在,方法createBitmap已在ArtistResourceImageAdapter中重写,并在ArtistCoverFlowAdapter类中声明。
ArtistResourceImageAdapter的代码是(此代码中的最后一个方法(createBitmap),我得到字符串的空点异常)
package source.justanothermusicplayer.coverflw;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import source.justanothermusicplayer.R;
import source.justanothermusicplayer.R.drawable;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.util.TypedValue;
/**
* This class is an adapter that provides images from a fixed set of resource
* ids. Bitmaps and ImageViews are kept as weak references so that they can be
* cleared by garbage collection when not needed.
*
*/
public class ArtistResourceImageAdapter extends ArtistCoverFlowAdapter {
/** The Constant TAG. */
private static final String TAG = ArtistResourceImageAdapter.class.getSimpleName();
float ht_px;float wt_px;
/** The Constant DEFAULT_LIST_SIZE. */
private static final int DEFAULT_LIST_SIZE = 40;
/** The Constant IMAGE_RESOURCE_IDS. */
private static final List<Integer> IMAGE_RESOURCE_IDS = new ArrayList<Integer>(DEFAULT_LIST_SIZE);
/** The Constant DEFAULT_RESOURCE_LIST. */
String[] res=new String[20];
/** The bitmap map. */
private final Map<Integer, WeakReference<Bitmap>> bitmapMap = new HashMap<Integer, WeakReference<Bitmap>>();
private final Context context;
/**
* Creates the adapter with default set of resource images.
*
* @param context
* context
*/
public ArtistResourceImageAdapter(final Context context,String [] resources) {
super();
this.context = context;
ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 130,context. getResources().getDisplayMetrics());
wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 115,context. getResources().getDisplayMetrics());
setResources(resources);
}
public final synchronized void setResources(String[] resources) {
IMAGE_RESOURCE_IDS.clear();res=resources;
for ( int Id =1; Id<=resources.length;Id++) {
IMAGE_RESOURCE_IDS.add(Id);
}
notifyDataSetChanged();
}
/**
* Replaces resources with those specified.
*
* @param resourceIds
* array of ids of resources.
*/
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getCount()
*/
@Override
public synchronized int getCount() {
return IMAGE_RESOURCE_IDS.size();
}
/*
* (non-Javadoc)
*
* @see pl.polidea.coverflow.AbstractCoverFlowImageAdapter#createBitmap(int)
*/
@Override
protected Bitmap createBitmap( int position) {
Log.v(TAG, "creating item " + position);
String url=new String();
if(res[position]==null)
Log.i("holalala jhingallaa ", "res[position] is null");
url=res[position];
Bitmap b = BitmapFactory.decodeFile(url);
b = Bitmap.createScaledBitmap(b, (int) ht_px, (int) wt_px, true);
bitmapMap.put(position, new WeakReference<Bitmap>(b));
return b;
}
}
logcat:
12-12 14:16:58.876: E/AndroidRuntime(24293): FATAL EXCEPTION: main
12-12 14:16:58.876: E/AndroidRuntime(24293): java.lang.NullPointerException
12-12 14:16:58.876: E/AndroidRuntime(24293): at java.io.File.fixSlashes(File.java:185)
12-12 14:16:58.876: E/AndroidRuntime(24293): at java.io.File.<init>(File.java:134)
12-12 14:16:58.876: E/AndroidRuntime(24293): at source.justanothermusicplayer.coverflw.ArtistResourceImageAdapter.createBitmap(ArtistResourceImageAdapter.java:99)
12-12 14:16:58.876: E/AndroidRuntime(24293): at source.justanothermusicplayer.coverflw.ArtistCoverFlowAdapter.getItem(ArtistCoverFlowAdapter.java:73)
12-12 14:16:58.876: E/AndroidRuntime(24293): at source.justanothermusicplayer.coverflw.ArtistReflectingAdapter.createBitmap(ArtistReflectingAdapter.java:79)
12-12 14:16:58.876: E/AndroidRuntime(24293): at source.justanothermusicplayer.coverflw.ArtistCoverFlowAdapter.getItem(ArtistCoverFlowAdapter.java:73)
12-12 14:16:58.876: E/AndroidRuntime(24293): at source.justanothermusicplayer.coverflw.ArtistReflectingAdapter.createBitmap(ArtistReflectingAdapter.java:79)
12-12 14:16:58.876: E/AndroidRuntime(24293): at source.justanothermusicplayer.coverflw.ArtistCoverFlowAdapter.getItem(ArtistCoverFlowAdapter.java:73)
12-12 14:16:58.876: E/AndroidRuntime(24293): at source.justanothermusicplayer.coverflw.ArtistCoverFlowAdapter.getView(ArtistCoverFlowAdapter.java:117)
12-12 14:16:58.876: E/AndroidRuntime(24293): at source.justanothermusicplayer.coverflw.ArtistCoverFlowAdapter.getView(ArtistCoverFlowAdapter.java:1)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.View.measure(View.java:15401)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4950)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1426)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.widget.LinearLayout.measureVertical(LinearLayout.java:703)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.View.measure(View.java:15401)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4950)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.View.measure(View.java:15401)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.widget.LinearLayout.measureVertical(LinearLayout.java:855)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.View.measure(View.java:15401)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4950)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
12-12 14:16:58.876: E/AndroidRuntime(24293): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2154)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.View.measure(View.java:15401)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1856)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1279)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1002)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4294)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.Choreographer.doFrame(Choreographer.java:525)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.os.Handler.handleCallback(Handler.java:643)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.os.Looper.loop(Looper.java:137)
12-12 14:16:58.876: E/AndroidRuntime(24293): at android.app.ActivityThread.main(ActivityThread.java:4803)
12-12 14:16:58.876: E/AndroidRuntime(24293): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 14:16:58.876: E/AndroidRuntime(24293): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 14:16:58.876: E/AndroidRuntime(24293): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
12-12 14:16:58.876: E/AndroidRuntime(24293): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
12-12 14:16:58.876: E/AndroidRuntime(24293): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
看起来在res [position]返回null的情况下,你继续处理剩下的代码,我相信这就是给你空指针,因为你正在使用那个空值来处理其余的逻辑。 尝试为条件检查添加括号并返回null。使用createBitmap的方法需要处理空位图,因为它不可用。
protected Bitmap createBitmap( int position) {
Log.v(TAG, "creating item " + position);
String url=new String();
if(res[position]==null) {
Log.i("holalala jhingallaa ", "res[position] is null");
return null;
}
url=res[position];
File f=new File(url);
Bitmap b = BitmapFactory.decodeFile(url);
b = Bitmap.createScaledBitmap(b, (int) ht_px, (int) wt_px, true);
bitmapMap.put(position, new WeakReference<Bitmap>(b));
return b;
}