2013-04-05 53 views
2

我遇到了Android问题。 我想设置一个ImageAdapter使用这个代码库:Android - 设置ImageAdapter空指针异常

Integer[] images = {R.drawable.image01}; 
static int counter = 0; 

SectionsPagerAdapter mSectionsPagerAdapter; 
ViewPager mViewPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Set up the action bar. 
    final ActionBar actionBar = getActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the app. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(
      getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    // When swiping between different sections, select the corresponding 
    // tab. We can also use ActionBar.Tab#select() to do this if we have 
    // a reference to the Tab. 
    mViewPager 
      .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
       @Override 
       public void onPageSelected(int position) { 
        actionBar.setSelectedNavigationItem(position); 
       } 
      }); 

    // For each of the sections in the app, add a tab to the action bar. 
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
     // Create a tab with text corresponding to the page title defined by 
     // the adapter. Also specify this Activity object, which implements 
     // the TabListener interface, as the callback (listener) for when 
     // this tab is selected. 
     actionBar.addTab(actionBar.newTab() 
       .setText(mSectionsPagerAdapter.getPageTitle(i)) 
       .setTabListener(this)); 
    } 

    Gallery g = (Gallery) findViewById(R.id.gallery1); 
    g.setAdapter(new ImageAdapter(this, images)); 
} 

我要像“image01”添加到使用ImageAdapter画廊。 但是,此代码会导致NullPointerException。该ImageAdapter的代码是:

public class ImageAdapter extends BaseAdapter { 
    Context m; 
    public ImageAdapter(Context c){ 
     m = c; 
    } 

    @Override 
    public int getCount() { 
     return counter; 
    } 

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

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView i = new ImageView(m); 
     i.setImageResource(images[position]); 
     i.setLayoutParams(new Gallery.LayoutParams(150, 150)); 
     i.setScaleType(ImageView.ScaleType.FIT_XY); 
     return i; 

    } 
} 

编辑:我的XML是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Gallery 
     android:id="@+id/gallery1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="17.92" 
     android:spacing="@dimen/activity_horizontal_margin" /> 

</LinearLayout> 

堆栈跟踪是:在适配器的时候已经犯规

04-05 22:11:38.144: D/AndroidRuntime(32124): Shutting down VM 
04-05 22:11:38.144: W/dalvikvm(32124): threadid=1: thread exiting with uncaught exception (group=0x40ebf930) 
04-05 22:11:38.144: E/AndroidRuntime(32124): FATAL EXCEPTION: main 
04-05 22:11:38.144: E/AndroidRuntime(32124): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bw2801.uwelugemediathek/com.bw2801.uwelugemediathek.MainActivity}: java.lang.NullPointerException 
04-05 22:11:38.144: E/AndroidRuntime(32124): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at android.os.Looper.loop(Looper.java:137) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at java.lang.reflect.Method.invokeNative(Native Method) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at java.lang.reflect.Method.invoke(Method.java:511) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at dalvik.system.NativeStart.main(Native Method) 
04-05 22:11:38.144: E/AndroidRuntime(32124): Caused by: java.lang.NullPointerException 
04-05 22:11:38.144: E/AndroidRuntime(32124): at com.bw2801.uwelugemediathek.MainActivity.onCreate(MainActivity.java:79) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at android.app.Activity.performCreate(Activity.java:5104) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
04-05 22:11:38.144: E/AndroidRuntime(32124): at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
04-05 22:11:38.144: E/AndroidRuntime(32124): ... 11 more 
+1

我想像你的findViewById没有找到你的GridView和其当您尝试设置适配器时崩溃。否则你的代码看起来很好。不要忘记,你的计数器是0,并且不会显示任何值,直到你增加它。 – dymmeh 2013-04-05 19:38:23

+0

但是为什么不能找到画廊?它存在于布局中。 – bw2801 2013-04-05 20:07:10

+0

发布你的错误stacktrace(logcat)和xml请(或验证你的图库有一个'android:id =“@ + id/gallery1”'属性) – petey 2013-04-05 20:08:14

回答

0

我做到了使用此代码和它的工作原理:

final LayoutInflater factory = getLayoutInflater(); 
final View view = factory.inflate(R.layout.pictures, null); 
Gallery g = (Gallery) view.findViewById(R.id.gallery1); 
g.setAdapter(new ImageAdapter(this)); 
3

的图片库中同时存在,所以 添加一个新的参数给你的适配器的构造函数来接受图像

public class ImageAdapter extends BaseAdapter { 
    Context m; 
    Integer[] images 
    public ImageAdapter(Context c, Integer[] images){ 
     m = c; 
     this.images = images 
    } 
... 

然后调用,像这样在你的活动:

g.setAdapter(new ImageAdapter(this, images)); 
+0

我这样做了,但应用程序仍然在抛出一个NullPointerException在'setAdapter()'。 – bw2801 2013-04-05 19:46:56

+0

嗯!检查上面的dotroph注释。我想他可能是对的! – petey 2013-04-05 19:55:09