Android如何在超类中设置标题

时间:2012-09-25 11:10:43

标签: android inheritance titlebar

我有一组activities,它们都延伸baseActivity

我正在尝试设置title in the BaseActivity,以便它反映在所有子活动中。

该应用正在登录用户。我在登录后尝试在每个屏幕上都有username across the title

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

    public class NfcBaseActivity extends Activity{

        private static final String TAG = NfcBaseActivity.class.getSimpleName(); 
        String carername;

        public String getCarername() {
            return carername;
        }

        public void setCarername(String carername) {
            this.carername = carername;
            Log.e(TAG, "setting carername to " + this.carername);
            setTitle(carername + " is currently logged in");
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //setTitle(getCarername() + " is logged in");
        }

    }   

    @Override
        protected void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.entryscreen); 
            nfcscannerapplication = (NfcScannerApplication) getApplication();
            //loginWebservice = new LoginWebservice(this);
            loginWebservice = nfcscannerapplication.loginWebservice;
            userName = (EditText)findViewById(R.id.username);
            passwordPin = (EditText)findViewById(R.id.password);

            login = (Button)findViewById(R.id.buttonlogin);
            login.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    ////////////get user's input///////////


                     compId = "100";
                    String theUsername = userName.getText().toString();
                    thePassword = passwordPin.getText().toString();
                    String loginType = "1";



                    String[] params = new String[]{compId, theUsername, thePassword, loginType};

                    //validate user Asynchonously on background thread

                    AsyncValidateCarer avc = new AsyncValidateCarer();
                    avc.execute(params);
                EntryActivity.this.setCarername(fullName);

                }// end of onclick
            });

我调用AsyncTask来验证用户对webservice的影响,然后将实例变量fullName设置为用户名。

从Activity onClick()调用此AsyncTask。我使用以下命令调用活动的超类方法setcarer(),它在超类中设置标题。

我希望这个更改会反映在扩展超类的所有其他类中。

为什么没有反映出来?

EntryActivity.this.setCarername(fullName);

1 个答案:

答案 0 :(得分:0)

可能是因为您的fullName变量在调用EntryActivity.this.setCarername(fullName);时仍为空。

我假设您在AsyncTask中的某处设置了此变量,因此您必须在AsyncTask的EntryActivity.this.setCarername(fullName);方法中调用onPostExecute,因为avc.execute(params);会继续,而不会等待AsyncTask完成。< / p>