帮助使用按钮单击来显示图库视图
我正在尝试制作一个简单的应用程序,它有一个带有按钮的主屏幕。单击按钮将查看带有图库的新屏幕。每次我单击按钮时,它都会崩溃,我无法弄清楚我做错了什么。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的 Activity2.java 使用了错误的 xml 布局文件
所以 imgView 返回 null
我想你想要
your Activity2.java is using the wrong xml layout file
So imgView is coming back null
I think you want