I have one class called “Global” and two other activities. In each activity I want to create an instance of class Global for reading the first line of a text file called “textfile”. For some reason, it does not work
Here is the code of Global class (in file 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();
}
}
}
Here are codes for a antivity called “HelloWorld” (in file HelloWorld.java) which has an instance of class Global and is to display of first line of “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);
}
}
The correct way would be
HelloWorld.java
Global.java