开发者

Variable scope in Android

i'm new to android and i'm having some trouble accessing variables within an application. ive tried accessing it in the onClick method but its giving me erros. how do i read it? here is the simplyfied code:

public class DBDetails extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dbdetails);
        new fetchDBDetails(this).execute();

        Button ButtonCall = (Button) findViewById(R.id.ButtonCall);
        ButtonCall.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //READING VALUE BELOW
        startActivityForResult(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone))开发者_开发技巧, 1);
           }
        });
      }
}

    class fetchDBDetails extends AsyncTask<String, Void, String> {
        private DBDetails fetchDBDetailsContext = null;
        String phone;

        public fetchDBDetails(DBDetails context) {
            fetchDBContext = context;
        }
        @Override
        protected String doInBackground(String... params) {
                    phone = "12345678"; //ASSIGNING VALUE HERE
        }
    }

thanks in advance


You have different issues here. First, the user may click the button before your ASyncTask finishes (race condition).

phone is a member of fetchDBDetails and is not accessible as just phone' inDBDetails`

You need to somehow pass the phone variable back from the fetchDBDetails class to your DBDetails class.

EDIT:

You can add a public member variable public String phone in DBDetails and do

fetchDatenbankDetailsContext.phone = telefon;

at the end of onPostExecute()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