2011-10-01 132 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