我正在尝试获取我的Android设备中已登录帐户的Google帐户令牌。但是我得到致命异常:AsyncTask#1错误,我的应用程序正在变得有效。
这里我附上了我的源代码和错误文件。
请帮助。
谢谢。
public class Web_view_demo extends ActionBarActivity {
ProgressBar progress;
String mScope="oauth2:" + Scopes.PLUS_LOGIN;;
String accountName="";
final Activity activity = this;
private ProgressDialog dialog1;
String token1="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
Intent intent=getIntent();
final String accountName = intent.getExtras().getString("accountName");
Toast.makeText(this,"Account Selected="+accountName, 5000).show();
new GetGoogleToken().execute();
Toast.makeText(this,"Auth Token="+token1, 5000).show();
}
private class GetGoogleToken extends AsyncTask<Void, Void, String>{
protected void onPreExecute() {
dialog1 = new ProgressDialog(Web_view_demo.this); // App - your main activity class
dialog1.setMessage("Please wait...");
dialog1.show();
}
protected String doInBackground(Void... params) {
Log.i("Inside on AsynTask:","I am here");
try {
token1 = GoogleAuthUtil.getToken(Web_view_demo.this,
accountName,
mScope);
Log.i("Inside on AsynTask:","I am here");
} catch (IOException transientEx) {
// Network or server error, try later
Log.e("IO", transientEx.toString());
} catch (UserRecoverableAuthException e) {
// Recover (with e.getIntent())
Log.e("UserRecover", e.toString());
//Intent recover = e.getIntent();
//startActivityForResult(recover, 1);
} catch (GoogleAuthException authEx) {
// The call is not ever expected to succeed
// assuming you have already verified that
// Google Play services is installed.
Log.e("GoogleAuth", authEx.toString());
}
Log.i("Token:",token1);
return token1;
}
@SuppressWarnings("unused")
protected void onPostExecute(Void unused) {
dialog1.dismiss();
}
}
}
08-04 14:34:55.705: E/AndroidRuntime(25473): FATAL EXCEPTION: AsyncTask #1
08-04 14:34:55.705: E/AndroidRuntime(25473): Process: test.dsi.decathnavigation, PID: 25473
08-04 14:34:55.705: E/AndroidRuntime(25473): java.lang.RuntimeException: An error occured while executing doInBackground()
08-04 14:34:55.705: E/AndroidRuntime(25473): at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-04 14:34:55.705: E/AndroidRuntime(25473): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-04 14:34:55.705: E/AndroidRuntime(25473): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-04 14:34:55.705: E/AndroidRuntime(25473): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-04 14:34:55.705: E/AndroidRuntime(25473): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-04 14:34:55.705: E/AndroidRuntime(25473): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-04 14:34:55.705: E/AndroidRuntime(25473): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-04 14:34:55.705: E/AndroidRuntime(25473): at java.lang.Thread.run(Thread.java:841)
08-04 14:34:55.705: E/AndroidRuntime(25473): Caused by: java.lang.IllegalArgumentException: the name must not be empty:
08-04 14:34:55.705: E/AndroidRuntime(25473): at android.os.Parcel.readException(Parcel.java:1469)
08-04 14:34:55.705: E/AndroidRuntime(25473): at android.os.Parcel.readException(Parcel.java:1419)
08-04 14:34:55.705: E/AndroidRuntime(25473): at com.google.android.gms.internal.s$a$a.a(Unknown Source)
08-04 14:34:55.705: E/AndroidRuntime(25473): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
08-04 14:34:55.705: E/AndroidRuntime(25473): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
08-04 14:34:55.705: E/AndroidRuntime(25473): at test.dsi.decathnavigation.Web_view_demo$GetGoogleToken.doInBackground(Web_view_demo.java:117)
08-04 14:34:55.705: E/AndroidRuntime(25473): at test.dsi.decathnavigation.Web_view_demo$GetGoogleToken.doInBackground(Web_view_demo.java:1)
08-04 14:34:55.705: E/AndroidRuntime(25473): at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-04 14:34:55.705: E/AndroidRuntime(25473): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-04 14:34:55.705: E/AndroidRuntime(25473): ... 4 more
08-04 14:34:56.274: E/WindowManager(25473): android.view.WindowLeaked: Activity test.dsi.decathnavigation.Web_view_demo has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{420eb868 V.E..... R......D 0,0-513,144} that was originally added here
08-04 14:34:56.274: E/WindowManager(25473): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:359)
08-04 14:34:56.274: E/WindowManager(25473): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
08-04 14:34:56.274: E/WindowManager(25473): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
08-04 14:34:56.274: E/WindowManager(25473): at android.app.Dialog.show(Dialog.java:286)
08-04 14:34:56.274: E/WindowManager(25473): at test.dsi.decathnavigation.Web_view_demo$GetGoogleToken.onPreExecute(Web_view_demo.java:109)
08-04 14:34:56.274: E/WindowManager(25473): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
08-04 14:34:56.274: E/WindowManager(25473): at android.os.AsyncTask.execute(AsyncTask.java:535)
08-04 14:34:56.274: E/WindowManager(25473): at test.dsi.decathnavigation.Web_view_demo.onCreate(Web_view_demo.java:58)
08-04 14:34:56.274: E/WindowManager(25473): at android.app.Activity.performCreate(Activity.java:5248)
08-04 14:34:56.274: E/WindowManager(25473): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
08-04 14:34:56.274: E/WindowManager(25473): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
08-04 14:34:56.274: E/WindowManager(25473): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
08-04 14:34:56.274: E/WindowManager(25473): at android.app.ActivityThread.access$800(ActivityThread.java:139)
08-04 14:34:56.274: E/WindowManager(25473): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
08-04 14:34:56.274: E/WindowManager(25473): at android.os.Handler.dispatchMessage(Handler.java:102)
08-04 14:34:56.274: E/WindowManager(25473): at android.os.Looper.loop(Looper.java:136)
08-04 14:34:56.274: E/WindowManager(25473): at android.app.ActivityThread.main(ActivityThread.java:5086)
08-04 14:34:56.274: E/WindowManager(25473): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 14:34:56.274: E/WindowManager(25473): at java.lang.reflect.Method.invoke(Method.java:515)
08-04 14:34:56.274: E/WindowManager(25473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-04 14:34:56.274: E/WindowManager(25473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-04 14:34:56.274: E/WindowManager(25473): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
改变:
protected void onPostExecute(Void unused)
到
protected void onPostExecute(String unused)
答案 1 :(得分:0)
问题:
引起:java.lang.IllegalArgumentException:名称不得为 空
在AsyncTask
内使用accountName
变量:
token1 = GoogleAuthUtil.getToken(Web_view_demo.this, accountName, mScope);
但变量是Web_view_demo
类的实例成员。它被设置为一个空字符串,以后没有其他任何东西:
String accountName="";
在onCreate
方法中,当你这样做时:
final String accountName = intent.getExtras().getString("accountName");
您没有将值设置为名为accountName
的实例成员变量,但是您创建了一个新变量,仅在onCreate
方法中可见。如果要设置实例成员变量,请将上一行更改为:
accountName = intent.getExtras().getString("accountName");