在listactivity中使用(Intent putextra)传递数据不起作用

时间:2012-04-20 00:22:32

标签: java android android-intent

当点击打开新活动时,我有listactivity每行形成5行,

我为每一个设置单独的类(以自定义它)所以我有5个类:as(City1,City2,City3等)

在每个行类中我有很多视图,其中一个按钮点击打开自定义无限图库

我想通过使用 putextra intent

将这个无限的图库传递给5个行类

但是当运行app PRESS BUTTON打开INFINITE GALLERY时它给了(强制关闭

,我在Android开发方面没什么经验,

请你帮助我,任何建议都将不胜感激,谢谢

我的代码: GalleryCity类

 public class GalleryCity extends Activity {
  static String city;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
      // Set the layout to use
      setContentView(R.layout.main);
      if (customTitleSupported) { 
 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); 
    TextView tv = (TextView) findViewById(R.id.tv); 
         Typeface face=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");     
         tv.setTypeface(face);
         tv.setText("My Pictures"); 
            } 

      Intent intent = getIntent(); 
      city = intent.getStringExtra("galleryimags"); 

      InfiniteGallery galleryOne = (InfiniteGallery) findViewById(R.id.galleryOne);
      galleryOne.setAdapter(new InfiniteGalleryAdapter(this)); 
      galleryOne.setSelection(galleryOne.getCount()/2);            
                     }
                     }


   class InfiniteGalleryAdapter extends BaseAdapter { 
      private Context mContext;
      public InfiniteGalleryAdapter(Context c, int[] imageIds) { 
      this.mContext = c; } 

       public int getCount() { 
      return Integer.MAX_VALUE; } 

       public Object getItem(int position) { 
      return position; } 

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

    private LayoutInflater inflater=null; 
    public InfiniteGalleryAdapter(Context a) { 
       this.mContext = a; 
       inflater = (LayoutInflater)mContext.getSystemService (
            Context.LAYOUT_INFLATER_SERVICE); 
       getImagesforCity(); 
        } 

      private int[] images;
      private void getImagesforCity() {
        if(GalleryCity.city.equalsIgnoreCase("City1")){ 
         int[] imagestemp = {
               R.drawable.one_1, R.drawable.one_1, 
                R.drawable.one_1

                  }; 
        images=imagestemp;

                        }
        if(GalleryCity.city.equalsIgnoreCase("City2")){
         int[] imagestemp = {
        R.drawable.one_2, R.drawable.one_2, 
             R.drawable.one_2   }; 
       images=imagestemp;

                                  }

        if(GalleryCity.city.equalsIgnoreCase("City3")){
          int[] imagestemp = {
        R.drawable.one_3, R.drawable.one_3, 
             R.drawable.one_3   }; 
        images=imagestemp;

                       }
        if(GalleryCity.city.equalsIgnoreCase("City4")){
          int[] imagestemp = {
        R.drawable.one_4, R.drawable.one_4, 
             R.drawable.one_4   }; 
        images=imagestemp;

                         }
        if(GalleryCity.city.equalsIgnoreCase("City5")){
            int[] imagestemp = {
        R.drawable.one_5, R.drawable.one_5, 
             R.drawable.one_5   }; 
       images=imagestemp;

                          }
                   }
       public class ViewHolder{ 
      public TextView text; 
      public ImageView image; } 

      private String[] name = { "in the cafe", "im in park", "nice place", };

      public View getView(int position, View convertView, ViewGroup parent) { 
         ImageView i = getImageView(); 

             int itemPos = (position % images.length); 

        try { i.setImageResource(images[itemPos]); ((BitmapDrawable) i.getDrawable 
                     ()).setAntiAlias(true); } 

       catch (OutOfMemoryError e) { Log.e("InfiniteGalleryAdapter", "Out of memory
                    creating imageview. Using empty view.", e); } 

       View vi=convertView; 
       ViewHolder holder; 
    if(convertView==null){ 
     vi = inflater.inflate(R.layout.gallery_items, null); 
      holder=new ViewHolder(); holder.text=(TextView)vi.findViewById
                    (R.id.textView1); 
      holder.image=(ImageView)vi.findViewById(R.id.image); 
         vi.setTag(holder); } 

      else holder=(ViewHolder)vi.getTag(); 
      holder.text.setText(name[itemPos]); 

      final int stub_id=images[itemPos]; 
      holder.image.setImageResource(stub_id); 

         return vi; } 

      private ImageView getImageView() { 

        ImageView i = new ImageView(mContext); 

               return i; } 

                        }
    class InfiniteGallery extends Gallery {

   public InfiniteGallery(Context context) {
    super(context);
      init();
            }

public InfiniteGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
                   }

public InfiniteGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
               }

private void init(){
    setSpacing(25);
    setHorizontalFadingEdgeEnabled(false);
                   }

public void setResourceImages(int[] name){
    setAdapter(new InfiniteGalleryAdapter(getContext(), name));
    setSelection((getCount() / 2));
                  }
                    }

