我在一些地方读到有必要在每个类中都有一个 main 方法,如下所示:
public static void main(String args [ ]) { }
但是,我当前项目中的类都不包含这样的方法,并且到目前为止我的应用程序没有遇到任何问题...这是我的类之一供引用。
public class GridAdapter extends BaseAdapter {
private final String[] classes = {"Database"}; // Sets the labels for each button
private Context mContext;
public GridAdapter(Context c) {
mContext = c;
}
public int getCount() { //autogenerated tab, returns length of an array.
return mThumbIds.length;
}
// The position an item is in in an array.
public Object getItem(int position) {
return mThumbIds[position];
}
// Gets the ID of each item in the array.
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) { // if it's not recycled, initialize some attributes
gridView = new View(mContext);
gridView = inflater.inflate(R.layout.gridset, null);
TextView textView = (TextView) gridView
.findViewById(R.id.label);
textView.setText(classes[position]);
ImageView imageView = (ImageView) gridView
.findViewById(R.id.img);
imageView.setImageResource(mThumbIds[position]);
} else {
gridView = convertView;
}
return gridView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.img};
}
是因为我正在扩展某些东西(在本例中为 BaseAdapter)吗?现在,当前已完成并实际运行的类有一个扩展,所以我想知道我的 WIP 类是否不需要 main() 方法。
最佳答案
您的引用内容为“至少在一个类中”并且属于独立的 Java SE 程序。
然而,在 Android 中,您根本不需要 main
。 Android 操作系统将回调调用到您的 Activity,例如 onCreate
,从而使您的 Activity 栩栩如生。 , onPause
等等...
关于java - 类需要一个 main 方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33484159/