Hello...

I guess that subject was pretty loaded.  But I've had this problem
that I haven't been able to solve all day.  (I'm new at Android and
this is driving me a little crazy).

I have an application utilizing fragments.  The application is broken
down into two two fragments, a ListFragment and an ImageFragment.
Both Fragments are defined in the layout.xml file.

For smartphones, items in the list a launch a new FragmentActivity,
with a custom made ImageFragment.

When I try to add the ImageFragment and initiliaze its children; (it
has both a TextView and ImageView in it); I can't access any of the
resources.  getActivity() and getView() are both null.

It seems that the Activity is attached to the Fragment when
onCreateView is called...which happens when you add something to the
View. (I don't really understand how to use the ActivityLifeCylce &
Listener activities in general).

I was able to set the Activity through a setter method, but getView
doesn't work.  Also I think there might be a problem earlier in my
code.

Here is the logcat error: 02-21 22:08:28.178: E/AndroidRuntime(898):
at android.app.ActivityThread.main(ActivityThread.java:4424)
02-21 22:08:28.178: E/AndroidRuntime(898):      at
java.lang.reflect.Method.invokeNative(Native Method)
02-21 22:08:28.178: E/AndroidRuntime(898):      at
java.lang.reflect.Method.invoke(Method.java:511)
02-21 22:08:28.178: E/AndroidRuntime(898):      at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-21 22:08:28.178: E/AndroidRuntime(898):      at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-21 22:08:28.178: E/AndroidRuntime(898):      at
dalvik.system.NativeStart.main(Native Method)
02-21 22:08:28.178: E/AndroidRuntime(898): Caused by:
java.lang.NullPointerException
02-21 22:08:28.178: E/AndroidRuntime(898):      at
ca.mobicartel.ImageActivity.init(ImageActivity.java:36)
02-21 22:08:28.178: E/AndroidRuntime(898):      at
ca.mobicartel.ImageActivity.onCreate(ImageActivity.java:29)
02-21 22:08:28.178: E/AndroidRuntime(898):      at
android.app.Activity.performCreate(Activity.java:4465)
02-21 22:08:28.178: E/AndroidRuntime(898):      at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1049)
02-21 22:08:28.178: E/AndroidRuntime(898):      at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
1920)
02-21 22:08:28.178: E/AndroidRuntime(898):      ... 11 more


Here is my code:
==================================================================================

package ca.mobicartel;


import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageActivity extends FragmentActivity {

        private int position;
        private ImageView image;

        public void onCreate(Bundle savedStateInstance) {
                super.onCreate(savedStateInstance);
                //setContentView(R.layout.one_pane_layout);

                position = getIntent().getExtras().getInt("image_index");

                if (getResources().getBoolean(R.bool.has_two_panes)) {
                        finish();
                        return;
                }

                init();

        }

        private void init() {
                ImageFragment image_frag =
                                (ImageFragment)
getSupportFragmentManager().findFragmentById(R.id.image_fragment);
                Resources r = image_frag.getResources();
                FragmentManager fm = getSupportFragmentManager();
                fm.beginTransaction().attach(image_frag).commit();
            image_frag.setPosition(position);
        //      image_frag.setActivity(this);

                // Place an ArticleFragment as our content pane
                //ImageFragment image_frag = (ImageFragment)
getSupportFragmentManager().findFragmentById(R.id.image_fragment_a);
                //image_frag = (ImageFragment)
getSupportFragmentManager().findFragmentById(R.id.image_fragment);

                //What is android.R.id.content, and what does this do? Why not 
just
get the Fragment like above, and where will this go
                //on the layout?
            //onAttachFragment(image_frag);
                image_frag.displayImage();


                //setContentView(R.layout.one_pane_image);
        }

        public void onAttachFragment(Fragment fragment) {
                Log.i("ImageActivity", "Fragment has been attached from
ImageActivity end");
        }

}





==================================================================================
package ca.mobicartel;



import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageFragment extends Fragment {

        private static String IMAGE_FRAGMENT = "image fragment class";

        View img_and_txt;
        int position;
        TextView txt_image_text;
        String [] text_array;
        int id;
        private Activity activity;
        ImageView img;

        private Bitmap bm;

        public void onActivityCreaded(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
        }

        public ImageFragment() {
                super();
        }


        public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {

                img_and_txt = inflater.inflate(R.layout.fragment_image_profile,
null);
                displayImage();
                return img_and_txt;
        }

        public void displayImage() {

                img = (ImageView) img_and_txt.findViewById(R.id.image);

                switch(position){
                        case 0: id = R.drawable.aurora_one; break;
                        case 1: id = R.drawable.aurora_two; break;
                        case 2: id = R.drawable.aurora_three; break;
                        case 3: id = R.drawable.aurora_four; break;

                }

                        if (bm != null) {
                                bm.recycle();
                        }


                        Bitmap bm =
BitmapFactory.decodeResource(this.getActivity().getResources(), id);
                        BitmapDrawable bmd = Images.resizeImageTo(bm, 500, 300);
                        bm = bmd.getBitmap();

                        ;
                        //set the bitmap
                        img.setImageBitmap(bm);

                        //set the text
                        text_array =
this.getActivity().getResources().getStringArray(R.array.description_text);
                        TextView txt = (TextView) 
img_and_txt.findViewById(R.id.txt_desc);
                        txt.setText(text_array[position]);

        }


        public void setPosition(int position) {
                this.position = position;
        }

        public void setActivity(ImageActivity image_activity) {
                if (!isAdded()) {
                        this.activity = image_activity;
                }

        }

        public void displayImage(int position) {

                switch(position){
                case 0: id = R.drawable.aurora_one; break;
                case 1: id = R.drawable.aurora_two; break;
                case 2: id = R.drawable.aurora_three; break;
                case 3: id = R.drawable.aurora_four; break;

        }

                if (bm != null) {
                        bm.recycle();
                }


                Bitmap bm =
BitmapFactory.decodeResource(getActivity().getResources(), id);
                BitmapDrawable bmd = Images.resizeImageTo(bm, 500, 300);
                bm = bmd.getBitmap();

                img = (ImageView) img_and_txt.findViewById(R.id.image);
                //set the bitmap
                img.setImageBitmap(bm);

                //set the text
                text_array =
getActivity().getResources().getStringArray(R.array.description_text);
                TextView txt = (TextView) 
img_and_txt.findViewById(R.id.txt_desc);
                txt.setText(text_array[position]);
        }
}

-- 
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

Reply via email to