簡體   English   中英

自定義操作欄onclick不適用於異步任務-android

[英]Custom Action bar onclick does not work for async task - android

我有一個自定義操作欄,並在其上附加了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();
}
}

在您點擊時,調用異步任務,如下所示

           new backend().execute(); 

為什么要聲明asynctask期望使用String類型的參數,但是在調用execute時卻不發送任何參數? 另外,doInBackground返回結果類型為Result的對象。 您在onPostExecute中使用該對象做什么? 了解問題所在很重要的兩件事:異常以及您在AsyncTask中正在做什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM