帮助使用按钮单击来显示图库视图

发布于 2024-10-28 05:43:43 字数 7391 浏览 5 评论 0原文

我正在尝试制作一个简单的应用程序,它有一个带有按钮的主屏幕。单击按钮将查看带有图库的新屏幕。每次我单击按钮时,它都会崩溃,我无法弄清楚我做错了什么。

Activity1.java

Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(), Activity2.class);
        startActivityForResult(myIntent, 0);
    }

});

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="This is Activity 1" />

    <Button android:text="Next"
        android:id="@+id/Button01"
        android:layout_width="250px"
        android:textSize="18px"
        android:layout_height="55px" />
</LinearLayout>

Activity2.java

private Gallery gallery;
private ImageView imgView;

private Integer[] Imgid = {
    R.drawable.a_1, R.drawable.a_2, R.drawable.a_3
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imgView = (ImageView)findViewById(R.id.ImageView01);
    imgView.setImageResource(Imgid[0]);

    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) {
            imgView.setImageResource(Imgid[position]);
        }
    });
}

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

    private Integer[] Imgid = {
        R.drawable.a_1, R.drawable.a_2, R.drawable.a_3
    };

    public AddImgAdp(Context c) {
        cont = c;
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.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.setLayoutParams(new Gallery.LayoutParams(80, 70));
        imgView.setScaleType(ImageView.ScaleType.FIT_XY);
        imgView.setBackgroundResource(GalItemBg);

        return imgView;
    }
}

main2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery"
    android:paddingTop="10px"
    android:paddingBottom="10px"
    android:scrollbarSize="200px"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<ImageView
    android:id="@+id/ImageView01"
    android:layout_gravity="fill"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

按下按钮后 logcat 读数

03-31 19:53:03.396: INFO/ActivityManager(57): Starting activity: Intent { cmp=com.sketchyproductions.newscreen/.Activity2 }
03-31 19:53:03.846: DEBUG/AndroidRuntime(274): Shutting down VM
03-31 19:53:03.846: WARN/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-31 19:53:03.906: ERROR/AndroidRuntime(274): FATAL EXCEPTION: main
03-31 19:53:03.906: ERROR/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sketchyproductions.newscreen/com.sketchyproductions.newscreen.Activity2}: java.lang.NullPointerException
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.os.Looper.loop(Looper.java:123)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at java.lang.reflect.Method.invoke(Method.java:521)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at dalvik.system.NativeStart.main(Native Method)
03-31 19:53:03.906: ERROR/AndroidRuntime(274): Caused by: java.lang.NullPointerException
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at com.sketchyproductions.newscreen.Activity2.onCreate(Activity2.java:31)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     ... 11 more
03-31 19:53:04.028: WARN/ActivityManager(57):   Force finishing activity com.sketchyproductions.newscreen/.Activity2
03-31 19:53:04.076: WARN/ActivityManager(57):   Force finishing activity com.sketchyproductions.newscreen/.Activity1
03-31 19:53:04.716: WARN/ActivityManager(57): Activity pause timeout for HistoryRecord{43faa780 com.sketchyproductions.newscreen/.Activity2}
03-31 19:53:04.906: INFO/ARMAssembler(57): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x300808:0x3008c4] in 9887950 ns
03-31 19:53:16.462: WARN/ActivityManager(57): Activity destroy timeout for HistoryRecord{43f167e0 com.sketchyproductions.newscreen/.Activity1}
03-31 19:53:16.606: WARN/ActivityManager(57): Activity destroy timeout for HistoryRecord{43faa780 com.sketchyproductions.newscreen/.Activity2}
03-31 19:54:29.329: INFO/Process(274): Sending signal. PID: 274 SIG: 9
03-31 19:54:30.267: INFO/ActivityManager(57): Process com.sketchyproductions.newscreen (pid 274) has died.
03-31 19:54:30.267: INFO/WindowManager(57): WIN DEATH: Window{43f63798 com.sketchyproductions.newscreen/com.sketchyproductions.newscreen.Activity1 paused=true}

I'm trying to make a simple app that has a main screen with buttons. Clicking on a button will view a new screen with a gallery. Every time I click the button it FC and I can't figure out what I'm doing wrong.

