2016-10-18 85 views
0

我試圖練習在Android Studio中使用GridView,我不知道爲什麼我不斷收到此異常。AndroidRuntime:致命例外:主要ID不會

這是我的主要類的代碼。

package com.example.chetam.gokalsapp; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.GridView; 
    import android.widget.Toast; 

    public class Products extends Activity { 
     GridView grid; 
     String[] web = { 
       "Google", 
       "Github", 
       "Instagram", 
       "Facebook", 
       "Flickr", 
       "Pinterest", 
       "Quora", 
       "Twitter", 
       "Vimeo", 
       "WordPress", 
       "Youtube", 
       "Stumbleupon", 
       "SoundCloud", 
       "Reddit", 
       "Blogger" 

     } ; 
     int[] imageId = { 
       R.drawable.v, 
       R.drawable.img, 
       R.drawable.img1, 
       R.drawable.img2, 
       R.drawable.img3, 
       R.drawable.v, 
       R.drawable.img, 
       R.drawable.img1, 
       R.drawable.img2, 
       R.drawable.img3, 
       R.drawable.v, 
       R.drawable.img, 
       R.drawable.img1, 
       R.drawable.img2, 
       R.drawable.img3  
     };  

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

      grid=(GridView)findViewById(R.id.grid); 

      CustomGrid adapter = new CustomGrid(Products.this, web, imageId); 
      grid.setAdapter(adapter); 

      grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
             int position, long id) { 
        Toast.makeText(Products.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show(); 

       } 
      });  
     }  
    } 

這裏是我的主XML文件的代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.chetam.gokalsapp.Products"> 

    <GridView 
     android:numColumns="auto_fit" 
     android:gravity="center" 
     android:columnWidth="100dp" 
     android:stretchMode="columnWidth" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/grid" 
     />  
</RelativeLayout> 

這是我的佈局文件

<?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:padding="5dp" 
    android:gravity="center_horizontal" 
    android:orientation="vertical">  

    <ImageView 
     android:id="@+id/grid_image" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_marginTop="15dp"> 
    </ImageView> 

    <TextView 
     android:id="@+id/grid_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:textSize="12sp" > 
    </TextView> 

</LinearLayout> 

這裏是我的適配器類

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class CustomGrid extends BaseAdapter{ 
    private Context mContext; 
    private final String[] web; 
    private final int[] Imageid; 

    public CustomGrid(Context c,String[] web,int[] Imageid) { 
     mContext = c; 
     this.Imageid = Imageid; 
     this.web = web; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return web.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View grid; 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 

      grid = new View(mContext); 
      grid = inflater.inflate(R.layout.grid_single, null); 
      TextView textView = (TextView) grid.findViewById(R.id.grid_text); 
      ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8,8,8,8); 
      textView.setText(web[position]); 
      imageView.setImageResource(Imageid[position]); 
     } 
     else 
     { 
      grid = (View) convertView; 
     } 

     return grid; 
    } 
} 
+0

添加logcat的輸出 –

+0

發表您的崩潰logcat的 – SripadRaj

+2

'網站[+位置]'這看起來是錯誤的。刪除那個'+' –

回答

0

更換電網onClickListener與:

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      Toast.makeText(Products.this, "You Clicked at " +web[position], Toast.LENGTH_SHORT).show(); 

     } 
    }); 

您正在使用整型變量作爲String數組的位置。如果你有「+位置」,因爲你比的位置想一個數字越高,那麼正確的語法是:

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
     Toast.makeText(Products.this, "You Clicked at " +web[position + 1], Toast.LENGTH_SHORT).show(); 

    } 
}); 
+0

解釋你改變了什麼,爲什麼,否則答案是無用的 –

相關問題