Gridview setadapter android

时间:2012-04-25 21:50:49

标签: android image gridview

我正在尝试在线性布局中实现Gridview来显示图片。应用程序在第77行的第77行抛出空指针异常。我正在尝试将图像适配器设置为网格视图。

package com.texpress.app;    

import java.util.Locale;    
import com.pager.test.R;    
import com.texpress.app.SQLiteAdapter;    

import android.app.Activity;    
import android.content.Context;    
import android.os.Bundle;    
import android.os.Parcelable;    
import android.support.v4.view.PagerAdapter;    
import android.support.v4.view.ViewPager;    
import android.view.LayoutInflater;    
import android.view.View;    
import android.view.ViewGroup;    
import android.database.Cursor;    
import android.speech.tts.TextToSpeech;    
import android.widget.AdapterView;    
import android.widget.BaseAdapter;    
import android.widget.Button;    
import android.widget.EditText;    
import android.widget.GridView;    
import android.widget.ImageView;    
import android.widget.ListView;    
import android.widget.PopupMenu;    
import android.widget.SimpleCursorAdapter;    
import android.widget.Toast;    
import android.view.View.OnClickListener;    



public class Texpress extends Activity implements TextToSpeech.OnInitListener, OnClickListener {    

private TextToSpeech mTts; //declaring the text to speech engine    
ListView listContent; // declaring the list for the database    
private SQLiteAdapter mySQLiteAdapter;    
SimpleCursorAdapter cursorAdapter;    
Cursor cursor;    
Button talk,Clear;    
EditText mt;    

    @Override    
    public void onCreate(Bundle savedInstanceState) {    
            super.onCreate(savedInstanceState);    
            setContentView(R.layout.main);    


           /* ----------------- objects  ---------------------------- */     
            mTts = new TextToSpeech(this,this);    
            talk = (Button) findViewById(R.id.talk);    
            Clear = (Button) findViewById(R.id.clear);    
            mt = (EditText) findViewById(R.id.mt);       
            /* ------------------------------------------------------ */    



            MyPagerAdapter adapter = new MyPagerAdapter();    
            ViewPager myPager = (ViewPager) findViewById(R.id.viewPager);    
            myPager.setAdapter(adapter);    
            myPager.setCurrentItem(2);    


       /*         
            mySQLiteAdapter = new SQLiteAdapter(this);    
            mySQLiteAdapter.openToWrite();    
            cursor = mySQLiteAdapter.queueAll();    
            String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_number, SQLiteAdapter.KEY_phrase};    
            int[] to = new int[]{R.id.id, R.id.text1,R.id.text2};    
            cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);    
            listContent.setAdapter(cursorAdapter);    
            registerForContextMenu(listContent);    

            listContent.setTextFilterEnabled(true);    
        */    

            GridView gridView = (GridView) findViewById(R.id.gridview);    
            gridView.setAdapter(new ImageAdapter(this));    

            gridView.setOnItemClickListener(new GridView.OnItemClickListener()     
            {    
                public void onItemClick(AdapterView parent, View v, int position, long id)     
                {                    
                    //Insert what to do when you click on an image.    
                }    
            });    


    }// end onCreate    


    public class ImageAdapter extends BaseAdapter     
    {    
        private Context context;    

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

        //---returns the number of images---    
        public int getCount() {    
            return imageIDs.length;    
        }    

        //---returns the ID of an item---     
        public Object getItem(int position) {    
            return position;    
        }    

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

        //---returns an ImageView view---    
        public View getView(int position, View convertView, ViewGroup parent)     
        {    
            ImageView imageView;    
            if (convertView == null) {    
                imageView = new ImageView(context);    
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));    
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);    
                imageView.setPadding(5, 5, 5, 5);    
            } else {    
                imageView = (ImageView) convertView;    
            }    
            imageView.setImageResource(imageIDs[position]);    
            return imageView;    
        }    

        Integer[] imageIDs = {    
                R.drawable.icon,    
                R.drawable.icon,    
                R.drawable.icon,    
                R.drawable.icon,    
                R.drawable.icon,    
                R.drawable.icon,    
                R.drawable.icon,    
                R.drawable.icon    
        };    


    } //end gridview    

    public void onInit(int status) {    

              if (status == TextToSpeech.SUCCESS) {    
                  int result = mTts.setLanguage(Locale.US);    
                  //mTts.speak("testing testing",TextToSpeech.QUEUE_FLUSH, null);    
                  if (result == TextToSpeech.LANG_MISSING_DATA ||    
                      result == TextToSpeech.LANG_NOT_SUPPORTED) {    
                  } else {    
                    //  talk.setEnabled(true);    
                  }    
              } else {    
              }    
    }//close oninit    


    private class MyPagerAdapter extends PagerAdapter {    

            public int getCount() {    
                    return 5;    
            }    
            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.one;    
                            break;    
                    case 1:    
                            resId = R.layout.two;    
                            break;    
                    case 2:    
                            resId = R.layout.three;    
                            break;    
                    case 3:    
                            resId = R.layout.four;    
                            break;    
                    case 4:    
                            resId = R.layout.five;    
                            break;    
                    }    

                    View view = inflater.inflate(resId, null);    
                    ((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) {    
            }    

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

            @Override    
            public void restoreState(Parcelable arg0, ClassLoader arg1) {    
            }    

            @Override    
            public Parcelable saveState() {    
                return null;    
            }    

            @Override    
            public void startUpdate(View arg0) {    
            }    

    }    

    @Override    
    public void onClick(View v) {    


    }//end onClick    

     public void onTalk(View view) {    
         mt = (EditText) findViewById(R.id.mt);              

         String mtstring =mt.getText().toString();                   
            mTts.speak(mtstring,TextToSpeech.QUEUE_FLUSH,null);    
         Toast.makeText(getApplicationContext(),     
                 "Speaking : "+mtstring, Toast.LENGTH_LONG).show();    
         System.out.println(mtstring);    
     }//end onTalk    

     public void onClear(View view) {    
         mt = (EditText) findViewById(R.id.mt);              

            mt.setText("");    
     }//end onTalk    

     protected void onDestroy()     
        {    
            super.onDestroy();              
                if (mTts != null) {    
                    mTts.stop();    
                    mTts.shutdown();    
                }//shutdown the tts engine    

        }//end onDestroy    
}

这是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" android:background="@drawable/background"  >


<GridView
android:id="@+id/gridview"
android:layout_width="400dp"
android:layout_height="600dp"
android:cacheColorHint="#00000000"
android:textSize="24dp"
android:layout_gravity="center_vertical|center_horizontal"
></GridView>

</LinearLayout>

0 个答案:

没有答案