2013-12-12 63 views
0

的onCreate的全部代碼是在這裏:爲什麼我必須在函數中調用XML元素,而不是在onCreate中調用?

//Global variables 
ExpandableListAdapter listAdapter; 
ExpandableListView expListView; 
List<String> listDataHeader; 
HashMap<String, List<String>> listDataChild; 
int expandedGroup = -1; 
boolean notification; 
boolean vibrate; 
String ringtone; 
int hour; 
int minutes; 
int day; 

//UI elementi 
TextView dan; 
TextView ura; 
ImageView facebook; 
ImageView twitter; 
TextView tw1, tw2, tw3, tw4, tw5, tw6, tw7, tw8, tw9, tw10, tw11, tw12, tw13, tw14, tw15, tw16, tw17, tw18, tw19, tw20, tw21, tw22, tw23, tw24, tw25, tw26, tw27, tw28, tw29, tw30; 
Button nadomescanja; 

//Mapa aplikacije 
public static File appDir = new File(Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.whizzapps.stpsurniki/"); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Mapa aplikacije se naredi, če ne obstaja 
    if (!appDir.exists()) 
    { 
     appDir.mkdirs(); 
    } 

    //Povezava z elementi iz navigacije 
    expListView = (ExpandableListView) findViewById(R.id.lvExp); 
    dan = (TextView) findViewById(R.id.dan); 
    ura = (TextView) findViewById(R.id.ura); 
    tw15 = (TextView) findViewById(R.id.textView22); 
    facebook = (ImageView) findViewById(R.id.facebook); 
    twitter = (ImageView) findViewById(R.id.twitter); 

    //Nastavitev glave in noge 
    View headerView = ((LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header, null, false); 
    View footerView = ((LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false); 
    expListView.addHeaderView(headerView); 
    expListView.addFooterView(footerView); 

    //Nastavitev ExpandableListView adapterja 
    prepareListData(); 
    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); 
    expListView.setAdapter(listAdapter); 

    //Klicanje pomembnih funkcij 
    nadomescanjaClickListener(); 
    setCustomFontToAllTextViews(); 
    loadRandomQuote(); 
    getDay(); 
    getTime(); 
    expListViewClickListeners(); 
    posodabljanjeUre(); 
} 

因此,大家可以看到,onCreate方法之前,我聲明全局變量,然後在實例的onCreate這些變量。然後幾行後,我調用了所有需要在onCreate中執行的方法。但是這些方法會因爲nullpointerexception而使應用程序崩潰(它們還沒有得到變量)。但如果我刪除 onCreate中的這些變量,並在方法本身中實例化它們然後它工作。

我現在不能發佈logcat,因爲我不在家,但我可以讓你知道這是一個簡單的nullpointerexception錯誤,因爲它沒有得到我在onCreate中設置的那些變量。

+0

發佈您的logcat,以便我們可以幫助您 – Apoorv

+0

函數的含義在哪裏?你可以發佈活動代碼嗎? – Raghunandan

+0

你的問題還不清楚,甚至代碼也。請以正確的流程發佈整個代碼並清除您的問題。 @Matthew – GrIsHu

回答

0

臉譜和嘰嘰喳喳ImageView不是你的活動佈局的一部分,直到他們被添加。所以在添加Views之前,findViewById()將返回null。在將footerView添加到expandableListView後搜索twitter和facebook Views。

expListView.addFooterView(footerView); 
facebook = (ImageView)footerView.findViewById(R.id.facebook); 
twitter = (ImageView)footerView.findViewById(R.id.twitter); 
+0

我已經這樣寫過了。檢查我在問題中發佈的代碼。我發佈了完整的onCreate。 – Guy

+0

這實際上很有意義,非常感謝! – Guy

相關問題