在每个行类(City1,City2,City3等)中:

我把这段代码:

 public void handleClick(View v){


    Intent intent = new Intent(this, GalleryCity.class);
    intent.putExtra("galleryimags",city);
    startActivity(intent);                           

            }

                }

另请告诉我如何为每个设置不同的图片字符串名称: (City1,City2,City3,City4,City5)类。

如:

   private String[] name = { "in the cafe", "im in park",
                                 "nice place", };   

这是堆栈跟踪:

 04-20 18:29:17.421: E/AndroidRuntime(5007): FATAL EXCEPTION: main
 04-20 18:29:17.421: E/AndroidRuntime(5007): java.lang.RuntimeException: Unable to
 start activity ComponentInfo{com.test.city/com.test.city.GalleryCity}:
 java.lang.NullPointerException
 04-20 18:29:17.421: E/AndroidRuntime(5007):
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
 04-20 18:29:17.421: E/AndroidRuntime(5007):
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
 04-20 18:29:17.421: E/AndroidRuntime(5007):
 at android.os.Handler.dispatchMessage(Handler.java:99)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at android.os.Looper.loop(Looper.java:123)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at android.app.ActivityThread.main(ActivityThread.java:3687)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at java.lang.reflect.Method.invokeNative(Native Method)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at java.lang.reflect.Method.invoke(Method.java:507)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at dalvik.system.NativeStart.main(Native Method)
 04-20 18:29:17.421: E/AndroidRuntime(5007):
 Caused by: java.lang.NullPointerException
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at com.test.city.InfiniteGalleryAdapter.getImagesforCity(GalleryCity.java:71)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at com.test.city.InfiniteGalleryAdapter.<init>(GalleryCity.java:66)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at com.test.city.GalleryCity.onCreate(GalleryCity.java:43)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
 04-20 18:29:17.421: E/AndroidRuntime(5007): 
    ... 11 more

