Don't implement a constructor and do stuff in it. Implement your initialization in onCreate().
On Thu, Mar 10, 2011 at 6:25 PM, Mark Sharpley <[email protected]>wrote: > I was playing around with the galleryview and image switcher, and I decided > to implement a textview that describes my images as I scroll through them. I > created a string array, and added a text view in my layout xml. I then added > the string and textview to my code. I thought I could reuse the position int > from the images to get the right string from my array. However, this > addition causes my app to force close when I try to run it. > > I have a feeling that I have totally butchered my working java code and you > may very well cringe at what you are about to see. Apologies. I am learning > :) > > If anyone could help me out, I would be most grateful. I have attached my > java code, some probably not relevant xml and my logcat. > > Regards, > > Mark (Java Abuser) > > package com.markopolo.test; > > import android.app.Activity; > import android.content.Context; > import android.content.res.Resources; > import android.content.res.TypedArray; > import android.os.Bundle; > import android.view.View; > import android.view.ViewGroup; > import android.view.animation.AnimationUtils; > import android.widget.AdapterView; > import android.widget.BaseAdapter; > import android.widget.Gallery; > import android.widget.TextView; > import android.widget.Gallery.LayoutParams; > import android.widget.ViewSwitcher.ViewFactory; > import android.widget.ImageSwitcher; > import android.widget.ImageView; > import android.widget.AdapterView.OnItemClickListener; > > public class MainMenuActivity extends Activity > implements ViewFactory > > { > //---the images to display-- > Integer[] imageIDs = { > R.drawable.swan1, > R.drawable.swan2, > R.drawable.swan3, > R.drawable.swan4, > R.drawable.swan5, > R.drawable.swan6 }; > > Resources res = getResources(); > String[] swans = res.getStringArray(R.array.swan); > > private ImageSwitcher imageSwitcher; > > @Override > public void onCreate(Bundle savedInstanceState) > { > super.onCreate(savedInstanceState); > setContentView(R.layout.displayview); > > imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1); > imageSwitcher.setFactory(this); > imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, > android.R.anim.fade_in)); > imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, > android.R.anim.fade_out)); > > Gallery gallery = (Gallery) findViewById(R.id.gallery1); > gallery.setAdapter(new ImageAdapter(this)); > gallery.setOnItemClickListener(new OnItemClickListener() > { > public void onItemClick(AdapterView parent, > View v, int position, long id) > { > imageSwitcher.setImageResource(imageIDs[position]); > > } > }); > } > > public View makeView() > { > ImageView imageView = new ImageView(this); > imageView.setBackgroundColor(0xFF000000); > imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); > imageView.setLayoutParams(new > ImageSwitcher.LayoutParams( > LayoutParams.FILL_PARENT, > LayoutParams.FILL_PARENT)); > return imageView; > } > > public class ImageAdapter extends BaseAdapter > { > private Context context; > private int itemBackground; > > public ImageAdapter(Context c) > { > context = c; > > //---setting the style--- > TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); > itemBackground = a.getResourceId( > R.styleable.Gallery1_android_galleryItemBackground, 0); > a.recycle(); > > } > > //---returns the number of images--- > public int getCount() > { > return imageIDs.length; > } > > //---returns the ID of an item--- > public Object getItem(int position) > { > return position; > } > > public long getItemId(int position) > { > return position; > } > > > //---returns an ImageView and TextView view--- > public View getView(int position, View convertView, ViewGroup > parent) > { > ImageView imageView = new ImageView(context); > imageView.setImageResource(imageIDs[position]); > imageView.setScaleType(ImageView.ScaleType.FIT_XY); > imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); > imageView.setBackgroundResource(itemBackground); > > TextView textview = (TextView) findViewById(R.id.textview1); > textview.setText(swans[position]); > > return imageView; > > } > } > } > > And arrays.xml: > > <?xml version="1.0" encoding="utf-8"?> > <resources> > <string-array > name="swan"> > <item>swan_1</item> > <item>swan_2</item> > <item>swan_3</item> > <item>swan_4</item> > <item>swan_5</item> > <item>swan_6</item> > <item>swan_7</item> > </string-array> > </resources> > > My layout xml: > > <?xml version="1.0" encoding="utf-8"?> > <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"> > <ImageSwitcher > android:id="@+id/switcher1" > android:layout_width="fill_parent" > android:layout_height="fill_parent" > android:layout_alignParentLeft="true" > android:layout_alignParentRight="true" > android:layout_alignParentBottom="true" > /> > <Gallery > android:id="@+id/gallery1" > android:layout_width="fill_parent" > android:layout_height="wrap_content" > android:layout_marginTop="20dip" > /> > > <TextView > android:id="@+id/textview1" > android:layout_width="wrap_content" > android:layout_height="wrap_content" > android:layout_marginBottom="10dip" > android:layout_gravity="center_horizontal|bottom" > /> > </FrameLayout> > > And here is my logcat: > > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): FATAL EXCEPTION: main > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): > java.lang.RuntimeException: Unable to instantiate activity > ComponentInfo{com.markopolo.test/com.markopolo.test.MainMenuActivity}: > java.lang.NullPointerException > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > android.app.ActivityThread.access$2300(ActivityThread.java:125) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > android.os.Handler.dispatchMessage(Handler.java:99) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > android.os.Looper.loop(Looper.java:123) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > android.app.ActivityThread.main(ActivityThread.java:4627) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > java.lang.reflect.Method.invokeNative(Native Method) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > java.lang.reflect.Method.invoke(Method.java:521) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > dalvik.system.NativeStart.main(Native Method) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): Caused by: > java.lang.NullPointerException > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > android.content.ContextWrapper.getResources(ContextWrapper.java:80) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > com.markopolo.test.MainMenuActivity.<init>(MainMenuActivity.java:36) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > java.lang.Class.newInstanceImpl(Native Method) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > java.lang.Class.newInstance(Class.java:1429) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > android.app.Instrumentation.newActivity(Instrumentation.java:1021) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): at > android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) > 03-11 02:00:59.288: ERROR/AndroidRuntime(12636): ... 11 more > > > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

