2015-06-10 15 views
0

我爲我的Android應用程序創建了一個簡單的類,名爲coreTuts。我用它綁到做我MainActivity.javaactivity_main.xml,等等,像這樣:在另一個簡單的類中使用getApplicationContext(),而不是在MainActivity.java中

MainActivity.java

coreTuts tuts = new coreTuts(); 
 
\t 
 
public void displayToast(View view) 
 
{ 
 
\t tuts.sampleToast(); 
 
}
這是我的 coreTuts.java樣子:

coreTuts.java

public class coreTuts{ 
 

 
\t 
 
\t //Do a toast notification 
 
\t public void sampleToast() 
 
\t { 
 
\t \t 
 
\t \t Toast toast = Toast.makeText(getActivity().getApplicationContext(), 
 
\t \t   "This is a message displayed in a Toast", 
 
\t \t   Toast.LENGTH_SHORT); 
 
\t \t  
 
\t \t toast.show(); 
 
\t } 
 

 
}

我不能決定我是否應該使用getActivity().getApplicationContext()或者只是getApplicationContext()Toast因爲無論代碼不起作用。

事實上,這是我的問題:

  1. 我瞭解Android的上下文是有點像棲息地的動物。我是對的,如果我看看getActivity()getApplicationContext()那樣?
  2. 我如何讓getApplicationContext()在另一個班級工作,以便我可以運行我的toast,或者甚至允許?

謝謝!

+0

你可以通過上下文實例作爲參數傳遞給sampleToast()方法(上下文上下文) – dora

+0

我認爲這個問題有很多問題。他們都說你需要傳遞一個Context對象給這個類才能得到它。例如,sampleToast(Context context){... makeText(context,....)} – Surely

+0

我已經查看了前三個答案,看起來我的應用程序的Context總是從MainActivity.java中獲取它的值。 –

回答

2

您coreTuts應該像下面

public class coreTuts{ 


//Do a toast notification 
public void sampleToast(Context context) 
{ 

    Toast toast = Toast.makeText(context, 
      "This is a message displayed in a Toast", 
      Toast.LENGTH_SHORT); 

    toast.show(); 
} 

} 

,你可以調用它像下面,

coreTuts tuts = new coreTuts(); 

public void displayToast(View view) 
{ 
    tuts.sampleToast(view.getContext()); 
} 

注:鑑於不能爲空

+0

我有兩個後續問題:我應該永遠記住'MainActivity.java'中,我的應用程序中顯示內容的所有方法應該始終在內部具有'View view'參數?在'sampleToast()'中,'Context context'可以放在方法的其他地方嗎? :) –

+1

@MonicaLabbao歡迎您。 1)你不需要在裏面有這個視圖參數。你可以使用MainActivity.this(我的意思是每當你需要一個上下文時)。 2)我認爲在任何地方,你的意思是你可以把它作爲第二個參數或第三個參數來傳遞。是的,你可以這麼做。 – dora

0

當你的類CoreTuts不繼承Activity或其他Context子類(ActivityContext的子項),您無法訪問您嘗試的方式。你需要明確它傳遞給你的sampleToast方法,像這樣:

public class coreTuts{ 

    //Do a toast notification 
    public void sampleToast(Context context) { 
     Toast toast = Toast.makeText(context, 
       "This is a message displayed in a Toast", 
       Toast.LENGTH_SHORT); 
     toast.show(); 
    } 

} 

而在你的活動:

coreTuts tuts = new coreTuts(); 

public void displayToast(View view) { 
    // Pass your activity as the context 
    tuts.sampleToast(this); 
} 
+0

明白了,當我的類coreTuts沒有繼承Activity或Context的子類時,必須在方法內部寫上下文上下文參數。如果我不將它用於其他任何東西,你會建議這種將'Context context'作爲參數的方法嗎? :) –

+0

是的!如果你不打算將'Context'用於其他任何事情,則不需要引用它(實際上,因爲你的活動的生命週期它比看起來更復雜)。這樣你就可以在需要時使用它。 – peguerosdc

+0

如果你想知道更多關於持有對上下文的引用,你可以開始看看[this](http://possiblemobile.com/2013/06/context/)。 – peguerosdc

0

傳遞一個上下文你​​coretuts類當你創建它的對象。你的coretuts類看起來像這樣。

public class coreTuts{ 

private Context mContext; 

public coreTuts(Context context) { 
    mContext = context; 
} 

//Do a toast notification 
public void sampleToast() 
{ 

    Toast toast = Toast.makeText(mContext, 
      "This is a message displayed in a Toast", 
      Toast.LENGTH_SHORT); 

    toast.show(); 
} 

} 

現在,當你創建該類的MainActivity內部的物體,只需通過像sampleToast上下文

// Pass your context. You can also use getApplicationContext() instead of MainActivity.this 
coreTuts tuts = new coreTuts(MainActivity.this); 

// You don't really need a view argument for this method. 
// It could just be public void displayToast() {...} 
public void displayToast(View view) 
{ 
    tuts.sampleToast(); 
} 
相關問題