简体   繁体   English

无法启动新的Android活动

[英]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 这是调用Gallery.java的类,我放置了Gallery按钮,并在OnClickListener iam上调用gallery类

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 这是Gallery类,一旦用户单击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;
        }
    }
}

You are getting a NullPointerException in the onCreate() method of ScrollGallery . 您在ScrollGalleryonCreate()方法中得到了NullPointerException

From looking at your code, the only thing that could possible be null is gallery . 通过查看您的代码,唯一可能为null的东西是gallery This is because findViewById(R.id.examplegallery); 这是因为findViewById(R.id.examplegallery); returns null which only happens where there is no view with that ID in the currently inflated layout. 返回null ,仅在当前展开的布局中没有使用该ID的视图时才会发生。

Make sure you have got the same examplegallery ID in both the XML and the Java files. 确保在XML和Java文件中都具有相同的examplegallery ID。 Remember that it is case sensitive. 请记住,它区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM