我有一个名为“全球”的课程和另外两个活动。在每个活动中,我想创建一个Global类的实例,用于读取名为“textfile”的文本文件的第一行。出于某种原因,它不起作用
这是Global类的代码(在文件Global.java中):
import android.app.Activity;
public class Global extends Activity {
public String line;
public Global() {
InputStream file = getResources().openRawResource(R.raw.textfile);
BufferedReader input = new BufferedReader(new InputStreamReader(file));
try {
line = input.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以下是名为“HelloWorld”的antivity的代码(在文件HelloWorld.java中),它有一个Global类的实例,并显示“textfile”的第一行
public class HelloWorld extends Activity{
Global gb;
TextView myTV;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.helloworld);
gb=new Global();
myTV = (TextView) findViewById(R.id.textView1);
myTV.setText("First line is: "+gb.line);
}
}
答案 0 :(得分:1)
import android.app.Activity;
public class Global extends Activity {
static String line="";
public Global(Activity mainactivity)
{
InputStream file = mainactivity.getResources().openRawResource(R.raw.textfile);
BufferedReader input = new BufferedReader(new InputStreamReader(file));
try {
line = input.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class HelloWorld extends Activity{
Global gb;
TextView myTV;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.helloworld);
gb=new Global((Activity)this);
myTV = (TextView) findViewById(R.id.textView1);
myTV.setText("First line is: "+gb.line);
}
}
答案 1 :(得分:0)
试试这个......
public class Global extends Activity {
public String line;
public Global() {
try{
InputStream inputStream = getApplicationContext().getResources().openRawResource(R.raw.textfile);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader input = new BufferedReader(inputreader);
String s = null;
while ((s=input.readLine())!=null)
{
line = s;
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
答案 2 :(得分:0)
不需要不必要地具有全局扩展活动 类。
您可以做的是在全局构造函数中传递上下文。
正确的方法是
<强> HelloWorld.java 强>
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorld extends Activity{
Global gb;
TextView myTV;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.helloworld);
gb=new Global(this);
myTV = (TextView) findViewById(R.id.textView1);
myTV.setText("First line is: "+gb.getLine());
}
}
<强> Global.java 强>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Global {
private AbcActivity abcActivity;
public String line;
public Global(AbcActivity abcActivity) {
this.abcActivity = abcActivity;
}
public String getLine() {
InputStream file = abcActivity.getResources().openRawResource(
R.raw.textfile);
BufferedReader input = new BufferedReader(new InputStreamReader(file));
try {
line = input.readLine();
return line;
} catch (IOException e) {
e.printStackTrace();
return "Error reading File!";
}
}
}