0

单击打开新活动时,我的列表活动每行形成 5 行,

我为每个类设置了单独的类(以对其进行自定义),因此我有 5 个类:as(City1、City2、City3 等)

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

我想通过使用putextra 意图将这个无限画廊传递给 5 行类

但是当运行应用程序时按下按钮打开无限画廊它给了(强制关闭

,我在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));
 }
     }

在我运行应用程序后,当单击打开图库的按钮时,它会强制关闭,

以下 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); 
4

3 回答 3

0

你尝试过类似的东西吗

Intent myIntent = new Intent();
myIntent.putExtra("key", "value");
startActivity(myIntent); 
于 2012-04-20T00:28:50.253 回答
0

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

您需要做的并且在您的代码中没有这样的事情是为您设置一个适配器InfiniteGallery(这是典型的扩展Gallery),所以它知道从哪里获取图像。

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

InfiniteGalleryAdapter mAdapter = new InfiniteGalleryAdapter(this, imageIds)

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

然后通过以下方式将其设置到您的画廊:

galleryOne.setAdapter(mAdapter).

于 2012-04-20T08:50:40.960 回答
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;





    }

}

于 2012-04-20T18:29:23.647 回答