更新:将更改应用于 GallerCity ,如下所示:

  public class GalleryCity extends Activity {
 static String city;

    /** Called when the activity is first created. */
   @Override
      public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

    Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    // Set the layout to use
    setContentView(R.layout.main);
    if (customTitleSupported) { 
 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); 
         TextView tv = (TextView) findViewById(R.id.tv); 
     Typeface face=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");     
         tv.setTypeface(face);
         tv.setText("My Pictures"); 
            } 

    Intent intent = getIntent(); 
    city = intent.getStringExtra("galleryimags"); 

    InfiniteGallery galleryOne = (InfiniteGallery) findViewById(R.id.galleryOne);

    galleryOne.setAdapter(new InfiniteGalleryAdapter(this,intent.getStringExtra
                          ("galleryimags"))); 
    galleryOne.setSelection(galleryOne.getCount()/2);            
     }
      }

  class InfiniteGalleryAdapter extends BaseAdapter { 
  private Context mContext;

   String city ="";  
  public InfiniteGalleryAdapter(Context c, String city) {  
     this.mContext = c;     
     this.city=city;       }  


   public int getCount() { 
return Integer.MAX_VALUE; } 

   public Object getItem(int position) { 
return position; } 

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

   private LayoutInflater inflater=null; 
   public InfiniteGalleryAdapter(Context a) { 
  this.mContext = a; 
     inflater = (LayoutInflater)mContext.getSystemService
             (Context.LAYOUT_INFLATER_SERVICE);                        
       getImagesforCity(); 
           } 

      private int[] images;


   private void getImagesforCity() {
  if(city.equalsIgnoreCase("City1")){  
    int[] imagestemp = {
            R.drawable.one_1, R.drawable.one_1, 
            R.drawable.one_1

    }; 
    images=imagestemp;

            }
 if(city.equalsIgnoreCase("City2")){  
    int[] imagestemp = {
            R.drawable.one_2, R.drawable.one_2, 
            R.drawable.one_2   }; 
    images=imagestemp;

            }

 if(city.equalsIgnoreCase("City3")){  
    int[] imagestemp = {
            R.drawable.one_3, R.drawable.one_3, 
            R.drawable.one_3   }; 
    images=imagestemp;

            }
 if(city.equalsIgnoreCase("City4")){  
    int[] imagestemp = {
            R.drawable.one_4, R.drawable.one_4, 
            R.drawable.one_4   }; 
    images=imagestemp;

            }
 if(city.equalsIgnoreCase("City5")){  
    int[] imagestemp = {
            R.drawable.one_5, R.drawable.one_5, 
            R.drawable.one_5   }; 
    images=imagestemp;

          }
        }
 public class ViewHolder{ 
 public TextView text; 
 public ImageView image; } 

   private String[] name = { "in the cafe", "im in park", "nice place", };

   public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView i = getImageView(); 

   int itemPos = (position % images.length); 

   try { i.setImageResource(images[itemPos]); ((BitmapDrawable) i.getDrawable
                   ()).setAntiAlias(true); } 

  catch (OutOfMemoryError e) { Log.e("InfiniteGalleryAdapter", "Out of memory
              creating imageview. Using empty view.", e); } 

     View vi=convertView; 
     ViewHolder holder; 
   if(convertView==null){ 
 vi = inflater.inflate(R.layout.gallery_items, null); 
 holder=new ViewHolder(); holder.text=(TextView)vi.findViewById(R.id.textView1); 
 holder.image=(ImageView)vi.findViewById(R.id.image); 
 vi.setTag(holder); } 

      else holder=(ViewHolder)vi.getTag(); 
      holder.text.setText(name[itemPos]); 

      final int stub_id=images[itemPos]; 
      holder.image.setImageResource(stub_id); 

        return vi; } 

    private ImageView getImageView() { 

     ImageView i = new ImageView(mContext); 

      return i; } 

            }
    class InfiniteGallery extends Gallery {

  public InfiniteGallery(Context context) {
    super(context);
    init();
           }

public InfiniteGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
                 }

public InfiniteGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
              }

private void init(){
    setSpacing(25);
    setHorizontalFadingEdgeEnabled(false);
              }
public void setResourceImages(String name){
    setAdapter(new InfiniteGalleryAdapter(getContext(), name));
    setSelection((getCount() / 2));
                }
            }

应用更改后的第一件事我在此部分有红线错误:

  public void setResourceImages(int[] name){
    setAdapter(new InfiniteGalleryAdapter(getContext(), name));
    setSelection((getCount() / 2));
          }
          }
  red mark under :
     setAdapter(new InfiniteGalleryAdapter(getContext(), name));

然后更改为此(因此不会出现红线错误):

    public void setResourceImages(String name){
    setAdapter(new InfiniteGalleryAdapter(getContext(), name));
    setSelection((getCount() / 2));
 }
     }

