我收到错误ERROR / AndroidRuntime(1482):来自我的ImageAdapter类的java.lang.NullPointerException。请帮我解决这个问题。
ImageSwitcherActivity.java
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitcherActivity extends Activity{
private ImageSwitcher iSwitcher;
private Gallery gallery;
Integer[] images = {R.drawable.image1,R.drawable.image2,
R.drawable.image3,R.drawable.image4,
R.drawable.image5,R.drawable.icon,
R.drawable.icon,R.drawable.icon,
R.drawable.icon
};
static int counter = 0;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
counter = images.length;
Log.i("Image counter in onCreate", "" +counter);
setContentView(R.layout.imageswitcher);
gallery = (Gallery)findViewById(R.id.glry);
gallery.setAdapter(new ImageAdapter1(this));
}
class ImageAdapter1 extends BaseAdapter{
Context m;
public ImageAdapter1(Context c){
m = c;
}
public ImageAdapter1(ImageSwitcherActivity imageSwitcherActivity) {
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
Log.i("Image Count in ImageAdapter1", ""+counter);
return counter;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.i("Enters in GetView", "GetView");
ImageView i = new ImageView(m);
Log.i("After Intializing Imageview", "Init ImageView");
i.setImageResource(images[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 150));
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;
}
}
我的LogCat详细信息:
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): FATAL EXCEPTION: main
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): java.lang.NullPointerException
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.ViewConfiguration.get(ViewConfiguration.java:228)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.View.<init>(View.java:1878)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.widget.ImageView.<init>(ImageView.java:103)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at com.myworkspace.ImageAdapter1.getView(ImageSwitcherActivity.java:107)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.View.measure(View.java:8313)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.View.measure(View.java:8313)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.View.measure(View.java:8313)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.View.measure(View.java:8313)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.View.measure(View.java:8313)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.os.Looper.loop(Looper.java:123)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at android.app.ActivityThread.main(ActivityThread.java:3647)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at java.lang.reflect.Method.invokeNative(Native Method)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at java.lang.reflect.Method.invoke(Method.java:507)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-14 06:10:14.290: ERROR/AndroidRuntime(1515): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
只需删除
行public ImageAdapter1(ImageSwitcherActivity imageSwitcherActivity)
{
// TODO Auto-generated constructor stub
}
因为它被称为构造函数,但未将Context m
设置为ImageSwitcherActivity
。
由于Context
为null
,因此会导致NullPointerException
答案 1 :(得分:0)
只是尝试替换此代码
Activity m;
public ImageAdapter1(Activity c){
m = c;
}
取代
Context m;
public ImageAdapter1(Context c){
m = c;
}
希望这有效。