Problem with HelloGallery

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem with HelloGallery

Postby siwica » Tue Jun 23, 2009 7:01 pm

hey i don't know what`s wrong...
its giving me nullpointer...

any idea???????

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package hello.gallery;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.util.Log;
  10.  
  11. import android.view.View;
  12.  
  13. import android.widget.AdapterView;
  14.  
  15. import android.widget.Gallery;
  16.  
  17. import android.widget.Toast;
  18.  
  19. import android.widget.AdapterView.OnItemClickListener;
  20.  
  21.  
  22.  
  23. public class HelloGallery extends Activity {
  24.  
  25.     /** Called when the activity is first created. */
  26.  
  27.     @Override
  28.  
  29.     public void onCreate(Bundle savedInstanceState) {
  30.  
  31.        
  32.  
  33.         super.onCreate(savedInstanceState);
  34.  
  35.         setContentView(R.layout.main);
  36.  
  37.  
  38.  
  39.         Gallery g = (Gallery) findViewById(R.id.gallery);
  40.  
  41.        
  42.  
  43.         g.setAdapter(new ImageAdapter(this));                                   // I think the problem is here
  44.  
  45.      
  46.  
  47.  
  48.  
  49.         g.setOnItemClickListener(new OnItemClickListener() {
  50.  
  51.             public void onItemClick(AdapterView parent, View v, int position, long id) {
  52.  
  53.                 Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
  54.  
  55.             }
  56.  
  57.         });
  58.  
  59.     }
  60.  
  61.  
  62.  
  63. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4




Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package hello.gallery;
  2.  
  3.  
  4.  
  5. import android.content.Context;
  6.  
  7. import android.content.res.TypedArray;
  8.  
  9. import android.view.View;
  10.  
  11. import android.view.ViewGroup;
  12.  
  13. import android.widget.BaseAdapter;
  14.  
  15. import android.widget.Gallery;
  16.  
  17. import android.widget.ImageView;
  18.  
  19.  
  20.  
  21. public class ImageAdapter extends BaseAdapter {
  22.  
  23.        
  24.  
  25.         //TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
  26.  
  27.     int mGalleryItemBackground;
  28.  
  29.     private Context mContext;
  30.  
  31.  
  32.  
  33.    
  34.  
  35.     private Integer[] mImageIds = {
  36.  
  37.             R.drawable.hgf,
  38.  
  39.             /**R.drawable.hge,
  40.  
  41.             R.drawable.hgf,
  42.  
  43.             R.drawable.sample_4,
  44.  
  45.             R.drawable.sample_5,
  46.  
  47.             R.drawable.sample_6,
  48.  
  49.             R.drawable.sample_7*/
  50.  
  51.     };
  52.  
  53.    
  54.  
  55.     public ImageAdapter(Context c) {
  56.  
  57.         mContext = c;
  58.  
  59.         TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
  60.  
  61.         mGalleryItemBackground = a.getResourceId
  62.  
  63. (R.styleable.Gallery1_android_galleryItemBackground, 0);
  64.  
  65.         a.recycle();
  66.  
  67.     }
  68.  
  69.  
  70.  
  71.     private TypedArray obtainStyledAttributes(int[] gallery1) {
  72.  
  73.                 // TODO Auto-generated method stub
  74.  
  75.                 return null;
  76.  
  77.         }
  78.  
  79.  
  80.  
  81.         public int getCount() {
  82.  
  83.         return mImageIds.length;
  84.  
  85.     }
  86.  
  87.  
  88.  
  89.     public Object getItem(int position) {
  90.  
  91.         return position;
  92.  
  93.     }
  94.  
  95.  
  96.  
  97.     public long getItemId(int position) {
  98.  
  99.         return position;
  100.  
  101.     }
  102.  
  103.  
  104.  
  105.     public View getView(int position, View convertView, ViewGroup parent) {
  106.  
  107.         ImageView i = new ImageView(mContext);
  108.  
  109.  
  110.  
  111.         i.setImageResource(mImageIds[position]);
  112.  
  113.         i.setLayoutParams(new Gallery.LayoutParams(150, 100));
  114.  
  115.         i.setScaleType(ImageView.ScaleType.FIT_XY);
  116.  
  117.         i.setBackgroundResource(mGalleryItemBackground);
  118.  
  119.  
  120.  
  121.         return i;
  122.  
  123.     }
  124.  
  125. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
siwica
Freshman
Freshman
 
Posts: 3
Joined: Thu Jun 18, 2009 8:33 pm

Top

Postby WarrenFaith » Tue Jun 23, 2009 8:21 pm

Hey wait... my crystal ball says.... oh no sorry, its not working.

Where is your NPE? Check in debugging mode which object or variable is null and than find the reason therefor...
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby siwica » Tue Jun 23, 2009 8:36 pm

java.lang.RuntimeException: Unabele to start activtiy ComponentInfo{hello.gallery/hello.gallery.HelloGallery}: java.lang.NullPointerExeption

no idea.... whats wrong!!!!
siwica
Freshman
Freshman
 
Posts: 3
Joined: Thu Jun 18, 2009 8:33 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests