嗨,我正在尝试添加一个新的SDK值到healthvault,当我使用wEIGHT提供的代码时,它写得很好,我拆分了代码,以便与另一个UI.Now一起使用,它给我错误,Logcat显示没有错误。错误消息是“发生异常:空”
这是我的代码
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.customlist.R;
import com.microsoft.hsg.Request;
import com.microsoft.hsg.android.HealthVaultService;
import com.microsoft.hsg.android.demo.hv.Weight;
import com.microsoft.hsg.request.SimpleRequestTemplate;
public class AddWeight extends Activity {
private HealthVaultService service;
// private Record selectedRecord;
String recd_id = "";
String persn_id = "";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addweight);
service = HealthVaultService.getInstance();
Intent intent = getIntent();
recd_id = intent.getExtras().getString("recd_id");
persn_id = intent.getExtras().getString("persn_id");
Button save = (Button) findViewById(R.id.savebutton);
save.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText text = (EditText) findViewById(R.id.edt_wght);
PutWeight putAction = new PutWeight(text.getText().toString());
putAction.execute();
}
});
Button backpage = (Button) findViewById(R.id.backbutton);
backpage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
service.reset();
Intent i = new Intent(AddWeight.this,WeightActivity.class);
startActivity(i);
finish();
}
});
}
private class PutWeight extends AsyncTask<Void, Void, Void> {
private String weight;
private Exception exception;
ProgressDialog progressDialog;
public PutWeight(String weight) {
this.weight = weight;
progressDialog = ProgressDialog.show(
AddWeight.this,
"",
"Saving Data",
true);
}
protected Void doInBackground(Void... v) {
try {
putWeight(weight);
} catch(Exception e) {
exception = e;
}
return null;
}
/* (non-Javadoc)
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Void v) {
progressDialog.dismiss();
if (exception == null) {
Intent i=new Intent(AddWeight.this,WeightActivity.class);
startActivity(i);
finish();
} else {
Toast.makeText(
AddWeight.this,
"An error occurred. " + exception.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}
private void putWeight(String value)
{
Weight weight = new Weight(Double.parseDouble(value));
SimpleRequestTemplate template = new SimpleRequestTemplate(
service.getConnection(),
persn_id,
recd_id);
StringBuilder infoBuilder = new StringBuilder();
infoBuilder.append("<info><thing><type-id>");
infoBuilder.append(Weight.TYPE);
infoBuilder.append("</type-id><data-xml>");
infoBuilder.append(weight.toXml());
infoBuilder.append("<common/></data-xml></thing></info>");
Request request = new Request();
request.setMethodName("PutThings");
request.setInfo(infoBuilder.toString());
template.makeRequest(request);
}
}发布于 2014-08-21 16:23:03
您正在重写AsyncTask构造函数,并且没有调用super()。无论如何,这样做有什么目的吗?您可以在onPreExecute()方法中开始显示对话框,并将weight变量传递给execute方法。只需调整您的AsyncTask,以便doInBackground可以接受参数,并像new PutWeight().execute(text.getText().toString())一样调用它
https://stackoverflow.com/questions/25421314
复制相似问题