我无法尝试解决Facebook登录按钮作为Android应用程序的一部分.
应用程序启动良好,但是当我按下Facebook登录按钮时它崩溃了.
这就是它在日志中所说的:
12-06 17:17:01.079 25678-25678/com.example.icen.tij01 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.icen.tij01, PID: 25678 java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.Fragment com.facebook.internal.FragmentWrapper.getSupportFragment()' on a null object reference at com.facebook.FacebookButtonBase.getFragment(FacebookButtonBase.java:105) at com.facebook.login.widget.LoginButton$LoginClickListener.onClick(LoginButton.java:736) at com.facebook.FacebookButtonBase$1.onClick(FacebookButtonBase.java:383) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 12-06 17:17:03.055 25678-25678/com.example.icen.tij01 I/Process: Sending signal. PID: 25678 SIG: 9
更新:
这是必须处理Facebook登录的Activity的一部分:
package com.example.icen.tij01; import android.app.Activity; /* import... */ public class StartActivity extends ActionBarActivity { static String hostDomain = "http://192.168.48.1/myPhpApp/"; private TextView info; private LoginButton loginButton; private CallbackManager callbackManager; static String checkUrl = hostDomain + "connect.php"; String responseServer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); setContentView(R.layout.activity_start); info = (TextView)findViewById(R.id.info); loginButton = (LoginButton)findViewById(R.id.login_button); // redirekicija na formu za logovanje Button btnLogin = (Button) findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent loginForm = new Intent(StartActivity.this, LoginFormActivity.class); startActivity(loginForm); } }); loginButton.registerCallback(callbackManager, new FacebookCallback() { @Override public void onSuccess(LoginResult loginResult) { info.setText( "User ID: " + loginResult.getAccessToken().getUserId() + "\n" + "Auth Token: " + loginResult.getAccessToken().getToken() ); } @Override public void onCancel() { info.setText("Login attempt canceled."); } @Override public void onError(FacebookException e) { } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { callbackManager.onActivityResult(requestCode, resultCode, data); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_start, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /* Inner class to get response */ class AsyncT extends AsyncTask { @Override protected Void doInBackground(Void... voids) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(checkUrl); try { JSONObject jsonobj = new JSONObject(); jsonobj.put("name", "Aneh"); jsonobj.put("age", "22"); List nameValuePairs = new ArrayList (); nameValuePairs.add(new BasicNameValuePair("req", jsonobj.toString())); Log.e("mainToPost", "mainToPost" + nameValuePairs.toString()); // Use UrlEncodedFormEntity to send in proper format which we need httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); InputStream inputStream = response.getEntity().getContent(); InputStreamToStringExample str = new InputStreamToStringExample(); responseServer = str.getStringFromInputStream(inputStream); //Log.e("response", "response -----" + responseServer); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); Context context = getApplicationContext(); int duration = Toast.LENGTH_SHORT; Toast toast; if(responseServer.trim().equals("1")) { toast = Toast.makeText(context, "Connection ok, redirecting to adverts...", duration); toast.show(); Intent show = new Intent(StartActivity.this, MainActivity.class); startActivity(show); } else { toast = Toast.makeText(context, "Connection error", duration); toast.show(); AlertDialog alertDialog = new AlertDialog.Builder(StartActivity.this).create(); alertDialog.setTitle("Connection error"); alertDialog.setMessage("Error. Please check your connection"); alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.show(); } } } public static class InputStreamToStringExample { public static void main(String[] args) throws IOException { // intilize an InputStream InputStream is = new ByteArrayInputStream("file content..blah blah".getBytes()); String result = getStringFromInputStream(is); System.out.println(result); System.out.println("Done"); } // convert InputStream to String private static String getStringFromInputStream(InputStream is) { BufferedReader br = null; StringBuilder sb = new StringBuilder(); String line; try { br = new BufferedReader(new InputStreamReader(is)); while ((line = br.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); } } }
我跟着几个教程,比如:
http://code.tutsplus.com/tutorials/quick-tip-add-facebook-login-to-your-android-app--cms-23837
请帮我解决一下这个问题.
问题出在facebook登录按钮上.由于某种原因,它没有初始化.我尝试了很多不同的方法来解决这个问题.并最终得到两个解决方案.
1)根据https://developers.facebook.com/docs/facebook-login/android更改您的类以将fragment.And和setFragment扩展到您的fb_login_button
{ loginButton = (LoginButton) view.findViewById(R.id.login_button); loginButton.setReadPermissions("user_friends"); loginButton.setFragment(this); }
2)而不是facebook_login_button创建自定义按钮.并在onClickListener上调用一个具有callBackManager和LoginManager的方法.
{
login_activity中的自定义按钮
Login_Activity
fb_btn = (Button) findViewById(R.id.fb_btn); fb_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onfbClick(); } });
调用LoginManager的方法
private void onfbClick() { callbackManager = CallbackManager.Factory.create(); LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email","public_profile")); LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback() { @Override public void onSuccess(LoginResult loginResult) { // App code accessToken = loginResult.getAccessToken(); Profile profile = Profile.getCurrentProfile(); Bundle parameters = new Bundle(); parameters.putString("fields", "first_name,last_name"); //request.setParameters(parameters); //request.executeAsync(); } @Override public void onCancel() { Log.i(TAG, "onCancel triggered"); } @Override public void onError(FacebookException exception) { Log.i(TAG, "onError triggered"); } }); } }