我在https://dev.twitter.com/twitter-kit/android/twitter-login上的示例中显示了TwitterLoginButton,以允许使用twitter登录我的应用程序。我正在使用Android Studio并安装了Fabric插件来开始并使用它注册我的应用程序。我完全按照提到的方式实现了代码,但是当我点击Twitter按钮时,没有任何反应。
我的活动布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash"
tools:context="com.rayat.pricewiz.view.activity.SplashScreen">
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!--<RelativeLayout android:id="@+id/fullscreen_content_controls"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:ignore="UselessParent" android:visibility="invisible">
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Get STARTED"
android:id="@+id/btnGetStarted"
android:gravity="center"
android:layout_marginTop="200dp"
android:layout_centerHorizontal="true"
android:textColor="@color/black"
android:background="@drawable/transparent_rounded_box"
/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="300dp"
android:layout_height="242dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/btnGetStarted"
android:layout_alignStart="@+id/btnGetStarted"
android:id="@+id/loginForm"
android:background="@drawable/login_form">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layoutEmail"
android:weightSum="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_weight="0.14"
android:src="@drawable/login_form_email_icon"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:layout_marginTop="10dp"
android:id="@+id/txtEmailAddress"
android:layout_weight="0.72"
android:textColor="@color/black"/>
<TextView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_weight="0.14"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layoutEmail"
android:id="@+id/layoutPassword"
android:layout_alignParentStart="true"
android:weightSum="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgPassIcon"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_weight="0.14"
android:src="@drawable/login_form_passwd_icon"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_marginTop="10dp"
android:id="@+id/txtPassword"
android:layout_weight="0.72"
android:textColor="@color/black"/>
<TextView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_weight="0.14"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layoutPassword"
android:layout_alignParentStart="true"
android:id="@+id/layoutLang"
android:weightSum="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgLangIcon"
android:layout_marginLeft="3dp"
android:layout_marginTop="20dp"
android:layout_weight="0.14"
android:src="@drawable/login_form_lang_icon"/>
<Spinner
android:layout_height="36dp"
android:layout_width="0dp"
android:layout_weight="0.80"
android:layout_marginTop="14dp"
android:spinnerMode="dialog"
android:entries="@array/language"
android:prompt="@string/select_language"
android:id="@+id/selLanguage"
style="@style/SpinnerAsEditText"
android:background="@null"/>
</LinearLayout>
<FrameLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/layoutLang"
android:layout_marginTop="5dp">
<Button
android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="36dp"
android:scaleType="fitXY"
android:background="@drawable/login_selector"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textColor="@drawable/text_color"
android:contentDescription="@string/login"
android:onClick="login"
android:text="@string/login">
</Button>
</FrameLayout>
</RelativeLayout>
</RelativeLayout>-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/splashScreenMargin"
android:layout_marginEnd="@dimen/splashScreenMargin"
android:layout_centerInParent="true"
android:id="@+id/socialLoginForm"
android:background="@drawable/login_form">
<com.facebook.widget.LoginButton
xmlns:fb="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:paddingStart="10dp"
android:id="@+id/btnLoginFacebook"
android:drawableStart="@drawable/facebook_f"
/>
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/btnTwitterLogin"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/btnLoginFacebook"/>
<com.google.android.gms.common.SignInButton
android:id="@+id/btnLogingplus"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/btnTwitterLogin"
android:text="Log in with Google+"
/>
<Button android:text="Log in with email"
android:id="@+id/button5"
android:background="@drawable/orange_button"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@+id/btnLogingplus"
android:drawableStart="@drawable/envelope"/>
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
以下是我实施登录的方式:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
//Twitter login
twitterButton = (TwitterLoginButton)
findViewById(R.id.btnTwitterLogin);
twitterButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
// Do something with result, which provides a
// TwitterSession for making API calls
Log.w("twitter logged","twitter logged");
TwitterSession session =
Twitter.getSessionManager().getActiveSession();
TwitterAuthToken authToken = session.getAuthToken();
String token = authToken.token;
String secret = authToken.secret;
}
@Override
public void failure(TwitterException exception) {
// Do something on failure
}
}); }
当我点击按钮时,没有任何反应,也没有出现Twitter授权页面。我安装了Twitter应用程序。
请帮忙。
谢谢, Noorul
答案 0 :(得分:0)
你的Oncreate()首先定义 setContentView()然后设置 Fabric.With()但是在twitter集成中设置这些流程是错误的:
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
setContentView(R.layout.activity_main);
确定你成功了:) 仍然把我的代码放在这里供您参考...... !!!
activity_twitter.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
TwitterIntegrationDemo.java:
package com.twitter.dhruv.twitterexample;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.twitter.sdk.android.Twitter;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterAuthToken;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterLoginButton;
import io.fabric.sdk.android.Fabric;
public class TwitterIntegrationDemo extends ActionBarActivity {
// Note: Your consumer key and secret should be obfuscated in your source code before shipping.
private static final String TWITTER_KEY = "Your Key";
private static final String TWITTER_SECRET = "Your Secret";
private TwitterLoginButton twitterLoginButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
setContentView(R.layout.activity_main);
twitterLoginButton = (TwitterLoginButton) findViewById(R.id.login_button);
twitterLoginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> twitterSessionResult) {
Toast.makeText(MainActivity.this, "Authentication Success", Toast.LENGTH_SHORT);
TwitterSession session = Twitter.getSessionManager().getActiveSession();
TwitterAuthToken authToken = session.getAuthToken();
String token = authToken.token;
String secret = authToken.secret;
String userName = session.getUserName();
}
@Override
public void failure(TwitterException e) {
Toast.makeText(MainActivity.this, "Authentication Fail", Toast.LENGTH_SHORT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Pass the activity result to the login button.
twitterLoginButton.onActivityResult(requestCode, resultCode, data);
}
}
build.gradle:
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.twitter.dhruv.twitterexample"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile('com.twitter.sdk.android:twitter:1.3.1@aar') {
transitive = true;
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter.dhruv.twitterexample" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TwitterIntegrationDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="io.fabric.ApiKey"
android:value="Auto-Generated Value" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
必须记住:
TwitterLoginButton 在对话视图中不支持:)