在我运行app之后,当点击按钮打开图库时,它会关闭力量

在logcat日志之后:

  04-21 23:31:32.394: E/AndroidRuntime(27795): FATAL EXCEPTION: main
  04-21 23:31:32.394: E/AndroidRuntime(27795): java.lang.NullPointerException
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
    at com.test.city.InfiniteGalleryAdapter.getView(GalleryCity.java:126)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.view.View.measure(View.java:8366)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.view.View.measure(View.java:8366)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.view.View.measure(View.java:8366)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.view.View.measure(View.java:8366)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.view.View.measure(View.java:8366)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.os.Handler.dispatchMessage(Handler.java:99)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.os.Looper.loop(Looper.java:123)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at android.app.ActivityThread.main(ActivityThread.java:3687)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at java.lang.reflect.Method.invokeNative(Native Method)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at java.lang.reflect.Method.invoke(Method.java:507)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
  04-21 23:31:32.394: E/AndroidRuntime(27795): 
   at dalvik.system.NativeStart.main(Native Method)

此行中log cat中的错误:

   04-21 23:31:32.394: E/AndroidRuntime(27795): 
     at com.test.city.InfiniteGalleryAdapter.getView(GalleryCity.java:126)

在代码块中与此行匹配:

     int itemPos = (position % images.length); 

3 个答案:

答案 0 :(得分:0)

did you try something like

Intent myIntent = new Intent();
myIntent.putExtra("key", "value");
startActivity(myIntent); 

答案 1 :(得分:0)

我不认为你的意图有问题。您可以通过调试来检查它,或者在从意图中获取String extra之后放入Log.d("intentExtra", city);以检查它是否真的得到它。我认为确实如此。

你需要做什么,你的代码中没有这样的东西是为你设置一个适配器InfiniteGallery(这是典型的Gallery的扩展),所以它知道在哪里获取图像从

首先,您必须通过调用以下内容来实例化您的适配器:

InfiniteGalleryAdapter mAdapter = new InfiniteGalleryAdapter(this, imageIds)

int[] imageIds是一个id数组,我不知道你从哪里得到它)

然后通过以下方式将其设置为您的图库:

galleryOne.setAdapter(mAdapter)

答案 2 :(得分:0)

更新: 请尝试使用CityGallery类的以下代码:

InfiniteGallery galleryOne = (InfiniteGallery) findViewById(R.id.galleryOne);
        galleryOne.setAdapter(initializeImages()); 
        galleryOne.setSelection(galleryOne.getCount()/2);            





    }

    private InfiniteGalleryAdapter initializeImages() {
        InfiniteGalleryAdapter galleryAdapter = null;

        String city = getIntent().getStringExtra("cityname");
        tv.setText("My "+city+" Pictures"); 

        if(city.equalsIgnoreCase("City1")){
            int[] tempimages = { R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher };  
            String[] name = { "Image 1","Image 2", "Image 3"};  

            galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name);
        }else if(city.equalsIgnoreCase("City2")){
            int[] tempimages = { R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher };  
            String[] name = { "Image 4","Image 5", "Image 6"};  

            galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name);


        }else if(city.equalsIgnoreCase("City3")){
            int[] tempimages = { R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher };  
            String[] name = { "Image 7","Image 8", "Image 9"};  

            galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name);

        }else if(city.equalsIgnoreCase("City4")){
            int[] tempimages = { R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher };  
            String[] name = { "Image 10","Image 11", "Image 12"};  

            galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name);

        }else if(city.equalsIgnoreCase("City5")){
            int[] tempimages = { R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher };  
            String[] name = { "Image 13","Image 14", "Image 15"};  

            galleryAdapter=new InfiniteGalleryAdapter(this, tempimages, name);
        }
        return galleryAdapter;





    }

}