1

总的来说,我对android开发和开发有点陌生。我有一个自定义布局,它有一个视图寻呼机,它有两个可以水平翻转的页面(它们工作得很好)。在第一页中,我有一个填充页面的网格视图。但是,我尝试了无数不同的方法来让它工作,但网格视图不会填充图像,并且我收到一个空指针异常。

我有一个包含视图寻呼机的布局文件,视图寻呼机的 id 是 fontsviewpager。然后,我有两个页面的两个独立布局,第一个有 gridview。网格视图的 id 是斜体网格。

这是活动:

public class CalligraphyFontsActivity extends Activity implements
    View.OnClickListener {
public Typeface calligraphyfont;
public TextView italiclowercase;

@Override
public void onCreate(Bundle savedInstanceState) {
    SharedPreferences getPrefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    boolean fullscreen = getPrefs.getBoolean("fullscreen", true);
    if (fullscreen) {
        setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
    } else {
        setTheme(android.R.style.Theme_NoTitleBar);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calligraphyfonts2);
    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.fontsviewpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
    ImageView backhomebutton = (ImageView) findViewById(R.id.backhomebutton);
    ImageView settingspic = (ImageView) findViewById(R.id.settingspic);
    calligraphyfont = Typeface.createFromAsset(getAssets(),
            "fonts/MTCORSVA.TTF");
    settingspic.setOnClickListener(this);
    backhomebutton.setOnClickListener(this);
    GridView gridview = (GridView) findViewById(R.id.italiclcgrid);
    gridview.setAdapter(new ImageAdapter(this));

}

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85,     85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = { R.drawable.capitalagray,
            R.drawable.capitalbgray, R.drawable.capitalcgray };

}

public void onClick(View v) {
    Intent launchsettings = new Intent(CalligraphyFontsActivity.this,
            Settings.class);
    switch (v.getId()) {
    case R.id.backhomebutton:
        finish();
        break;
    case R.id.settingspic:
        startActivity(launchsettings);
        break;

    }
}

public class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 2;
    }

    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.italiclc;

            break;
        case 1:
            resId = R.layout.italicuc;
            break;
        }

        View view = inflater.inflate(resId, null);
        if (resId == R.layout.italiclc) {
            TextView italiclowercase = (TextView) view
                    .findViewById(R.id.italiclowercase);
            italiclowercase.setTypeface(calligraphyfont);
        }
        if (resId == R.layout.italicuc) {
            TextView italicuppercase = (TextView) view
                    .findViewById(R.id.italicuppercase);
            italicuppercase.setTypeface(calligraphyfont);

        }
        ((ViewPager) collection).addView(view, 0);

        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public void finishUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

}

正如我之前所说,我尝试了许多不同的方法来使其工作无济于事。异常来自将图像适配器设置为网格视图的行(第 29 行)。任何可以提供的帮助将不胜感激。

4

1 回答 1

1

我有两个页面的两个单独的布局,第一个有gridview。

所以你在以下陈述中有问题:

GridView gridview = (GridView) findViewById(R.id.italiclcgrid);

在这里,您尝试GridViewmain.xml. 相反,您应该从定义它的布局文件中引用它。

你的代码是这样的:

setContentView(R.layout.calligraphyfonts2);
GridView gridview = (GridView) findViewById(R.id.italiclcgrid);

因此,您要确保您GridViewcalligraphyfonts2布局中定义或尝试使用充气机从另一个布局中引用它。

如果您已GridView在 italiclc 布局中定义,则在代码中添加以下更改:

settingspic.setOnClickListener(this);
backhomebutton.setOnClickListener(this);
GridView gridview = (GridView) findViewById(R.id.italiclcgrid); // <- Remove this
gridview.setAdapter(new ImageAdapter(this)); // <-- Remove this

并在此处添加:

if (resId == R.layout.italiclc) {
  GridView gridview = (GridView) view.findViewById(R.id.italiclcgrid);
  gridview.setAdapter(new ImageAdapter(mContext));

  TextView italiclowercase = (TextView) view.findViewById(R.id.italiclowercase);
  italiclowercase.setTypeface(calligraphyfont);
}
于 2012-06-17T05:04:02.480 回答