2011-10-01 130 views
0

我正在關注使用gridview的一個很好的教程。我一直無法讓代碼工作,但我一直編譯並運行app force關閉。 Logcat稱其「無法實例化活動CompnentInfo」等於一系列其他錯誤。我不想調試,所以我陷入了僵局。這是我的代碼:Android:Gridview部隊關閉

public class GridViewDemo extends Activity { 
    public String[] filenames = { 
      "File 1", 
      "File 2", 
      "Roflcopters" 
      }; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ButtonAdapter(this)); 

    } 


    //Classes 
    public class ButtonAdapter extends BaseAdapter { 
     private Context mContext; 

     // Gets the context so it can be used later 
     public ButtonAdapter(Context c) { 
      mContext = c; 
     } 

     // Total number of things contained within the adapter 
     public int getCount() { 
      return filenames.length; 
     } 

      // Require for structure, not really used in my code. 
     public Object getItem(int position) { 
      return null; 
     } 

     // Require for structure, not really used in my code. Can 
     // be used to get the id of an item in the adapter for 
     // manual control. 
     public long getItemId(int position) { 
      return position; 
     } 

     @SuppressWarnings("null") 
     public View getView(int position, 
            View convertView, ViewGroup parent) { 
      Button btn = null; 
      btn.setOnClickListener(new MyOnClickListener(position)); 
      if (convertView == null) { 
      // if it's not recycled, initialize some attributes 
      btn = new Button(mContext); 
      btn.setLayoutParams(new GridView.LayoutParams(100, 55)); 
      btn.setPadding(8, 8, 8, 8); 
      } 
      else { 
      btn = (Button) convertView; 

      } 
      btn.setText(filenames[position]); 
      // filenames is an array of strings 
      btn.setTextColor(Color.WHITE); 
      btn.setBackgroundResource(R.drawable.icon); 
      btn.setId(position); 

      return btn; 
     } 
     } 





    class MyOnClickListener implements OnClickListener { 

     private final int position; 

     public MyOnClickListener(int position) { 
      this.position = position; 
     } 

     public void onClick(View v) { 
      // Preform a function based on the position 
      // someFunction(this.position) 
      Toast.makeText(getApplicationContext(), this.position, Toast.LENGTH_SHORT).show(); 
     } 
    } 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
/> 

清單XML:

<?xml version="1.0" encoding="utf-8"?> 

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".GridviewActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 

任何想法爲什麼這崩潰? 這是logcat的輸出: enter image description here

回答

2

首先,我建議學習如何調試[見this articlethis one例如。它會很快變得方便...

其次,請在logcat中添加一條日誌,其中顯示導致「強制關閉」的異常的詳細信息。

關於你的問題,你想調用的方法的空對象:

Button btn = null; 
btn.setOnClickListener(new MyOnClickListener(position)); 

這會導致空指針異常。只有在您將對象分配到btn(位於if-else塊之後)後,才應該添加偵聽器。

還有一件事 - 你壓制了null警告(@SuppressWarnings("null")),以避免警告,而不是照顧它,這樣你就得到了空指針異常。除非你肯定百分之百,否則不要忽視警告。

編輯:

看你的表現,這是一個小錯字。它應該是

<activity android:name=".GridViewActivity" 

相反的:

<activity android:name=".GridviewActivity" 
+0

感謝調試教程,我一直在尋找一個確切的事情,並且它帶有一個視頻太開機,哈哈。至於代碼我解決了你提到的問題,我清楚地知道爲什麼我在那個部分出現錯誤。修復它然後程序仍然崩潰,所以我必須有更多的壞編碼的地方。 – Nick

+0

所以請添加一個logcat日誌,以便我們可以看到異常詳細信息。 – MByD

+0

見上面的編輯,再次感謝您的幫助。 – Nick