我相信我有一個簡單的問題......但對於初學者來說,我再也看不到它了。如何從其他類中獲取var
在一個類中,我想從另一個類中獲取一些var。
類1:
package com.blabla;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//get shared preferences
GetSettings test1 = new RingSettings();
String theSMStext = test1.getSMStext();
Boolean theActivateSMS = test1.getActivateSMS();
}
}
類2:
package com.blabla;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class GetSettings {
/**
* @param args
*/
private String SMStext;
private Boolean ActivateSMS;
public static void RingSettings(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String SMStext = sp.getString("SMStext", "0");
Boolean ActivateSMS = sp.getBoolean("ActivateSMS", false);
}
public String getSMStext(){
return SMStext;
}
public Boolean getActivateSMS(){
return ActivateSMS;
}
}
Eclipse是給我Class1中 「RingSettings不能解析爲一個類型」=>在的getSettings TEST1 =新RingSettings() ;
我在做什麼錯了?
除非你有一個地方叫做類'RingSettings'現在你試圖實例化一個方法'RingSettings',從類'GetSettings'這顯然會失敗。我建議你更多地瞭解Java(以及Android)。也許你想做'GetSettings test1 = new GetSettings();' – Luksprog