0

これは私の最初の投稿です

私は新しいので、私と仲良くしてください!

私はtravisのチュートリアルに従っており、データの保存と非同期タスクの使用に行きます

私は本当に集中しましたが、コードの何が問題なのかを見つけることができないので、ここに投稿しました! : logcat を追加しました! 非同期と進行状況バーなしで機能しました(保存と読み込みの両方)

最新の変更!: プログレス バーを修正しましたが、loadwithasync クラスが機能していません。つまり、この行を意味します: これは Srting ld を返し、それをテキスト ビュー res に設定する必要があると思います。しかし、それはこのように見えません!なぜ mybringback のトラヴィスなのか! Strig s = new loadWith..... のような行を書きませんでしたか? どこに問題があるのか​​教えてください!私は混乱していて、適切にデバッグする方法がわかりません!! new loadWithAsyncTask().execute(FILENAME);

 public class SaveAndLoadInternal extends Activity implements OnClickListener {
EditText file, data;
TextView res;
FileInputStream fis;
FileOutputStream fos;
String FILE_NAME;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.save_load_internal);
    Button load, save;
    file = (EditText) findViewById(R.id.etSLIfile);
    data = (EditText) findViewById(R.id.etSLIdata);
    res = (TextView) findViewById(R.id.tvSLIres);
    load = (Button) findViewById(R.id.bSLIload);
    save = (Button) findViewById(R.id.bSLIsave);

    // set file and close it!

    load.setOnClickListener(this);
    save.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    FILE_NAME = file.getText().toString();
    switch (v.getId()) {
    case R.id.bSLIload:
        //Commented just for doing some tweaks! run 
        //loading process in another thread to give UI thread rest :D for          avoid hanging!
        FileInputStream fis = null;
        String ld = "LOADING FAILED!";

  /*            try {
            fis = openFileInput(FILE_NAME);
            byte[] b = new byte[fis.available()];
            while (fis.read(b) != -1) {
                ld = new String(b);
            }

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

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        res.setText(ld);
 */
        new loadWithAsyncTask ().execute(FILE_NAME);
        // execute will run doInBackground method!

        break;
    case R.id.bSLIsave:
        String sd = data.getText().toString();
/*
        // one way to save in file is below! must work but it isn't!
        File f = new File(FILE_NAME);
        try {
            fos = new FileOutputStream(FILE_NAME); 
            fos.write(sd.getBytes());
            fos.close();
            res.setText("SAVING DONE!");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 */
        try {
            fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
            fos.write(sd.getBytes());
            fos.close();
            res.setText("SAVING DONE!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        break;
    default:
        break;
    }
}
 // /*
// first param: what is being passed in (FILE_NAME) 
// second param for progress bar (we use integer here)
// third one is what we will return! (the saved text! String ld)
public class loadWithAsyncTask extends AsyncTask<String, Integer, String>{
    ProgressDialog pd;
    String Ld = "LOADING FAILED!";
    FileInputStream fis = null;
    // this gonna called first
    @Override
    protected void onPreExecute(){
        // example: setting up variables or something else!
        pd = new ProgressDialog(SaveAndLoadInternal.this);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMax(100);
        pd.show();

    }

    @Override
    protected String doInBackground(String... params) { 

        //for progress dialog
        for(int i =0 ; i< 20 ; i++){
            publishProgress(5);
            try {
                Thread.sleep(88);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        pd.dismiss();

        try {
            fis = openFileInput(FILE_NAME);
            byte[] b = new byte[fis.available()];
            res.setText(String.valueOf(fis.available()));
            while (fis.read(b) != -1) {
                Ld = new String(b);
            }


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

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                //return the string!
                return Ld;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    // progress of loading in example!
    @Override
    protected void onProgressUpdate(Integer...progress){

        pd.incrementProgressBy(progress[0]);
    }

}// */
    }
4

1 に答える 1