2011-10-01 3 views
0

gridviewの使い方に関する良いチュートリアルに従っています。私はコンパイルしてアプリの強制終了時にこれまでと同じようにコードを動作させることができませんでした。 Logcatによると、「アクティビティのインスタンス化ができないCompnentInfo」は、一連のその他のエラーを引き起こします。私はデバッグに便利ではないので、私は困っている。これは私のコードです:Android:Gridview force closes

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からログを追加してください。

あなたの問題について、あなたはnullオブジェクトのメソッドを呼び出すしようとしている。

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

nullポインタ例外が発生します。リスナーは、オブジェクトをbtnif-elseブロックの後)に割り当てた後にのみ追加する必要があります。

もう1つ - 警告の代わりに警告を避けるために、null警告(@SuppressWarnings("null"))を抑制しました。これにより、NULLポインタ例外が発生します。 100%確実でない限り、警告を無視しないでください。

編集:

がマニフェストを見てみると、これは小さなタイプミスです。それは

<activity android:name=".GridViewActivity" 

の代わりにする必要があります。デバッグのチュートリアルのための

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

おかげで、私はその正確なものを探してきた、そしてそれはハハ、あまりにもブートビデオが付属しています。私があなたが言及したコードを修正したコードに関しては、なぜそのセグメントのエラーが出ているのかがはっきり分かります。それを修正した後もプログラムはまだクラッシュするので、どこかでより悪いコーディングをしなければならない。 – Nick

+0

したがって、例外の詳細がわかるように、logcatログを追加してください。 – MByD

+0

上記の編集を参照してください。もう一度お手伝いをしてください。 – Nick