我正试图在Android编程中弄清楚开发一个与Facebook集成的真正基本的应用程序。我按照教程制作了一个基本的登录页面,但是应用程序在开始时就崩溃了。我试着自己调试,但几个小时后我没有取得任何成功。真正让我失望的是,错误消息并没有直接告诉我我的代码中的确切位置,我搞砸了,我想我现在缺乏了解他们现在所说的内容。
(https://developers.facebook.com/docs/android/login-with-facebook/v2.2#step1)
以下是错误(我为文本墙道歉)和我的MainActivity / MainFragment类。如果有人能够指出我犯了什么具体的错误(或者我只是接近整个错误),那将是非常棒的。此外,任何有关Android应用程序的一般调试和此类问题的提示也将非常受欢迎。我的思绪仍然没有完全围绕活动生命周期及其方法。
logcat的:
01-13 05:31:14.216 17714-17730/com.example.harrisonhe.chatstats W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0x41e44e30)
01-13 05:31:14.216 17714-17731/com.example.harrisonhe.chatstats W/dalvikvm﹕ threadid=13: thread exiting with uncaught exception (group=0x41e44e30)
01-13 05:31:14.216 17714-17730/com.example.harrisonhe.chatstats E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.harrisonhe.chatstats, PID: 17714
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:864)
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:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:864)
01-13 05:31:14.226 17714-17731/com.example.harrisonhe.chatstats E/AndroidRuntime_2_crash﹕ crash in the same process: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:864)
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:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:864)
代码:
public class MainActivity extends FragmentActivity {
private MainFragment mainFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null)
{
//add fragment on initial activity startup
mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
}
else
{
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
}
setContentView(R.layout.activity_main);
}
@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_main, 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);
}
}
public class MainFragment extends Fragment{
private static final String TAG = "MainFragment";
// @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);
return view;
}
private Session.StatusCallback callback = new Session.StatusCallback() {
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG,"swag420");
Log.i(TAG, "Logged in...");
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
private UiLifecycleHelper uiHelper;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
}
@Override
public void onResume(){
super.onResume();
/*
for scenarios where the main activity is launched and user session is
not null, the session state change notification may not be triggered.
Trigger it if it's open/closed
*/
Session session = Session.getActiveSession();
if(session!=null && (session.isOpened() || session.isClosed()))
{
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(resultCode, resultCode, data);
uiHelper.onActivityResult(requestCode,resultCode,data);
}
@Override
public void onPause(){
super.onPause();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
答案 0 :(得分:0)
至少你知道问题是什么,也许不知道它在哪里。它是一个空指针。查看代码时,我无法确定问题this answer作为参考。在这个答案中,该人没有初始化允许他们连接到互联网的jsonParser
。我假设还有其他课程没有显示
这也是我联系到的答案:
此外,您无法在UIThread上发出网络请求。这将导致NetworkOnMainThreadException。您将需要使用AsyncTask。检查此NetworkOnMainThreadException
答案 1 :(得分:0)
LoginButton需要在元数据中设置您的应用ID(AndroidManifest.xml和strings.xml)。您是否相应地设置了应用ID? https://developers.facebook.com/docs/android/getting-started