Activity1.java

Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(), Activity2.class);
        startActivityForResult(myIntent, 0);
    }

});

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="This is Activity 1" />

    <Button android:text="Next"
        android:id="@+id/Button01"
        android:layout_width="250px"
        android:textSize="18px"
        android:layout_height="55px" />
</LinearLayout>

Activity2.java

private Gallery gallery;
private ImageView imgView;

private Integer[] Imgid = {
    R.drawable.a_1, R.drawable.a_2, R.drawable.a_3
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imgView = (ImageView)findViewById(R.id.ImageView01);
    imgView.setImageResource(Imgid[0]);

    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) {
            imgView.setImageResource(Imgid[position]);
        }
    });
}

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

    private Integer[] Imgid = {
        R.drawable.a_1, R.drawable.a_2, R.drawable.a_3
    };

    public AddImgAdp(Context c) {
        cont = c;
        TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typArray.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.setLayoutParams(new Gallery.LayoutParams(80, 70));
        imgView.setScaleType(ImageView.ScaleType.FIT_XY);
        imgView.setBackgroundResource(GalItemBg);

        return imgView;
    }
}

main2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/examplegallery"
    android:paddingTop="10px"
    android:paddingBottom="10px"
    android:scrollbarSize="200px"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<ImageView
    android:id="@+id/ImageView01"
    android:layout_gravity="fill"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

logcat readout after pressing button

03-31 19:53:03.396: INFO/ActivityManager(57): Starting activity: Intent { cmp=com.sketchyproductions.newscreen/.Activity2 }
03-31 19:53:03.846: DEBUG/AndroidRuntime(274): Shutting down VM
03-31 19:53:03.846: WARN/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-31 19:53:03.906: ERROR/AndroidRuntime(274): FATAL EXCEPTION: main
03-31 19:53:03.906: ERROR/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sketchyproductions.newscreen/com.sketchyproductions.newscreen.Activity2}: java.lang.NullPointerException
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.os.Looper.loop(Looper.java:123)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at java.lang.reflect.Method.invoke(Method.java:521)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at dalvik.system.NativeStart.main(Native Method)
03-31 19:53:03.906: ERROR/AndroidRuntime(274): Caused by: java.lang.NullPointerException
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at com.sketchyproductions.newscreen.Activity2.onCreate(Activity2.java:31)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-31 19:53:03.906: ERROR/AndroidRuntime(274):     ... 11 more
03-31 19:53:04.028: WARN/ActivityManager(57):   Force finishing activity com.sketchyproductions.newscreen/.Activity2
03-31 19:53:04.076: WARN/ActivityManager(57):   Force finishing activity com.sketchyproductions.newscreen/.Activity1
03-31 19:53:04.716: WARN/ActivityManager(57): Activity pause timeout for HistoryRecord{43faa780 com.sketchyproductions.newscreen/.Activity2}
03-31 19:53:04.906: INFO/ARMAssembler(57): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x300808:0x3008c4] in 9887950 ns
03-31 19:53:16.462: WARN/ActivityManager(57): Activity destroy timeout for HistoryRecord{43f167e0 com.sketchyproductions.newscreen/.Activity1}
03-31 19:53:16.606: WARN/ActivityManager(57): Activity destroy timeout for HistoryRecord{43faa780 com.sketchyproductions.newscreen/.Activity2}
03-31 19:54:29.329: INFO/Process(274): Sending signal. PID: 274 SIG: 9
03-31 19:54:30.267: INFO/ActivityManager(57): Process com.sketchyproductions.newscreen (pid 274) has died.
03-31 19:54:30.267: INFO/WindowManager(57): WIN DEATH: Window{43f63798 com.sketchyproductions.newscreen/com.sketchyproductions.newscreen.Activity1 paused=true}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-11-04 05:43:43

你的 Activity2.java 使用了错误的 xml 布局文件

setContentView(R.layout.main);

imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);

所以 imgView 返回 null

我想你想要

setContentView(R.layout.main2);

your Activity2.java is using the wrong xml layout file

setContentView(R.layout.main);

imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);

So imgView is coming back null

I think you want

setContentView(R.layout.main2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文