Reputation: 5121
So i followed this tutorial to set up the android facebook sdk in my app i already have built. I now have all the files the tutorial says to build and everything shows no errors. Now i want to be able to set my app so that when the user presses a button on the app it brings them to the Facebook sign on page. I tried using this code but it doesnt work, the app crashes and i get these errors in logcat.
Can anyone help me get this to work?
ImageButton facebook= (ImageButton) findViewById(R.id.facebook);
facebook.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), FBConnectionActivity.class);
startActivityForResult(myIntent, 0);
}
});
And these are the errors logcat gives me:
07-10 22:10:57.510: ERROR/AndroidRuntime(819): FATAL EXCEPTION: main
07-10 22:10:57.510: ERROR/AndroidRuntime(819): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.outfit.first/com.outfit.first.FBConnectionActivity}: java.lang.InstantiationException: com.outfit.first.FBConnectionActivity
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1680)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at android.os.Looper.loop(Looper.java:123)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at android.app.ActivityThread.main(ActivityThread.java:3839)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at java.lang.reflect.Method.invoke(Method.java:507)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at dalvik.system.NativeStart.main(Native Method)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): Caused by: java.lang.InstantiationException: com.outfit.first.FBConnectionActivity
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at java.lang.Class.newInstanceImpl(Native Method)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at java.lang.Class.newInstance(Class.java:1409)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-10 22:10:57.510: ERROR/AndroidRuntime(819): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
Upvotes: 1
Views: 310
Reputation: 5542
Dont try and start an activity using the FBConnectionActivity, instead in the acivity you want them to login on extend the FBConnectionActivity eg:
public class myActivity extends FBconnectionActivity{.......
then in your onClickListener within myActivity(or whatever you've called it!) just call the setConnection() and getID() methods, this will then prompt the user to login via facebook. eg:
ImageButton facebook= (ImageButton) findViewById(R.id.facebook);
facebook.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setConnection();
getID();
}
});
Note that this used a getID() method without the textView and progress bar that it in the tutorial, you can just add the following to you FBConnectionActivity class:
public void getID() {
if (isSession()) {
Log.d(TAG, "sessionValid");
mAsyncRunner.request("me", new IDRequestListener());
} else {
// no logged in, so relogin
Log.d(TAG, "sessionNOTValid, relogin");
mFacebook.authorize(this, PERMS, new LoginDialogListener());
}
}
Upvotes: 2