所以我正在试用Facebook与Android工作室的整合,而且它并不酷。我发现了很多错误,我已经清除了这些错误并且在Facebook开发者网站https://developers.facebook.com/docs/android/login-with-facebook/
所以这是我的MainActivity.java
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
private MainFragment mainFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null) {
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
}
else
{
mainFragment=(MainFragment)getSupportFragmentManager().findFragmentById(android.R.id.content);
}
}}
和另一个名为MainFragment.java的类
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends Fragment
{
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
return view;
}
}
这就像在facebook网站上提到的xml代码
<LinearLayout 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:orientation="vertical">
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
/>
</LinearLayout>
它编译,当我运行它安装在Android虚拟设备并强制关闭自己... 这是logcat
02-09 10:21:30.561 1796-1796/com.pezfroll.faceb D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so
02-09 10:21:30.585 1796-1796/com.pezfroll.faceb D/﹕ HostConnection::get() New Host Connection established 0xb9832990, tid 1796
02-09 10:21:30.673 1796-1796/com.pezfroll.faceb D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-09 10:21:30.677 1796-1796/com.pezfroll.faceb D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so
02-09 10:21:31.017 1796-1796/com.pezfroll.faceb W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
02-09 10:21:31.021 1796-1796/com.pezfroll.faceb E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
02-09 10:21:31.085 1796-1796/com.pezfroll.faceb E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-09 10:21:31.089 1796-1796/com.pezfroll.faceb D/OpenGLRenderer﹕ Enabling debug mode 0
02-09 10:21:31.213 1796-1810/com.pezfroll.faceb W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0xa4bc3648)
02-09 10:21:32.129 1796-1810/com.pezfroll.faceb E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:911)
at com.facebook.internal.Utility.queryAppSettings(Utility.java:673)
at com.facebook.widget.LoginButton$1.doInBackground(LoginButton.java:678)
at com.facebook.widget.LoginButton$1.doInBackground(LoginButton.java:675)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
02-09 10:21:50.613 1796-1810/com.pezfroll.faceb I/Process﹕ Sending signal. PID: 1796 SIG: 9
我甚至没有添加行为,它仍然强制关闭
答案 0 :(得分:0)
明李说,应遵循整个指南。
我对说明书的唯一问题是它引用了一个应用程序选项卡
这里: 5.在“应用程序”选项卡中,添加名为com.facebook.sdk.ApplicationId的新元数据和值@ string / facebook_app_id。
应该说: 5.在AndroidManifest中的应用程序节点中,添加名为com.facebook.sdk.ApplicationId的新元数据和值@ string / facebook_app_id。
代码还有另一个可能导致此问题的基本方面。 如果您使用的是Android工作室,您可能会在 MainFragment 中看到该处的注释如下:
/ ** *一个简单的{@link Fragment}子类。 *包含此片段的活动必须实现 * {@link MainFragment.OnFragmentInteractionListener}界面 *处理交互事件。 *使用{@link MainFragment#newInstance}工厂方法 *创建此片段的实例。 * /
这是正确的, OnFragmentInteractionListener 必须在 MainActivity 中实施,如下所示:
公共类MainActivity扩展FragmentActivity实现MainFragment.OnFragmentInteractionListener {...此处的类代码...}
MainActivity 类必须覆盖 onFragmentInteraction 方法,如下所示:
@覆盖 public void onFragmentInteraction(Uri uri){ mainFragment.onButtonPressed(URI); }