2013-04-05 55 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