4
我在非Activity類中得到了以下方法,我的代碼如下。來自非活動類的非活動類中的Android getResource?
public class ReadTextByLineNo {
public void setContext(Context _context) {
if (context == null) {
context = _context;
}
}
public String getTextByLine(int Filename,int LineNumber)
{
String output="";
String line="";
int counter=1;
try
{
InputStream in = context.getResources().openRawResource(Filename);
//InputStream in = assetManager.open(Filename);
if(in!=null)
{
InputStreamReader input = new InputStreamReader(in);
BufferedReader buff = new BufferedReader(input);
while((line=buff.readLine())!=null)
{
if(counter ==LineNumber){
output=line;
}counter++;
}in.close();
}else{
Log.e("Input STREAM PROBLEM", "TEXT IS NULL NULL NULL NULL NULL");
}
}catch(Exception e)
{
//log
}
return output;
}
**我從NON_ACTIVITY CLASS類似這樣的**調用此方法
class sample implements Isample
{
ReadTextByLineNo read = new ReadTextByLineNo();
String subMsg = read.getTextByLine(R.raw.subtitle, storySceneId);
//the above string is to called from an activity called Layout
}
如何使用資源/上下文從非活性類?我不能在構造函數中使用上下文,因爲我也從非Activity類調用該方法。 所以我不能設置read.setContent(this);在我的ReadtextByLineNo類中獲得了setContext方法,感謝您的幫助。
請幫我拿到類樣本,例如通過代碼在上下文/國土資源是通過調用
ReadTextByLineNo.setContext(getApplicationContext());
從
讚賞
即時消息不從調用該方法因此如何在非Activity類中使用此類ReadTextByLineNo.setContext(getApplicationContext()); – optimus 2012-01-02 23:04:42
老兄,不要從非Activity中調用這個方法,從其他任何活動中調用這個方法來設置上下文,現在不要告訴我你在你的應用中沒有任何Activity。 :D,從你的第一個活動中調用它,只是爲了設置上下文,這很簡單... – AAnkit 2012-01-03 05:54:14
soory起初我不明白,但工作完美,thx很多傢伙... – optimus 2012-01-03 12:12:33