0

这是我第一次发帖

我是新人,所以对我好!

我正在关注 travis 的教程,它用于保存数据和使用异步任务

我真的很专注,但我找不到我的代码有什么问题,所以我在这里发布!: 我加了logcat!它在没有异步和进度条的情况下工作(保存和加载)

最新更改!:我修复了进度条,但 loadwithasync 类不起作用,我的意思是这一行:我认为这必须返回 Srting ld 并在文本视图 res 中设置它。但它不是这样看的!为什么来自 mybringback 的 travis!没有写像 Strig s = new loadWith..... 这样的行吗?你能告诉我问题出在哪里吗!我很困惑,我不知道如何正确调试!新的 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

0

当您查看错误时,您会看到onProgressUpdate()引发 NPE。看代码,有两种可能:1.pd为空或2.progress为空。在此处添加断点或一些日志记录以查看到底发生了什么。

protected void onProgressUpdate(Integer...progress){
    pd.incrementProgressBy(progress[0]);
}
于 2013-11-05T02:05:42.723 回答