在android中从字符串写入文件

在android中从字符串写入文件,android,string,file,file-handling,Android,String,File,File Handling,我试图做一个应用程序,将预订一个空教室,但当我从文件中读取它的罚款,但当我写了一个获取空指针引用错误 这是我试图存储在文件中的字符串。这里是我的代码。 当我点击上一个intent的按钮来访问此intent应用程序时崩溃。但是如果我对“button.setOnClickListener(new View.OnClickListener()”这个块进行注释,它就可以正常工作 public class result extends AppCompatActivity { Button button;

我试图做一个应用程序,将预订一个空教室,但当我从文件中读取它的罚款,但当我写了一个获取空指针引用错误 这是我试图存储在文件中的字符串。这里是我的代码。 当我点击上一个intent的按钮来访问此intent应用程序时崩溃。但是如果我对“button.setOnClickListener(new View.OnClickListener()”这个块进行注释,它就可以正常工作

public class result extends AppCompatActivity {

Button button;
TextView tv1;
TextView tv2;
TextView tv3;
EditText ed1;
public String f;
public static String[] strarray = new String[3];
public String slot;
public int roomno;


String[] mystr = null;
int i=0,j

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    Intent intent = getIntent();
    f = intent.getExtras().getString("day");
    slot=intent.getExtras().getString("time");
    tv1 = (TextView) findViewById(R.id.tv1);
    tv1.setText(f);
    tv1 = (TextView) findViewById(R.id.tv1);
    tv2 = (TextView) findViewById(R.id.tv2);
    tv3 = (TextView) findViewById(R.id.tv3);


    InputStream fin = null;
    try {
        fin = openFileInput(f);
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
    InputStreamReader ip = new InputStreamReader(fin);
    BufferedReader br = new BufferedReader(ip);
    int c;
    String str = "";
    String abc = "";
    String def = "";
    String xyz = "";
    int j=0;
    try {
        while((str = br.readLine())!= null)
        {

            String temp[] = str.split(" ");
            if (temp[0].compareTo(slot) == 0) {


                if(temp[1].compareTo("0") == 0) {
                    abc = "1301";
                    tv1.setText(abc);
                }
                if(temp[2].compareTo("0") == 0) {
                    def = "1302";
                    tv2.setText(def);
                }
                if(temp[3].compareTo("0") == 0) {
                    xyz = "1303";
                    tv3.setText(xyz);
                }
            }
        }

    } catch (IOException e1) {
        e1.printStackTrace();
    }





   // Toast.makeText(getBaseContext(), "file read", Toast.LENGTH_SHORT).show();
    button.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            int i = 0,j;
            String line;

            try {
                BufferedReader br = new BufferedReader(new FileReader(f));
                StringBuffer str = new StringBuffer();

                while((line = br.readLine()) != null)
                {
                    strarray[i] = line;     //Store each line i file in string array.
                    i++;
                }
                for(j=0; j<=i; j++)
                {
                    if(j==0)            //This i hav taken for 2nd slot (starting from 0)
                    //make it if(j==timeslot/time-1)
                    {
                        String temp = strarray[j];
                        //temp = trim(temp);
                        mystr = temp.split(" ",4);          //Processing that particular line ka string.
                        for(int k=0; k<4; k++)
                            //System.out.println(mystr[k]);
                        mystr[roomno] = "1";                        //Changing from 0 to 1 where classroom = 1301 hence here index=1 in mystr[1] = '1'.
                        //Make it mystr[roomno] = "1";


                        str.append(mystr[0]);
                        str.append(" ");
                        str.append(mystr[1]);
                        str.append(" ");
                        str.append(mystr[2]);           //Creating a new changed string
                        str.append(" ");
                        str.append(mystr[2]);



                        strarray[j] = str.toString();   //Since its a buffer convert it to string.

                    }
                }

                br.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            FileWriter fileWriter = null;
            try {
                fileWriter = new FileWriter(f);
            } catch (IOException e) {
                e.printStackTrace();
            }

            // Always wrap FileWriter in BufferedWriter.
            BufferedWriter b = new BufferedWriter(fileWriter);

            for(int m=0; m<3; m++)
            {
                String temp = strarray[m];   //Write back everything to a file.
                try {
                    b.write(temp);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    b.newLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                b.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                b.close();
            } catch (IOException e) {
                e.printStackTrace();
            }



        }});
    }

}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u结果);
Intent=getIntent();
f=intent.getExtras().getString(“日”);
slot=intent.getExtras().getString(“时间”);
tv1=(TextView)findViewById(R.id.tv1);
tv1.setText(f);
tv1=(TextView)findViewById(R.id.tv1);
tv2=(TextView)findViewById(R.id.tv2);
tv3=(TextView)findViewById(R.id.tv3);
InputStream fin=null;
试一试{
fin=openFileInput(f);
}捕获(FileNotFoundException e1){
e1.printStackTrace();
}
InputStreamReader ip=新的InputStreamReader(fin);
BufferedReader br=新的BufferedReader(ip);
INTC;
字符串str=“”;
字符串abc=“”;
字符串def=“”;
字符串xyz=“”;
int j=0;
试一试{
而((str=br.readLine())!=null)
{
字符串temp[]=str.split(“”);
如果(温度[0]。与(插槽)==0)比较{
如果(温度[1]。比较到(“0”)==0){
abc=“1301”;
tv1.setText(abc);
}
如果(温度[2]。比较到(“0”)==0){
def=“1302”;
tv2.setText(def);
}
如果(温度[3]。比较(“0”)==0){
xyz=“1303”;
tv3.setText(xyz);
}
}
}
}捕获(IOE1异常){
e1.printStackTrace();
}
//Toast.makeText(getBaseContext(),“文件读取”,Toast.LENGTH_SHORT.show();
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
int i=0,j;
弦线;
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(f));
StringBuffer str=新的StringBuffer();
而((line=br.readLine())!=null)
{
strarray[i]=line;//将每行i文件存储在字符串数组中。
i++;
}

对于(j=0;j>>>START com.android.internal.os.RuntimeInit uid 0>START com.android.internal.os.RuntimeInit uid 0,您不能对从未初始化为非null的按钮引用调用setContentView()

试试像这样的东西

button = (Button) findViewById(R.id.whateverYouCalledYourButtonInTheXML);
您随后的错误是:

open failed: EROFS (Read-only file system

如果未能指定适当的位置,则表示您正试图写入不允许的位置(可能是设备的根目录)中的文件。您可能需要预先编写类似于
getfiledir()结果的内容
调用了一个有效的上下文,例如您的活动本身。

始终有助于包含准确的错误消息。感谢您帮助我们现在可以转到“结果”活动,但现在当我单击结果活动“应用程序崩溃”按钮时,应用程序崩溃在这里不是一个可接受的问题陈述。但是您的try/catch设置不是很好-您已经处理了失败,然后继续尝试在try{}之外使用结果即使失败了。查看日志中的前面部分,查看获取FileWriter是否失败,并向代码中添加一些积极的日志记录,以查看这是否有效。添加了从第一个活动到最后一个活动的整个日志