我正在创建一个Android应用程序,我想通过Facebook登录。我按照 facebook开发者页面中的建议完成了每一步,但我收到了以下错误:
com.facebook.widget.LoginButton未实例化
但我在我的活动中实例化了。
我的xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:theme="@style/AppTheme"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar">
</include>
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
<LinearLayout
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="65dp"
android:orientation="vertical"
android:paddingBottom="15dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_marginTop="30dp"
android:hint="Enter Username"
android:id="@+id/et1"
android:textSize="20sp"
android:layout_marginBottom="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:hint="Enter Password"
android:id="@+id/et2"
android:inputType="textPassword"
android:layout_marginBottom="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bu1"
android:text="Login"
android:layout_marginBottom="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:textSize="20sp"
android:id="@+id/tv1"
android:layout_gravity="center_horizontal"
android:text="Register"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="20sp"
android:id="@+id/tv2"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
< /LinearLayout>`
我的活动代码
`
package com.manali.vivek.userregistration;
public class userLogin extends Activity {
private TextView info;
private LoginButton loginButton;
EditText et1,et2;
Button bu1;
private TextView tv1,tv2;
private CallbackManager callbackManager;
String nameT;
String passwordT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.user_login);
et1 = (EditText) findViewById(R.id.et1);
et2 = (EditText) findViewById(R.id.et2);
info = (TextView)findViewById(R.id.info);
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new
FacebookCallback<LoginResult>() {
public void onSuccess(LoginResult loginResult) {
info.setText(
"User ID: "
+ loginResult.getAccessToken().getUserId()
+ "\n" +
"Auth Token: "
+ loginResult.getAccessToken().getToken()
);
}
public void onCancel() {
info.setText("Login attempt canceled.");
}
public void onError(FacebookException e) {
info.setText("Login attempt failed.");
}
});
bu1 = (Button) findViewById(R.id.bu1);
tv1=(TextView)findViewById(R.id.tv1);
tv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(userLogin.this,MainActivity.class);
startActivity(i);
}
});
tv2=(TextView)findViewById(R.id.tv2);
nameT= et1.getText().toString().trim();
passwordT= et2.getText().toString().trim();
bu1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getCustomerData();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private void getCustomerData(){
class GetData extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading =
ProgressDialog.show(userLogin.this,"Fetching...","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
showData(s);
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s =
return s;
}
}
GetData ge = new GetData();
ge.execute();
}
private void showData(String json){
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONArray(Fetch.TAG_JSON_ARRAY);
JSONObject c = result.getJSONObject(0);
String id=c.getString(Fetch.TAG_ID);
String name = c.getString(Fetch.TAG_NAME);
String password = c.getString(Fetch.TAG_PASS);
String mailId = c.getString(Fetch.TAG_MAIl);
String x=et1.getText().toString();
String y=et2.getText().toString();
if(name.equals(x) &&password.equals(y)){
startActivity(new Intent(userLogin.this,secondStart.class));
// Intent i=new Intent(this,userInfo.class);
//i.putExtra("name",name);
// i.putExtra("password",password);
// i.putExtra("mailId",mailId);
//startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
`
我的清单代码
`
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
<activity
android:name=".userLogin"
android:label=" Login "
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label=" register " >
</activity>`