值未传递给intent

时间:2012-06-23 18:16:58

标签: android android-intent android-gridview

堆栈跟踪

Picture of stack trace here

值不会被传递给另一个意图......每次我尝试以意图启动活动时...它崩溃了..

//This activity will retrieve and display the different rewards that are available.
public class RewardsActivity extends Activity {


@Override
public void onCreate(Bundle SavedInstanceState)
{
    super.onCreate(SavedInstanceState);
    setContentView(R.layout.rewards);

    //stores retrieves and stores the current gridview
    GridView gridView = (GridView)findViewById(R.id.grid_view);

    //Instance of ImageAdapter class that will load the images into the gridview
    gridView.setAdapter(new ImageAdapter(this));

    //This function is used to set a listener on each item in the grid so that      when its clicked it will go to the other view.
    gridView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View v,int position, long id)
        {
            Intent i = new   Intent(getApplicationContext(),RewardsViewActivity.class);
            i.putExtra("id", position);
            startActivity(i);
        }

    });



}

它传递给它的新意图,当它传递给它时,它存储在一个变量中,然后在ImageView中用于加载图像。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

public class RewardsViewActivity extends Activity {

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

    // get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    ImageView imageView = (ImageView) findViewById(R.id.full_image);
    imageView.setImageResource(imageAdapter.finalImages[position]);
}

}

ImageAdapter.java     包org.android.pps;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/*This class will be used handle the loading of the image view that will display all the
images of the rewards. (this will be used along with RewardsActivity and rewards.xml)
 */
public class ImageAdapter extends BaseAdapter {

    //variable that will store the current context of the application
    private Context c;
    private Integer num = 6;
    private int[] rewards_num=new int[num];
    private Integer[] Images = new Integer[6];
    public Integer[] finalImages;



    //for loop will set the correct image to the array if its either activated or deactivated

    public Integer[] fillImageArray()
    {
        //Array that will be used to show the reward images

        Integer[] Activated ={
            R.drawable.rewards1,
            R.drawable.rewards2,
            R.drawable.rewards3,
            R.drawable.rewards4,
            R.drawable.rewards5,
            R.drawable.rewards6,
        };
        Integer[] Deactivated ={
                R.drawable.rewards1b,
                R.drawable.rewards2b,
                R.drawable.rewards3b,
                R.drawable.rewards4b,
                R.drawable.rewards5b,
                R.drawable.rewards6b,
            };


        //for loop that checks to see all the rewards that a particular users has to assign a particular image.
        for(int x = 0;x<rewards_num.length;x++)
        {

            for(int y = 0;y<6;y++)
            {
                if(rewards_num[x]==y)
                {
                    Images[x]=Activated[y];

                }
                else
                {
                    Images[x]=Deactivated[y];
                }
            }
            }

        return Images;


    }


    //constructor with the context being passed.
    public ImageAdapter(Context m)
    {
        c = m;

    }

    public int getCount() {

        return 6;
    }

    public Object getItem(int position) {

        return Images[position];
    }

    public long getItemId(int position) {
        return 0;
    }


    // The function View  create a new ImageView for each item that is being referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        finalImages = fillImageArray();
        ImageView imageView = new ImageView(c);
        imageView.setImageResource(finalImages[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
        return imageView;

    }



}  

1 个答案:

答案 0 :(得分:0)

noi中的finalImages在那里初始化.........

getview中的finalImages在第二个活动中没有被调用(RewardsViewActivity)............

如果可能的话,将这一行移到构造函数finalImages = fillImageArray();