I'm trying to get my feet wet in Android programming developing a really basic application that integrates with Facebook. I followed the tutorial for making a basic login page, but the application just crashes at the start. I tried to debug it myself, but after a few hours I haven't run into any success. What's also really killing me is that the error messages don't directly tell me where exactly in my code I'm screwing up, and I guess I lack the understanding to know what they're saying right now.
(https://developers.facebook.com/docs/android/login-with-facebook/v2.2#step1)
Here are the errors(and I apologize for the wall of text) and my MainActivity/MainFragment classes. If anybody could point out what specific mistake (or if I'm just approaching the entire thing wrong) I made, that would be really awesome. Also, any tips for just general debugging of Android apps and problems like this would be really appreciated too. My mind still hasn't completely wrapped around the activity lifecycle and their methods.
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)
Code:
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);
}
}