-3

现在它给了我这种 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 版本。

日志错误:

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;
        }

}
4

2 回答 2

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 类已弃用,将来将不再受支持。)


另外你应该从in
返回一个有效的 id :myRoseImagegetItem()

    @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;
}
于 2012-11-20T05:41:58.070 回答
3

您缺少setContentView();OnlineShopping Activity 来设置 Conatin 布局。在 super.onCreate(savedInstanceState) 之后添加 setContentView;在找到任何 View 之前:

@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();
于 2012-11-20T05:42:49.407 回答