我有一个自定义操作栏和附加到它的onclick,它在后台运行,出于某种奇怪的原因我的应用程序强制关闭。 我在doinbackground中进行网络操作 下面附有代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enroll);
final ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(enroll.this);
final View mCustomView = mInflater.inflate(R.layout.custom_actionbar_enroll, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text1);
mTitleTextView.setText("True Tracer");
String fontPath = "fonts/stasmic_.ttf";
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
mTitleTextView.setTypeface(tf);
final ImageButton imageButton = (ImageButton) mCustomView
.findViewById(R.id.imageButton1);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
tpass=(TextView)findViewById(R.id.tip);
otp1=(EditText)findViewById(R.id.otp1);
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
String samp=pref.getString("mynumber", null);
tpass.setText("Self authentication SMS sent \n with activation code to "+samp);
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
backend runner = new backend();
runner.execute();
}
});
}
class backend extends AsyncTask<String, Void, Result>
{
@Override
protected Result doInBackground(String... params) {
do something();
}
}
答案 0 :(得分:0)
在您点击时,请像这样调用异步任务
new backend().execute();
答案 1 :(得分:0)
为什么你声明你的asynctask需要String类型的参数,但是当你调用execute时你不发送任何参数?此外,doInBackground返回Result类型的对象。你在onPostExecute中对该对象做了什么?要知道问题是什么,有两件事是很重要的:异常以及你在AsyncTask中做了什么。