无法启动Android新活动

时间:2013-01-19 11:33:54

标签: java android android-gallery

我正在合并另一个正在运行的程序。但是,我无法运行合并的代码。获取以下logcat错误

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

这是调用Gallery.java类的类,我放置了一个Gallery按钮,并在OnClickListener上调用了gallery类

gformbtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
              Log.v("Gallery","Gallery");
                Intent i =   new Intent(MainActivity.this,ScrollGallery.class);
                startActivity(i);

            }

        });

这是Gallery类,一旦用户点击图库按钮被重定向到此类并显示图像

public class ScrollGallery extends Activity {

    private Gallery gallery;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

         gallery = (Gallery) findViewById(R.id.examplegallery);
         gallery.setAdapter(new AddImgAdp(this));

         gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {

                Toast.makeText(ScrollGallery.this, "Position=" + position, Toast.LENGTH_SHORT).show();
            }
        });

    }

    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;


        private Integer[] Imgid = {
               R.drawable.pic_1,R.drawable.pic_2,R.drawable.pic_3,R.drawable.pic_4,R.drawable.pic_5,R.drawable.pic_6,R.drawable.pic_7,R.drawable.pic_8,R.drawable.pic_9,
               R.drawable.pic_10,R.drawable.pic_11,R.drawable.pic_12,R.drawable.pic_13,R.drawable.pic_14
        };


        /*
        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
        }
*/
        public AddImgAdp(Context c) {
            cont = c;
            TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
            GalItemBg = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }
        public int getCount() {
            return Imgid.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imgView = new ImageView(cont);

            imgView.setImageResource(Imgid[position]);

            imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imgView.setBackgroundResource(GalItemBg);

            return imgView;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您在[{1}} NullPointerException方法中获得了onCreate()

从查看代码开始,唯一可能为null的是ScrollGallery。这是因为gallery返回findViewById(R.id.examplegallery);,只会在当前夸大的布局中没有带有该ID的视图时发生。

确保XML和Java文件中都有相同的null ID。请记住,它区分大小写。