Я пытаюсь сделать простое приложение с главным экраном с кнопками.Нажав на кнопку, вы увидите новый экран с галереей.Каждый раз, когда я нажимаю кнопку, это FC, и я не могу понять, что я делаю неправильно.
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}