0

我有一个名为“全球”的课程和另外两个活动。在每个活动中,我想创建一个 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”的活动代码(在文件 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);
    }

}

4

3 回答 3

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);
    }
}
于 2012-06-29T05:06:51.760 回答
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();
                  }
            }


} 
于 2012-06-29T04:45:32.087 回答
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());
        }
    }

全球.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!";
        }
    }

}
于 2012-06-29T04:53:54.120 回答