我把所有的asynctask都放在一个文件中,但它有点乱,所以我决定将它们放在单独的文件/类中。我不断收到一些错误。我做错了什么,谢谢
在我的mainActivity.and 错误内:
//Handler for the PIN. Leads to the Internet Intent
public void log_in(View v) {
new TwitterLogin(null, null, null).execute();
}
Description
The constructor TwitterLogin() is undefined MainActivity.java
TwitterLogin.java
//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> {
private Context context;
private Activity Activity;
public Twitter twitter = TwitterFactory.getSingleton();
public RequestToken requestToken = null;
public TwitterLogin(Context context, Twitter twitter,
RequestToken requestToken) {
this.context = context;
this.twitter = twitter;
this.requestToken = requestToken;
}
@Override
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()));
context.startActivity(browserIntent);
}
catch (NullPointerException e) {
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context.getApplicationContext(),
"Unable to log in, No access to the Internet.",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
}
这是logcat
04-24 21:27:23.635: E/AndroidRuntime(19644): Process: com.example.food4thought, PID: 19644
04-24 21:27:23.635: E/AndroidRuntime(19644): java.lang.RuntimeException: An error occured while executing doInBackground()
04-24 21:27:23.635: E/AndroidRuntime(19644): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-24 21:27:23.635: E/AndroidRuntime(19644): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-24 21:27:23.635: E/AndroidRuntime(19644): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-24 21:27:23.635: E/AndroidRuntime(19644): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-24 21:27:23.635: E/AndroidRuntime(19644): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-24 21:27:23.635: E/AndroidRuntime(19644): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-24 21:27:23.635: E/AndroidRuntime(19644): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-24 21:27:23.635: E/AndroidRuntime(19644): at java.lang.Thread.run(Thread.java:841)
04-24 21:27:23.635: E/AndroidRuntime(19644): Caused by: java.lang.NullPointerException
04-24 21:27:23.635: E/AndroidRuntime(19644): at com.example.food4thought.TwitterLogin.doInBackground(TwitterLogin.java:39)
04-24 21:27:23.635: E/AndroidRuntime(19644): at com.example.food4thought.TwitterLogin.doInBackground(TwitterLogin.java:1)
04-24 21:27:23.635: E/AndroidRuntime(19644): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-24 21:27:23.635: E/AndroidRuntime(19644): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-24 21:27:23.635: E/AndroidRuntime(19644): ... 4 more
答案 0 :(得分:2)
您的constructor
需要3 params
...其中第一个是Context
。您已将null
传递给所有这些,这意味着您在此处获得了NPE
context.startActivity(browserIntent);
在这里
requestToken = twitter.getOAuthRequestToken();
在这里
Uri.parse(requestToken.getAuthorizationURL()));
因为context
是null
。
您可能希望将其更改为
// assuming MainActivity is the name of your Activity
new TwitterLogin(MainActivity.this, null, null).execute();
twitter
和requestToken
也是null
,看起来你首先引用twitter
,这可能会给你的例外。您正在为这些变量传递null
,然后尝试使用发送的null
参数初始化它们。
修改强>
这也是一团糟,因为你不应该尝试更新doInBackground()
中的用户界面
catch (NullPointerException e) {
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context.getApplicationContext(),
"Unable to log in, No access to the Internet.",
Toast.LENGTH_LONG).show();
使用AsyncTask
为您提供的方法。