【问题标题】:Custom Action bar onclick does not work for async task - android自定义操作栏 onclick 不适用于异步任务 - android
【发布时间】:2014-11-15 11:11:23
【问题描述】:

我有一个自定义操作栏和附加在后台运行的 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();
}
}

【问题讨论】:

  • 发布您的 logcat 错误
  • 你有没有在清单文件中提到网络权限?
  • 正在我的手机上运行它..不确定如何获取 logcat

标签: android android-asynctask action


【解决方案1】:

在你的点击中,像这样调用异步任务

           new backend().execute(); 

【讨论】:

    【解决方案2】:

    为什么你声明你的 asynctask 需要 String 类型的参数,但是当你调用 execute 时却没有发送任何参数?此外,doInBackground 返回一个 Result 类型的对象。你在 onPostExecute 中用那个对象做什么?了解问题所在有两点很重要:异常以及您在 AsyncTask 中所做的工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多