自定义适配器的getview中的NullPointerException

时间:2014-12-18 12:50:03

标签: android listview bitmap

我从位图方法获取图像并尝试填充列表视图。但是当我在getview中调用位图函数时,会发生nullpointerException错误。请帮帮我......

这是我的观点活动类:

public class Viewactivity extends Activity{

    TextView tv;
    ImageView im;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.views);

        ListView mListView = (ListView)findViewById(R.id.listView);
        //array houlds all images
        int Images[] = new int[]{
            R.drawable.confidential,
            ...     
            };
        //array holds all strings to be drawn in the image

        CustomList adaptor = new CustomList(this , Images);
        mListView.setAdapter(adaptor);


    }

    public Bitmap ProcessingBitmap(int image) {
        // TODO Auto-generated method stub
        Bitmap bm1 = null;
        Bitmap newBitmap = null;

        final String data =getIntent().getExtras().getString("keys");
        bm1 = ((BitmapDrawable) Viewactivity.this.getResources()
                .getDrawable(image)).getBitmap();
        Config config = bm1.getConfig();
        if(config == null){
            config = Bitmap.Config.ARGB_8888;
        }

        newBitmap = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(),config);
        Canvas newCanvas = new Canvas(newBitmap);

        newCanvas.drawBitmap(bm1, 0, 0, null);


        if(data != null){
            Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
            paintText.setColor(Color.RED);
            paintText.setTextSize(300);
            // paintText.setTextAlign(Align.CENTER);
            paintText.setStyle(Style.FILL);
            paintText.setShadowLayer(10f, 10f, 10f, Color.BLACK);

            Rect rectText = new Rect();
            paintText.getTextBounds(data, 0, data.length(), rectText);
            paintText.setTextScaleX(1.f);

            newCanvas.drawText(data, 
              0, rectText.height(), paintText);

            Toast.makeText(getApplicationContext(), 
              "drawText: " + data, Toast.LENGTH_LONG).show();

        }else{
            Toast.makeText(getApplicationContext(), 
              "caption empty!", Toast.LENGTH_LONG).show();
        }
        return newBitmap;

    }
}

这是我的适配器类:

public class CustomList extends BaseAdapter{

    Viewactivity act;
    int[] IMAGES;
    LayoutInflater inflator;
    Context sContext;
    //private String[] TEXTS;

    public CustomList(Context context, int[] images){
        this.IMAGES = images;
        //this.TEXTS = texts;
        this.sContext = context;
        inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return IMAGES.length;
    }

    @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
        View v = inflator.inflate(R.layout.row_list, parent, false);
        final ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
        imageView.setImageBitmap(act.ProcessingBitmap(IMAGES[position]));// line no:52
        return imageView;
    }
}

这是我的logcat:

12-18 06:16:51.406: E/AndroidRuntime(1388): FATAL EXCEPTION: main
12-18 06:16:51.406: E/AndroidRuntime(1388): Process: com.emple.example, PID: 1388
12-18 06:16:51.406: E/AndroidRuntime(1388): java.lang.NullPointerException
12-18 06:16:51.406: E/AndroidRuntime(1388):     at com.emple.example.CustomList.getView(CustomList.java:52)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.widget.ListView.onMeasure(ListView.java:1175)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.View.measure(View.java:16497)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.View.measure(View.java:16497)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.View.measure(View.java:16497)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.View.measure(View.java:16497)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.View.measure(View.java:16497)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.Choreographer.doFrame(Choreographer.java:544)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.os.Handler.handleCallback(Handler.java:733)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.os.Looper.loop(Looper.java:136)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at java.lang.reflect.Method.invoke(Method.java:515)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-18 06:16:51.406: E/AndroidRuntime(1388):     at dalvik.system.NativeStart.main(Native Method)
12-18 06:21:51.616: I/Process(1388): Sending signal. PID: 1388 SIG: 9

1 个答案:

答案 0 :(得分:2)

您尚未初始化act变量。在适配器构造函数中初始化它。

类似的东西:

public CustomList(Viewactivitty act, int[] images){
    this.act = act;
    this.IMAGES = images;
    //this.TEXTS = texts;
    this.sContext = act;
    inflator = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}