2015-11-02 136 views
0

我讀,因爲它有必要在每個類這樣的方法主要地方:是否需要類的主要方法?

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()方法。

+3

沒有,那肯定是沒有必要有一個在所有類;只有在運行應用程序的入口點的類中才有必要。你在哪裏讀過這些? –

+0

你從哪裏讀到的? –

+1

不是。 Android中根本不需要public static void main(String args [])''。 – PPartisan

回答

4

您的參考文獻是「至少在一個類中」,屬於獨立的Java SE程序。 然而,在Android中根本不需要main。您的活動將通過Android操作系統調用回調到你的活動被賦予生命,如onCreateonPause等等

相關問題