Shweta
Shweta

Reputation: 807

Unable to Start new Activity Android

i am merging a running program in another one.However, I am unable to run the merged code.Getting the following logcat error

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

This is the class which is calling Gallery.java class,I have placed a Gallery button, and on OnClickListener iam calling the gallery class

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

            }

        });

This is the Gallery class, once user click on gallery button is redirected to this class and the images are displayed

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

Upvotes: 0

Views: 104

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82553

You are getting a NullPointerException in the onCreate() method of ScrollGallery.

From looking at your code, the only thing that could possible be null is gallery. This is because findViewById(R.id.examplegallery); returns null which only happens where there is no view with that ID in the currently inflated layout.

Make sure you have got the same examplegallery ID in both the XML and the Java files. Remember that it is case sensitive.

Upvotes: 2

Related Questions