java - How do put each AsyncTask class in a separate file? -
i have few asynstask definined inside of main activity. tried make code more modular putting each 1 of these classes on separate file. unfortunately keep getting errors such not being able intents work. how connect code main activity. way if place code is(without imports) in mainactivity works fine. thanks
package com.example.food4thought; import java.net.url; import twitter4j.twitterexception; import twitter4j.auth.requesttoken; import android.content.intent; import android.net.uri; import android.os.asynctask; import android.util.log; import android.widget.toast; // starts intent loads web browser , asks user log in twitter // , pin# public class twitterlogin extends asynctask<url, integer, requesttoken> { protected requesttoken doinbackground(url... arg0) { try { requesttoken = twitter.getoauthrequesttoken(); log.i("got request token", "food4thought"); } catch (twitterexception e) { log.i("failed request token", "food4thought"); } //log.i(requesttoken.getauthorizationurl(), "food4thought"); //requesttoken.getauthorizationurl(); //log_in.settext(); try { intent browserintent = new intent(intent.action_view, uri.parse(requesttoken.getauthorizationurl())); startactivity(browserintent); } catch(nullpointerexception e) { runonuithread(new runnable() { public void run() { toast.maketext(getapplicationcontext(), "unable log in, no access internet.", toast.length_long).show(); } }); } return null; } }
to need understand dependencies asynctask has.
to fire intents need context
intance. see twitter
variable.
so need declare appropriate fields , pass objects twitterlogin constructor.
something that:
public class twitterlogin extends asynctask<url, integer, requesttoken> { private context context; //other fields here public twitterlogin(context context, ...){ // other variables here this.context = context; //other fields assignment } }
later can fire intent:
context.startactivity(browserintent);
what's important understand methods startactivity not "global functions", rather methods of class instance, , can't call methods asyctask instance.
Comments
Post a Comment