当我尝试在登录后获取用户个人资料,然后由{
}导致NullPointerException
09-26 06:57:50.960 19641-19641/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.sanni.retrievegoogleprofile, PID: 19641
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.sanni.retrievegoogleprofile/com.example.sanni.retrievegoogleprofile.GoogleLogin}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at com.example.sanni.retrievegoogleprofile.MainActivity.loginAndGetUserProfile(MainActivity.java:21)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这是我的代码段:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button login = (Button) findViewById(R.id.loginButton);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, GoogleLogin.class);
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
final ProgressDialog progressBar = ProgressDialog.show(this,
"Please wait", "Loading...");
WebView webview = new WebView(this);
webview.setVisibility(View.VISIBLE);
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);
String googleAuthorizationRequestUrl = new GoogleAuthorizationRequestUrl(
CLIENT_ID,
REDIRECT_URI,
SCOPE).build();
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
progressBar.dismiss();
if (url.startsWith(REDIRECT_URI)) {
try {
if (url.indexOf("code=") != -1) {
// Url is like http://localhost/?code=4/Z5DgC1IxNL-muPsrE2Sjy9zQn2pF
String code = url.substring(
REDIRECT_URI.length() + 7,
url.length());
AccessTokenResponse accessTokenResponse = new GoogleAuthorizationCodeGrant(
new NetHttpTransport(),
new JacksonFactory(),
CLIENT_ID,
CLIENT_SECRET,
code, REDIRECT_URI)
.execute();
write(accessTokenResponse);
view.setVisibility(View.INVISIBLE);
// MyStocks.mLoginLayout.setVisibility(View.GONE);
// MyStocks.mMyyStocksLayout.setVisibility(View.VISIBLE);
retrieveProfile();
Log.d("SANNI", "retrieveProfile excution finished");
finish();
} else if (url.indexOf("error=") != -1) {
view.setVisibility(View.INVISIBLE);
// MyStocks.mLoginLayout.setVisibility(View.GONE);
// MyStocks.mMyyStocksLayout.setVisibility(View.VISIBLE);
clearCredentials();
finish();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
webview.loadUrl(googleAuthorizationRequestUrl);
}
public AccessTokenResponse read() {
AccessTokenResponse accessTokenResponse = new AccessTokenResponse();
accessTokenResponse.accessToken = mPrefs.getString(ACCESS_TOKEN, "");
accessTokenResponse.expiresIn = mPrefs.getLong(EXPIRATION_TIME, 0);
accessTokenResponse.refreshToken = mPrefs.getString(REFRESH_TOKEN, "");
accessTokenResponse.scope = mPrefs.getString(SCOPE_STRING, "");
return accessTokenResponse;
}
public void clearCredentials() {
SharedPreferences.Editor editor = mPrefs.edit();
editor.remove(PREF_KEY_GOOGLE_LOGIN);
editor.remove(EXPIRATION_TIME);
editor.remove(REFRESH_TOKEN);
editor.remove(SCOPE_STRING);
editor.remove(PREF_KEY_GOOGLE_LOGIN);
editor.commit();
}
private void write(AccessTokenResponse accessTokenResponse) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(ACCESS_TOKEN, accessTokenResponse.accessToken);
editor.putLong(EXPIRATION_TIME, accessTokenResponse.expiresIn);
editor.putString(REFRESH_TOKEN, accessTokenResponse.refreshToken);
editor.putString(SCOPE_STRING, accessTokenResponse.scope);
editor.putBoolean(PREF_KEY_GOOGLE_LOGIN, true);
editor.commit();
}
private void retrieveProfile() {
Log.d("SANNI", "retrieveProfile called");
try {
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport transport = new NetHttpTransport();
AccessTokenResponse accessTokenResponse = read();
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
accessTokenResponse.accessToken, transport, jsonFactory,
CLIENT_ID,
CLIENT_SECRET,
accessTokenResponse.refreshToken);
Builder b = Plus.builder(transport, jsonFactory).setApplicationName("Stockal");
b.setHttpRequestInitializer(accessProtectedResource);
Plus plus = b.build();
mProfile = plus.people().get("me").execute();
Toast.makeText(this, "Welcome Mr." + mProfile.getName(), Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Log.d("SANNI", "exception occured in retrieveProfile :" + ex.getMessage().toString());
ex.printStackTrace();
}
}
请帮我解决这个问题的原因。
当我调试我的应用时,它崩溃了......
AccessTokenResponse accessTokenResponse = new GoogleAuthorizationCodeGrant(
new NetHttpTransport(),
new JacksonFactory(),
CLIENT_ID,
CLIENT_SECRET,
code, REDIRECT_URI)
.execute();