Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将每个AsyncTask类放在单独的文件中?_Java_Android_Android Asynctask - Fatal编程技术网

Java 如何将每个AsyncTask类放在单独的文件中?

Java 如何将每个AsyncTask类放在单独的文件中?,java,android,android-asynctask,Java,Android,Android Asynctask,我有一些在我的主要活动中定义的任务。我试图通过将这些类中的每一个放在单独的文件中,使代码更加模块化。不幸的是,我不断地犯一些错误,例如无法让意图发挥作用。如何将此代码与我的主要活动联系起来。顺便说一下,如果我在mainActivity中按原样放置此代码(不包含导入),它就可以正常工作。谢谢 package com.example.food4thought; import java.net.URL; import twitter4j.TwitterException; import twitt

我有一些在我的主要活动中定义的任务。我试图通过将这些类中的每一个放在单独的文件中,使代码更加模块化。不幸的是,我不断地犯一些错误,例如无法让意图发挥作用。如何将此代码与我的主要活动联系起来。顺便说一下,如果我在mainActivity中按原样放置此代码(不包含导入),它就可以正常工作。谢谢

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 an intent that loads up a web browser and asks the user to log in to twitter
    // and get a 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 to get 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 to log in, No access to the Internet.", Toast.LENGTH_LONG).show();

                    }
                });

            }

            return null;
        }

    }
package com.example.food4think;
导入java.net.URL;
导入twitter4j.TwitterException;
导入twitter4j.auth.RequestToken;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.AsyncTask;
导入android.util.Log;
导入android.widget.Toast;
//启动加载web浏览器并要求用户登录twitter的意图
//拿个别针#
公共类TwitterLogin扩展异步任务{
受保护的RequestToken doInBackground(URL…arg0){
试一试{
requestToken=twitter.getOAuthRequestToken();
Log.i(“获得请求令牌”、“food4think”);
}捕获(twitter异常){
Log.i(“无法获取请求令牌”,“food4think”);
}
//Log.i(requestToken.getAuthorizationURL(),“food4think”);
//requestToken.getAuthorizationURL();
//登录.setText();
试一试{
Intent-browserint=newintent(Intent.ACTION_视图,Uri.parse(requestToken.getAuthorizationURL());
startActivity(浏览器内容);
}
捕获(NullPointerException e){
runOnUiThread(新的Runnable(){
公开募捐{
Toast.makeText(getApplicationContext(),“无法登录,无法访问Internet。”,Toast.LENGTH_LONG).show();
}
});
}
返回null;
}
}

要做到这一点,您需要了解异步任务的依赖关系

要激发意图,您需要
Context
intance。我还看到一些
twitter
变量

因此,您需要声明适当的字段并将这些对象传递给TwitterLogin构造函数

诸如此类:

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
    }
}

需要了解的是,所有这些方法(如startActivity)都不是一些“全局函数”,而是某些类实例的方法,您不能仅从AsycTask实例调用这些方法。

您可以添加“一些错误”部分来解决您的问题吗?另外,请注意您的编译错误。如果您了解发生了什么,您可以解决错误。
context.startActivity(browserIntent);