android应用程序强制关闭时null指针异常

时间:2012-11-20 05:40:17

标签: android imageview

现在它给了我这种Logcat错误:

11-20 11:19:04.729: E/AndroidRuntime(447): FATAL EXCEPTION: main
11-20 11:19:04.729: E/AndroidRuntime(447): java.lang.NullPointerException
11-20 11:19:04.729: E/AndroidRuntime(447):  at com.example.orderplacemnet.ImageAdapter.getView(OnlineShopping.java:157)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.view.View.measure(View.java:8171)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.view.View.measure(View.java:8171)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.view.View.measure(View.java:8171)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.view.View.measure(View.java:8171)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.view.View.measure(View.java:8171)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.os.Looper.loop(Looper.java:123)
11-20 11:19:04.729: E/AndroidRuntime(447):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-20 11:19:04.729: E/AndroidRuntime(447):  at java.lang.reflect.Method.invokeNative(Native Method)
11-20 11:19:04.729: E/AndroidRuntime(447):  at java.lang.reflect.Method.invoke(Method.java:521)
11-20 11:19:04.729: E/AndroidRuntime(447):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-20 11:19:04.729: E/AndroidRuntime(447):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-20 11:19:04.729: E/AndroidRuntime(447):  at dalvik.system.NativeStart.main(Native Method)

此活动用于将图像适配器添加到库中但不起作用。 我正在研究android 2.0版本。

Logcat错误:

11-20 10:46:36.118: E/AndroidRuntime(420): FATAL EXCEPTION: main
11-20 10:46:36.118: E/AndroidRuntime(420): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.orderplacemnet/com.example.orderplacemnet.OnlineShopping}: java.lang.NullPointerException
11-20 10:46:36.118: E/AndroidRuntime(420):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-20 10:46:36.118: E/AndroidRuntime(420):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-20 10:46:36.118: E/AndroidRuntime(420):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-20 10:46:36.118: E/AndroidRuntime(420):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-20 10:46:36.118: E/AndroidRuntime(420):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 10:46:36.118: E/AndroidRuntime(420):  at android.os.Looper.loop(Looper.java:123)
11-20 10:46:36.118: E/AndroidRuntime(420):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-20 10:46:36.118: E/AndroidRuntime(420):  at java.lang.reflect.Method.invokeNative(Native Method)
11-20 10:46:36.118: E/AndroidRuntime(420):  at java.lang.reflect.Method.invoke(Method.java:521)
11-20 10:46:36.118: E/AndroidRuntime(420):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-20 10:46:36.118: E/AndroidRuntime(420):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-20 10:46:36.118: E/AndroidRuntime(420):  at dalvik.system.NativeStart.main(Native Method)
11-20 10:46:36.118: E/AndroidRuntime(420): Caused by: java.lang.NullPointerException
11-20 10:46:36.118: E/AndroidRuntime(420):  at com.example.orderplacemnet.OnlineShopping.onCreate(OnlineShopping.java:51)
11-20 10:46:36.118: E/AndroidRuntime(420):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-20 10:46:36.118: E/AndroidRuntime(420):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-20 10:46:36.118: E/AndroidRuntime(420):  ... 11 more

代码:

package com.example.orderplacemnet;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;


import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import android.widget.ImageView;


public class OnlineShopping extends Activity  {

    Gallery myRoseGallery;
     private List<Drawable> drawables=null;

//  ImageView img;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_online_shopping);
        myRoseGallery=(Gallery)findViewById(R.id.rosegallery);
        //img=(ImageView)findViewById(R.id.roseimg);

        Toast.makeText(this, "dfhgkhj", Toast.LENGTH_SHORT).show();


getDrawablesList();





      myRoseGallery.setAdapter(new ImageAdapter(this));


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);
    }



     private void getDrawablesList() {


            drawables = new ArrayList<Drawable>();
            drawables.add(getResources().getDrawable(R.drawable.rose_five));
            drawables.add(getResources().getDrawable(R.drawable.rose));
            drawables.add(getResources().getDrawable(R.drawable.rose_two));
            drawables.add(getResources().getDrawable(R.drawable.rose_three));
            drawables.add(getResources().getDrawable(R.drawable.rose_four));


        }

//      private void setSelectedImage(int selectedImagePosition) {
//
//          BitmapDrawable bd = (BitmapDrawable) drawables.get(selectedImagePosition);
//          Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);
//         
//
//      }

    }

 class ImageAdapter  extends BaseAdapter{

    private int[] myRoseImage = { R.drawable.rose, R.drawable.rose_two,
               R.drawable.rose_three, R.drawable.rose_four,R.drawable.rose_five };
    private Context myContext=null;

    private Activity context=null;

    private static ImageView iv=null;

    private List<Drawable> plotsImages=null;

    private static ViewHolder holder=null;

    public ImageAdapter(Activity context, List<Drawable> plotsImages) {

        this.context = context;
        this.plotsImages = plotsImages;

    }
    public ImageAdapter(Context c) {
        super();
        // give yr conext here
        this.myContext=c;

    }

    @Override
    public int getCount() {
        // how many image is there
        return this.myRoseImage.length;
    }

    @Override
    public Object getItem(int position) {
        //which item is selected
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }


    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // display image in yr Image view as per yr view and position

        if (view == null) {

               holder = new ViewHolder();

               ImageView iv = new ImageView(this.myContext);

                iv.setPadding(3, 3, 3, 3);

                view = iv;

                holder.imageView = iv;

               view.setTag(holder);

            } else {

                    holder = (ViewHolder) view.getTag();
            }

            holder.imageView.setImageDrawable(plotsImages.get(position));

            holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            holder.imageView.setLayoutParams(new Gallery.LayoutParams(150, 90));

            return iv;

    }
     private static class ViewHolder {
            ImageView imageView;
        }

}

2 个答案:

答案 0 :(得分:4)

如果您未使用findViewById() ...

,则无法使用setContentView()找到观看次数
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.xxx); Add the layout that contains rosegallery
    myRoseGallery=(Gallery)findViewById(R.id.rosegallery);

或者,如果图库是您想要的唯一视图,则可以使用:

    myRoseGallery = new Gallery(this);

(了解Gallery类已弃用,将来不再支持。)


<强>加成
您应该从myRoseImage中的getItem()返回有效ID:

    @Override
    public int getItem(int position) {
        //which item is selected
        return myRoseImage[position];
    }

并更新您在getView()中设置图片的方式:

holder.imageView.setImageResource(getItem(position));

您无法调用plotImages.get(position),因为使用此构造函数时plotImages仍为null:

public ImageAdapter(Context c) {
    super();
    // give yr conext here
    this.myContext=c;
}

答案 1 :(得分:3)

您的在线购物活动缺少setContentView();以设置包含布局。在super.onCreate(savedInstanceState)之后添加setContentView;在找到任何视图之前:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Your_layout_id);  //add  here
        myRoseGallery=(Gallery)findViewById(R.id.rosegallery);
        //img=(ImageView)findViewById(R.id.roseimg);

        Toast.makeText(this, "dfhgkhj", Toast.LENGTH_SHORT).show();