我有一个网格视图,并且我定义了一个必须在网格中显示的 6 个项目(字符串)的数组。
Grid_ViewActivity:
public class Grid_ViewActivity extends Activity {
/** Called when the activity is first created. */
//TextView selection;
String[] characters=getResources().getStringArray(R.array.characters);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gv=(GridView)findViewById(R.id.gridView1);
//selection=(TextView)findViewById(R.id.selection);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.simple_list_item,characters);
gv.setAdapter(adapter);
gv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//selection.setText(characters[position]);
Toast.makeText(getApplicationContext(),
((TextView) v).getText() , Toast.LENGTH_SHORT).show();
}
});
}
}
主.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
android:id="@+id/gridView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="20px"
android:horizontalSpacing="5px"
android:gravity="center"
android:columnWidth="40dp"
android:stretchMode="columnWidth"
/>
</LinearLayout>
simple_listitrm.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14pt"
android:textStyle="bold"
/>
</LinearLayout>
我得到一个空点异常。当我运行这个应用程序时,我的应用程序被强制关闭。我哪里出错了?