Android GridView仅在不在平板电脑上的智能手机上崩溃

时间:2014-01-09 10:26:18

标签: android gridview

这里有奇怪的错误。我是android开发的新手。我有一个网格视图活动,在平板电脑上工作正常。但是,当我在智能手机上启动相同的活动时,它会崩溃。 我不知道为什么,但我对日志没有信心。我需要帮助。

我的活动课程:

public class BookletsGridActivity extends Activity {

    GridView gv;




        String[] images = new String[] { 
                "url", "url"
        };

        String[] names = new String[] { 
                "name", "name"
        };


        String thepdfurl;


        private ImageButton button1;
        private ImageButton button2;
        private ImageButton button3;
        private ImageButton button4;    
        private ImageButton button5;


   @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

     // Portrait only
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            //Remove title bar
                    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

            //Remove notification bar
                    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);



                        setContentView(R.layout.activity_booklets);
                      // View's main buttons

                        button1 = (ImageButton)findViewById(R.id.imageButton1);
                        button1.setOnClickListener(new OnClickListener()  
                        {
                           @Override          
                           public void onClick(View Button1) {
                              finish();//go back to the previous Activity
                            overridePendingTransition(R.anim.backin, R.anim.backout); 
                           }
                           });

                        button2 = (ImageButton)findViewById(R.id.imageButton2);
                        button2.setOnClickListener(new OnClickListener()  
                        {
                           @Override          
                           public void onClick(View Button1) {
                               Intent i = new Intent(Intent.ACTION_SEND);
                                i.setType("message/rfc822");
                                i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"contact@votre-bijoutier.fr"});
                                i.putExtra(Intent.EXTRA_SUBJECT, "");
                                i.putExtra(Intent.EXTRA_TEXT   , "");
                                try {
                                    startActivity(Intent.createChooser(i, "Send mail..."));
                                } catch (android.content.ActivityNotFoundException ex) {
                                    Toast.makeText(BookletsGridActivity.this, "Pas de client de messagerie installé sur cet appareil.", Toast.LENGTH_SHORT).show();
                                }
                            }
                            });

                        button3 = (ImageButton)findViewById(R.id.imageButton3);
                        button3.setOnClickListener(new OnClickListener()  
                        {
                           @Override          
                           public void onClick(View Button1) {
                               Intent intent = new Intent(BookletsGridActivity.this, PhilosophyActivity.class);
                               startActivity(intent);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                           }
                           });


                        button4 = (ImageButton)findViewById(R.id.imageButton6);
                        button4.setOnClickListener(new OnClickListener()  
                        {
                           @Override          
                           public void onClick(View Button1) {
                               Intent intent = new Intent(BookletsGridActivity.this, MapActivity.class);
                               startActivity(intent);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                           }
                           });        

                      button5 = (ImageButton)findViewById(R.id.imageButton4);
                        button5.setOnClickListener(new OnClickListener()  
                        {
                           @Override          
                           public void onClick(View Button1) {
                               Intent intent = new Intent(BookletsGridActivity.this, ShopsActivity.class);
                               startActivity(intent);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                           }
                           });  


                        gv = (GridView) findViewById(R.id.gridView);
                        gv.setAdapter(new Custom_view(this));
                      gv.setOnItemClickListener(new OnItemClickListener() {
                          public void onItemClick(AdapterView<?> parent, View v, 
                                                  int position, long id) {
                            Intent intent = new Intent(BookletsGridActivity.this, PdfReader.class);
                              if (position == 0){
                                String pdfName = (names[0]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                              else if (position == 1){
                                String pdfName = (names[1]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 2){
                                String pdfName = (names[2]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 3){
                                String pdfName = (names[3]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 4){
                                String pdfName = (names[4]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 5){
                                String pdfName = (names[5]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 6){
                                String pdfName = (names[6]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 7){
                                String pdfName = (names[7]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 8){
                                String pdfName = (names[8]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 9){
                                String pdfName = (names[9]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 10){
                                String pdfName = (names[10]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 11){
                                String pdfName = (names[11]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 12){
                                String pdfName = (names[12]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 13){
                                String pdfName = (names[13]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }
                            else if (position == 14){
                                String pdfName = (names[14]);
                                System.out.println("names --->" + names);
                                intent.putExtra("pdfname", pdfName);
                                String prompt = (String)parent.getItemAtPosition(position);
                                intent.putExtra("pdfurl", prompt);
                                System.out.println("prompt --->" + prompt);
                               overridePendingTransition(R.anim.animationin, R.anim.animationout);
                                 startActivity(intent);
                              }


                          }
                        });

   }



                    public class Custom_view extends BaseAdapter {
                        Context ctx;
                        Custom_view(Context ctx) {
                            this.ctx = ctx;
                        }
                        public int getCount() {
                         return images.length;
                       }
                     public Object getItem(int position) {
                         return images[position];
                        }
                      public long getItemId(int position) {
                            return 0;
                       }
                        public View getView(int position, View convertView, ViewGroup parent) {

                         View myview = convertView;

                                LayoutInflater li = getLayoutInflater();
                                myview = li.inflate(R.layout.gridview_item_cell, null);
                               // TextView tv = (TextView) myview.findViewById(R.id.textView1);
                                ImageView iv = (ImageView) myview.findViewById(R.id.imageView1);
                              if (images[position].equals(images[0])) {
                              //   tv.setText(data[position]);
                                   iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/ca9e/3131/6100/1124/0000/thumb/OK_BRIAL_2013_CATALOGUE_50Pages-HD.png?1386672850", R.drawable.placeholder);
                                } else if (images[position].equals(images[1])) {
                                //    tv.setText(data[position]);
                                    iv.setImageResource(R.drawable.bookletplaceholder);
                                      UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/cb5b/3131/6100/0e36/0000/thumb/OK_HERBERT-SURGERES_2013_CATALOGUE_50Pages-HD.png?1386672859", R.drawable.placeholder);
                                } else if (images[position].equals(images[2])) {
                                  //  tv.setText(data[position]);
                                    iv.setImageResource(R.drawable.bookletplaceholder);
                                      UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/c155/3131/6100/0e17/0000/thumb/OK_HEBERT-SAINTES_2013_CATALOGUE_50Pages-HD.png?1386672865", R.drawable.placeholder);
                                } else if (images[position].equals(images[3])) {
                                  //  tv.setText(data[position]);
                                    iv.setImageResource(R.drawable.bookletplaceholder);
                                      UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/d192/3131/6100/0e40/0000/thumb/OK_DOLET_2013_CATALOGUE_50Pages-HD.png?1386672870", R.drawable.placeholder);

                         } else if (images[position].equals(images[4])) {
                              //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/be14/3131/6100/1103/0000/thumb/OK_LASSORT_2013_CATALOGUE_50Pages-HD.png?1386672875", R.drawable.placeholder);

                           } else if (images[position].equals(images[5])) {
                                  //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/d133/3131/6100/112c/0000/thumb/OK_TURCAUD_2013_CATALOGUE_50Pages-HD.png?1386672885", R.drawable.placeholder);

                           } else if (images[position].equals(images[6])) {
                                  //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/d1e6/3131/6100/0e44/0000/thumb/OK_GODET_2013_CATALOGUE_50Pages-HD.png?1386672879", R.drawable.placeholder);

                           } else if (images[position].equals(images[7])) {
                                  //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/cf96/3131/6100/1128/0000/thumb/OK_VOIX2013_CATALOGUE_50-PagesHD.png?1386672890", R.drawable.placeholder);

                            } else if (images[position].equals(images[8])) {
                                  //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/c753/3131/6100/141a/0000/thumb/OK_ANNEAU-D-OR_2013_CATALOGUE_50Pages-HD.png?1386672902", R.drawable.placeholder);

                            } else if (images[position].equals(images[9])) {
                                  //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/c854/3131/6100/1421/0000/thumb/OK_PHILIPPARIE_2013_CATALOGUE_50Pages-HD.png?1386672911", R.drawable.placeholder);

                            } else if (images[position].equals(images[10])) {
                                  //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/c024/3131/6100/0e09/0000/thumb/OK_LANDREAU-BIJOUTIER_2013_CATALOGUE_50-Pages-HD.png?1386672915", R.drawable.placeholder);

                            } else if (images[position].equals(images[11])) {
                                  //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/52a5/a9d4/3863/3700/020a/0000/thumb/OK_ANNE-OR-2013_CATALOGUE_50Pages-HD.png?1386672928", R.drawable.placeholder);

                            } else if (images[position].equals(images[12])) {
                                  //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/52a8/8d54/3134/6400/0208/0000/thumb/OK_HERBERT-ROYAN_2013_CATALOGUE_50Pages-HD.png?1386777938", R.drawable.placeholder);

                            } else if (images[position].equals(images[13])) {
                                  //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                              UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/thumbnails/5295/c9ed/3131/6100/1120/0000/thumb/COUV_Alain.png?1386679978", R.drawable.placeholder);
                            }
                          else if (images[position].equals(images[14])) {
                              //  tv.setText(data[position]);
                                iv.setImageResource(R.drawable.bookletplaceholder);
                                  UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/thumbnails/5295/bcfa/3131/6100/1403/0000/thumb/landreau-joaillier.png?1386925524", R.drawable.placeholder);
                            }
                        else if (images[position].equals(images[15])) {
                              //  tv.setText(data[position]);
                            iv.setImageResource(R.drawable.bookletplaceholder);
                              UrlImageViewHelper.setUrlDrawable(iv, "http://s3.amazonaws.com/votre-bijoutier-resources/booklets/documents/5295/c674/3131/6100/0e23/0000/thumb/OK_DANIEL_2013_CATALOGUE_50Pages-HD.png?1386672854", R.drawable.placeholder);
                        }

                            return myview;
                        }
                    }

                  protected void onPostExecute(Void result) {

                 }
                }

我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Home" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@color/black" >

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="21dp"
            android:background="@null"
            android:src="@drawable/contactbutton" />

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignTop="@+id/imageButton2"
            android:background="@null"
            android:src="@drawable/backbutton" />

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="100dp"
            android:layout_height="75dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="13dp"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/logo_alone" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton5"
            android:layout_centerHorizontal="true"
            android:scaleType="fitXY"
            android:src="@drawable/booklettitle" />

        <GridView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="325dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/imageView1"
            android:layout_marginTop="16dp"
            android:background="@color/white"


            android:gravity="center"
            android:numColumns="2"

            android:verticalSpacing="5dp" >

        </GridView>

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="@null"
            android:layout_marginBottom="14dp"
            android:src="@drawable/shoplistbutton" />

        <ImageButton
            android:id="@+id/imageButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/imageButton4"
            android:layout_centerHorizontal="true"
            android:background="@null"
            android:src="@drawable/aroundme" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/imageButton6"
            android:background="@null"
            android:src="@drawable/philosophiebutton" />

    </RelativeLayout>

</RelativeLayout>

我的Gidview单元格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/bookletgradient" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/bookletplaceholder" />

</LinearLayout>

我的日志:

01-09 05:14:37.284: W/dalvikvm(1761): threadid=1: thread exiting with uncaught exception (group=0xb1a2ab90)
01-09 05:14:37.314: E/AndroidRuntime(1761): FATAL EXCEPTION: main
01-09 05:14:37.314: E/AndroidRuntime(1761): Process: com.b.votre, PID: 1761
01-09 05:14:37.314: E/AndroidRuntime(1761): java.lang.NullPointerException
01-09 05:14:37.314: E/AndroidRuntime(1761):     at com.begital.votre.BookletsGridActivity$Custom_view.getView(BookletsGridActivity.java:355)

1 个答案:

答案 0 :(得分:0)

根据您的意见:

iv.setImageResource(R.drawable.bookletplaceholder);

只有当ivnull时,您才能拥有NPE。

ImageView iv = (ImageView) myview.findViewById(R.id.imageView1);

如果null层次结构不包含具有该ID的视图,您将获得myView

  

我使用不同的布局(一个用于智能手机,一个用于7“,一个用于10”)

确保智能手机特定的布局实际上包含ImageView,ID为imageView1