我在android studio中创建了一个带有facebook sdk集成的应用程序,当我在我的设备上运行应用程序并单击播放按钮时,出现“应用程序已停止”错误,并显示调试中显示的错误列表android studio!
我已经使用homeactivity class
编辑了新错误错误:
10-08 18:53:47.061 29083-29083/com.facebook.android.friendsmash E/Trace: error opening trace file: No such file or directory (2)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: FATAL EXCEPTION: main
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.facebook.android.friendsmash/com.facebook.android.friendsmash.HomeActivity}: java.lang.NullPointerException
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:138)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.os.Looper.loop(Looper.java:213)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4787)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: Caused by: java.lang.NullPointerException
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at com.facebook.android.friendsmash.HomeActivity.onCreate(HomeActivity.java:89)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5008)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:138)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.os.Looper.loop(Looper.java:213)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4787)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
10-08 18:53:47.241 29083-29083/com.facebook.android.friendsmash E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
HomeActivity.java
package com.facebook.android.friendsmash;
import ....
public class HomeActivity extends FragmentActivity {
// Tag used when logging messages
private static final String TAG = HomeActivity.class.getSimpleName();
// Uri used in handleError() below
private static final Uri M_FACEBOOK_URL = Uri.parse("http://m.facebook.com");
private UiLifecycleHelper fbUiLifecycleHelper;
public UiLifecycleHelper getFbUiLifecycleHelper() {
return fbUiLifecycleHelper;
}
// Fragment attributes
private static final int FB_LOGGED_OUT_HOME = 0;
private static final int HOME = 1;
private static final int FRAGMENT_COUNT = HOME +1;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
// Boolean recording whether the activity has been resumed so that
// the logic in onSessionStateChange is only executed if this is the case
private boolean isResumed = false;
// Constructor
public HomeActivity() {
super();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String currentFBUserJSONString =
savedInstanceState.getString(FriendSmashApplication.getCurrentFbUserKey());
if (currentFBUserJSONString != null) {
GraphUser currentFBUser =
null;
try {
currentFBUser = GraphObject.Factory.create(new JSONObject(currentFBUserJSONString), GraphUser.class);
} catch (JSONException e) {
e.printStackTrace();
}
((FriendSmashApplication)getApplication()).setCurrentFBUser(currentFBUser);
}
fbUiLifecycleHelper = new UiLifecycleHelper(this, new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
// Add code here to accommodate session changes
updateView();
}
});
fbUiLifecycleHelper.onCreate(savedInstanceState);
setContentView(R.layout.home);
FragmentManager fm = getSupportFragmentManager();
fragments[FB_LOGGED_OUT_HOME] = fm.findFragmentById(R.id.fbLoggedOutHomeFragment);
fragments[HOME] = fm.findFragmentById(R.id.homeFragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
// Restore the logged-in user's information if it has been saved and the existing data in the application
// has been destroyed (i.e. the app hasn't been used for a while and memory on the device is low)
// - only do this if the session is open for the social version only
if (FriendSmashApplication.IS_SOCIAL) {
// loggedIn
if (savedInstanceState != null) {
boolean loggedInState = savedInstanceState.getBoolean(FriendSmashApplication.getLoggedInKey(), false);
((FriendSmashApplication)getApplication()).setLoggedIn(loggedInState);
if ( ((FriendSmashApplication)getApplication()).isLoggedIn() &&
( ((FriendSmashApplication)getApplication()).getFriends() == null ||
((FriendSmashApplication)getApplication()).getCurrentFBUser() == null) ) {
try {
// currentFBUser
currentFBUserJSONString = savedInstanceState.getString(FriendSmashApplication.getCurrentFbUserKey());
if (currentFBUserJSONString != null) {
GraphUser currentFBUser = GraphObject.Factory.create(new JSONObject(currentFBUserJSONString), GraphUser.class);
((FriendSmashApplication)getApplication()).setCurrentFBUser(currentFBUser);
}
// friends
ArrayList<String> friendsJSONStringArrayList = savedInstanceState.getStringArrayList(FriendSmashApplication.getFriendsKey());
if (friendsJSONStringArrayList != null) {
ArrayList<GraphUser> friends = new ArrayList<GraphUser>();
Iterator<String> friendsJSONStringArrayListIterator = friendsJSONStringArrayList.iterator();
while (friendsJSONStringArrayListIterator.hasNext()) {
friends.add(GraphObject.Factory.create(new JSONObject(friendsJSONStringArrayListIterator.next()), GraphUser.class));
}
((FriendSmashApplication)getApplication()).setFriends(friends);
}
} catch (JSONException e) {
Log.e(FriendSmashApplication.TAG, e.toString());
}
}
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
fbUiLifecycleHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
if (!FriendSmashApplication.IS_SOCIAL) {
showFragment(HOME, false);
} else {
Session session = Session.getActiveSession();
if (session != null && session.isOpened() && ((FriendSmashApplication)getApplication()).getCurrentFBUser() != null) {
showFragment(HOME, false);
} else {
showFragment(FB_LOGGED_OUT_HOME, false);
}
}
}
@Override
public void onResume() {
super.onResume();
fbUiLifecycleHelper.onResume();
isResumed = true;
// Hide the notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Measure mobile app install ads
// Ref: https://developers.facebook.com/docs/tutorials/mobile-app-ads/
AppEventsLogger.activateApp(this, ((FriendSmashApplication)getApplication()).getString(R.string.app_id));
}
@Override
public void onPause() {
super.onPause();
fbUiLifecycleHelper.onPause();
isResumed = false;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
fbUiLifecycleHelper.onSaveInstanceState(outState);
// Save the logged-in state
outState.putBoolean(FriendSmashApplication.getLoggedInKey(), ((FriendSmashApplication)getApplication()).isLoggedIn());
// Save the currentFBUser
if (((FriendSmashApplication)getApplication()).getCurrentFBUser() != null) {
outState.putString(FriendSmashApplication.getCurrentFbUserKey(),
((FriendSmashApplication)getApplication()).getCurrentFBUser().getInnerJSONObject().toString());
}
// Save the logged-in user's list of friends
if (((FriendSmashApplication)getApplication()).getFriends() != null) {
outState.putStringArrayList(FriendSmashApplication.getFriendsKey(),
((FriendSmashApplication)getApplication()).getFriendsAsArrayListOfStrings());
}
}
@Override
public void onDestroy() {
super.onDestroy();
fbUiLifecycleHelper.onDestroy();
}
public void buyBombs() {
// update bomb and coins count (5 coins per bomb)
FriendSmashApplication app = (FriendSmashApplication) getApplication();
// check to see that we have enough coins.
if (app.getCoins() - 5 < 0) {
Toast.makeText(this, "Not enough coins.", Toast.LENGTH_LONG).show();
return;
}
app.setBombs(app.getBombs()+1);
app.setCoins(app.getCoins()-5);
// save inventory values
app.saveInventory();
// reload inventory values in fragment
loadInventoryFragment();
}
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
// Do other changes depending on the fragment that is now showing
if (FriendSmashApplication.IS_SOCIAL) {
switch (fragmentIndex) {
case FB_LOGGED_OUT_HOME:
// Hide the progressContainer in FBLoggedOutHomeFragment
if (fragments[FB_LOGGED_OUT_HOME] != null && ((FBLoggedOutHomeFragment)fragments[FB_LOGGED_OUT_HOME]) != null) {
((FBLoggedOutHomeFragment)fragments[FB_LOGGED_OUT_HOME]).progressContainer.setVisibility(View.INVISIBLE);
}
// Set the loggedIn attribute
((FriendSmashApplication)getApplication()).setLoggedIn(false);
break;
case HOME:
// Set the loggedIn attribute
((FriendSmashApplication)getApplication()).setLoggedIn(true);
break;
}
}
}
/* Facebook Integration Only ... */
// Call back on HomeActivity when the session state changes to update the view accordingly
private void updateView() {
if (isResumed) {
Session session = Session.getActiveSession();
if (session.isOpened() && !((FriendSmashApplication)getApplication()).isLoggedIn() && fragments[HOME] != null) {
// Not logged in, but should be, so fetch the user information and log in (load the HomeFragment)
fetchUserInformationAndLogin();
} else if (session.isClosed() && ((FriendSmashApplication)getApplication()).isLoggedIn() && fragments[FB_LOGGED_OUT_HOME] != null) {
// Logged in, but shouldn't be, so load the FBLoggedOutHomeFragment
showFragment(FB_LOGGED_OUT_HOME, false);
}
// Note that error checking for failed logins is done as within an ErrorListener attached to the
// LoginButton within FBLoggedOutHomeFragment
}
}
// Fetch user information and login (i.e switch to the personalized HomeFragment)
private void fetchUserInformationAndLogin() {
final Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// If the session is open, make an API call to get user information required for the app
// Show the progress spinner during this network call
if (fragments[FB_LOGGED_OUT_HOME] != null &&
((FBLoggedOutHomeFragment)fragments[FB_LOGGED_OUT_HOME]).progressContainer != null) {
((FBLoggedOutHomeFragment)fragments[FB_LOGGED_OUT_HOME]).progressContainer.setVisibility(View.VISIBLE);
}
// Create a RequestBatch and add a callback once the batch of requests completes
RequestBatch requestBatch = new RequestBatch();
// Get a list of friends who have _not installed_ the game.
Request invitableFriendsRequest = Request.newGraphPathRequest(session,
"/me/invitable_friends", new Request.Callback() {
@Override
public void onCompleted(Response response) {
FacebookRequestError error = response.getError();
if (error != null) {
Log.e(FriendSmashApplication.TAG, error.toString());
handleError(error, true);
} else if (session == Session.getActiveSession()) {
if (response != null) {
// Get the result
GraphObject graphObject = response.getGraphObject();
JSONArray dataArray = (JSONArray)graphObject.getProperty("data");
List<JSONObject> invitableFriends = new ArrayList<JSONObject>();
if (dataArray.length() > 0) {
// Ensure the user has at least one friend ...
for (int i=0; i<dataArray.length(); i++) {
invitableFriends.add(dataArray.optJSONObject(i));
}
}
((FriendSmashApplication)getApplication()).setInvitableFriends(invitableFriends);
}
}
}
});
Bundle invitableParams = new Bundle();
invitableParams.putString("fields", "id,first_name,picture");
invitableFriendsRequest.setParameters(invitableParams);
requestBatch.add(invitableFriendsRequest);
// Check to see that the user granted the user_friends permission before loading friends.
// This only loads friends who've installed the game.
if (session.getPermissions().contains("user_friends")) {
// Get the user's list of friends
Request friendsRequest = Request.newMyFriendsRequest(session,
new Request.GraphUserListCallback() {
@Override
public void onCompleted(List<GraphUser> users, Response response) {
FacebookRequestError error = response.getError();
if (error != null) {
Log.e(FriendSmashApplication.TAG, error.toString());
handleError(error, true);
} else if (session == Session.getActiveSession()) {
// Set the friends attribute
((FriendSmashApplication)getApplication()).setFriends(users);
}
}
});
Bundle params = new Bundle();
params.putString("fields", "name,first_name,last_name");
friendsRequest.setParameters(params);
requestBatch.add(friendsRequest);
}
// Get current logged in user information
Request meRequest = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
FacebookRequestError error = response.getError();
if (error != null) {
Log.e(FriendSmashApplication.TAG, error.toString());
handleError(error, true);
} else if (session == Session.getActiveSession()) {
// Set the currentFBUser attribute
((FriendSmashApplication)getApplication()).setCurrentFBUser(user);
// Now save the user into Parse.
saveUserToParse(user, session);
}
}
private void saveUserToParse(GraphUser user, Session session) {
}
});
requestBatch.add(meRequest);
requestBatch.addCallback(new RequestBatch.Callback() {
@Override
public void onBatchCompleted(RequestBatch batch) {
if ( ((FriendSmashApplication)getApplication()).getCurrentFBUser() != null) {
// Login by switching to the personalized HomeFragment
loadPersonalizedFragment();
} else {
showError(getString(R.string.error_fetching_profile), true);
}
}
});
// Execute the batch of requests asynchronously
requestBatch.executeAsync();
}
}
// Loads the inventory portion of the HomeFragment.
private void loadInventoryFragment() {
Log.d(TAG, "Loading inventory fragment");
if (isResumed) {
((HomeFragment)fragments[HOME]).loadInventory();
} else {
showError(getString(R.string.error_switching_screens), true);
}
}
// Switches to the personalized HomeFragment as the user has just logged in
private void loadPersonalizedFragment() {
if (isResumed) {
// Personalize the HomeFragment
((HomeFragment)fragments[HOME]).personalizeHomeFragment();
// Load the HomeFragment personalized
showFragment(HOME, false);
} else {
showError(getString(R.string.error_switching_screens), true);
}
}
void handleError(FacebookRequestError error, boolean logout) {
DialogInterface.OnClickListener listener = null;
String dialogBody = null;
if (error == null) {
dialogBody = getString(R.string.error_dialog_default_text);
} else {
switch (error.getCategory()) {
case AUTHENTICATION_RETRY:
// tell the user what happened by getting the message id, and
// retry the operation later
String userAction = (error.shouldNotifyUser()) ? "" :
getString(error.getUserActionMessageId());
dialogBody = getString(R.string.error_authentication_retry, userAction);
listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Intent.ACTION_VIEW, M_FACEBOOK_URL);
startActivity(intent);
}
};
break;
case AUTHENTICATION_REOPEN_SESSION:
// close the session and reopen it.
dialogBody = getString(R.string.error_authentication_reopen);
listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Session session = Session.getActiveSession();
if (session != null && !session.isClosed()) {
session.closeAndClearTokenInformation();
}
}
};
break;
case PERMISSION:
// request the publish permission
dialogBody = getString(R.string.error_permission);
listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (fragments[HOME] != null) {
((HomeFragment) fragments[HOME]).setPendingPost(true);
((HomeFragment) fragments[HOME]).requestPublishPermissions();
}
}
};
break;
case SERVER:
case THROTTLING:
// this is usually temporary, don't clear the fields, and
// ask the user to try again
dialogBody = getString(R.string.error_server);
break;
case BAD_REQUEST:
// this is likely a coding error, ask the user to file a bug
dialogBody = getString(R.string.error_bad_request, error.getErrorMessage());
break;
case CLIENT:
// this is likely an IO error, so tell the user they have a network issue
dialogBody = getString(R.string.network_error);
break;
case OTHER:
default:
// an unknown issue occurred, this could be a code error, or
// a server side issue, log the issue, and either ask the
// user to retry, or file a bug
dialogBody = getString(R.string.error_unknown, error.getErrorMessage());
break;
}
}
new AlertDialog.Builder(this)
.setPositiveButton(R.string.error_dialog_button_text, listener)
.setTitle(R.string.error_dialog_title)
.setMessage(dialogBody)
.show();
if (logout) {
logout();
}
}
// Show user error message as a toast
void showError(String error, boolean logout) {
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
if (logout) {
logout();
}
}
private void logout() {
// Close the session, which will cause a callback to show the logout screen
Session.getActiveSession().closeAndClearTokenInformation();
// Clear any permissions associated with the LoginButton
LoginButton loginButton = (LoginButton) findViewById(R.id.loginButton);
if (loginButton != null) {
loginButton.clearPermissions();
}
}
}
`
答案 0 :(得分:0)
如上所述,你的activity中有空指针异常.Log表示你在GameFragment类中有一个方法'即“getRandomFriendIndex第298行”有一个尚未分配的值。请先检查它,如果可以的话找不到我们的课程,所以我们可以帮助你。