我是Android APP的新手。我下载facebook sdk for android(https://developers.facebook.com/android/)。除了在模拟器中运行外,一切都很好。 SDK中的所有示例都无法正常运行,并且会弹出“不幸,XXX已停止”的错误。有人会遇到类似的问题吗?谢谢。 这是LogCat中的日志:
06-02 16:33:41.165: D/dalvikvm(536): Not late-enabling CheckJNI (already on)
06-02 16:33:41.425: I/dalvikvm(536): Turning on JNI app bug workarounds for target SDK version 8...
06-02 16:33:41.715: W/dalvikvm(536): Unable to resolve superclass of Lcom/facebook/samples/hellofacebook/HelloFacebookSampleActivity; (59)
06-02 16:33:41.715: W/dalvikvm(536): Link of class 'Lcom/facebook/samples/hellofacebook/HelloFacebookSampleActivity;' failed
06-02 16:33:41.715: D/AndroidRuntime(536): Shutting down VM
06-02 16:33:41.715: W/dalvikvm(536): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-02 16:33:41.755: E/AndroidRuntime(536): FATAL EXCEPTION: main
06-02 16:33:41.755: E/AndroidRuntime(536): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.facebook.samples.hellofacebook/com.facebook.samples.hellofacebook.HelloFacebookSampleActivity}: java.lang.ClassNotFoundException: com.facebook.samples.hellofacebook.HelloFacebookSampleActivity
06-02 16:33:41.755: E/AndroidRuntime(536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
06-02 16:33:41.755: E/AndroidRuntime(536): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-02 16:33:41.755: E/AndroidRuntime(536): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-02 16:33:41.755: E/AndroidRuntime(536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-02 16:33:41.755: E/AndroidRuntime(536): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 16:33:41.755: E/AndroidRuntime(536): at android.os.Looper.loop(Looper.java:137)
06-02 16:33:41.755: E/AndroidRuntime(536): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-02 16:33:41.755: E/AndroidRuntime(536): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 16:33:41.755: E/AndroidRuntime(536): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 16:33:41.755: E/AndroidRuntime(536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-02 16:33:41.755: E/AndroidRuntime(536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-02 16:33:41.755: E/AndroidRuntime(536): at dalvik.system.NativeStart.main(Native Method)
06-02 16:33:41.755: E/AndroidRuntime(536): Caused by: java.lang.ClassNotFoundException: com.facebook.samples.hellofacebook.HelloFacebookSampleActivity
06-02 16:33:41.755: E/AndroidRuntime(536): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-02 16:33:41.755: E/AndroidRuntime(536): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-02 16:33:41.755: E/AndroidRuntime(536): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-02 16:33:41.755: E/AndroidRuntime(536): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
06-02 16:33:41.755: E/AndroidRuntime(536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
06-02 16:33:41.755: E/AndroidRuntime(536): ... 11 more
06-02 16:33:42.365: I/dalvikvm(536): threadid=3: reacting to signal 3
06-02 16:33:42.375: I/dalvikvm(536): Wrote stack traces to '/data/anr/traces.txt'
06-02 16:33:42.875: I/dalvikvm(536): threadid=3: reacting to signal 3
06-02 16:33:42.885: I/dalvikvm(536): Wrote stack traces to '/data/anr/traces.txt'
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.facebook.samples.hellofacebook"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application android:label="@string/app_name"
android:icon="@drawable/icon"
android:theme="@android:style/Theme.NoTitleBar"
>
<activity android:name=".HelloFacebookSampleActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.facebook.LoginActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
</application>
</manifest>
以下是源代码:
/**
* Copyright 2012 Facebook
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.samples.hellofacebook;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.facebook.*;
import com.facebook.model.GraphObject;
import com.facebook.model.GraphPlace;
import com.facebook.model.GraphUser;
import com.facebook.widget.*;
import java.util.*;
public class HelloFacebookSampleActivity extends FragmentActivity {
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private static final Location SEATTLE_LOCATION = new Location("") {
{
setLatitude(47.6097);
setLongitude(-122.3331);
}
};
private final String PENDING_ACTION_BUNDLE_KEY = "com.facebook.samples.hellofacebook:PendingAction";
private Button postStatusUpdateButton;
private Button postPhotoButton;
private Button pickFriendsButton;
private Button pickPlaceButton;
private LoginButton loginButton;
private ProfilePictureView profilePictureView;
private TextView greeting;
private PendingAction pendingAction = PendingAction.NONE;
private ViewGroup controlsContainer;
private GraphUser user;
private enum PendingAction {
NONE,
POST_PHOTO,
POST_STATUS_UPDATE
}
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
if (savedInstanceState != null) {
String name = savedInstanceState.getString(PENDING_ACTION_BUNDLE_KEY);
pendingAction = PendingAction.valueOf(name);
}
setContentView(R.layout.main);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
HelloFacebookSampleActivity.this.user = user;
updateUI();
// It's possible that we were waiting for this.user to be populated in order to post a
// status update.
handlePendingAction();
}
});
profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);
greeting = (TextView) findViewById(R.id.greeting);
postStatusUpdateButton = (Button) findViewById(R.id.postStatusUpdateButton);
postStatusUpdateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onClickPostStatusUpdate();
}
});
postPhotoButton = (Button) findViewById(R.id.postPhotoButton);
postPhotoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onClickPostPhoto();
}
});
pickFriendsButton = (Button) findViewById(R.id.pickFriendsButton);
pickFriendsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onClickPickFriends();
}
});
pickPlaceButton = (Button) findViewById(R.id.pickPlaceButton);
pickPlaceButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
onClickPickPlace();
}
});
controlsContainer = (ViewGroup) findViewById(R.id.main_ui_container);
final FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment != null) {
// If we're being re-created and have a fragment, we need to a) hide the main UI controls and
// b) hook up its listeners again.
controlsContainer.setVisibility(View.GONE);
if (fragment instanceof FriendPickerFragment) {
setFriendPickerListeners((FriendPickerFragment) fragment);
} else if (fragment instanceof PlacePickerFragment) {
setPlacePickerListeners((PlacePickerFragment) fragment);
}
}
// Listen for changes in the back stack so we know if a fragment got popped off because the user
// clicked the back button.
fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
if (fm.getBackStackEntryCount() == 0) {
// We need to re-show our UI.
controlsContainer.setVisibility(View.VISIBLE);
}
}
});
}
@Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
updateUI();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
outState.putString(PENDING_ACTION_BUNDLE_KEY, pendingAction.name());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (pendingAction != PendingAction.NONE &&
(exception instanceof FacebookOperationCanceledException ||
exception instanceof FacebookAuthorizationException)) {
new AlertDialog.Builder(HelloFacebookSampleActivity.this)
.setTitle(R.string.cancelled)
.setMessage(R.string.permission_not_granted)
.setPositiveButton(R.string.ok, null)
.show();
pendingAction = PendingAction.NONE;
} else if (state == SessionState.OPENED_TOKEN_UPDATED) {
handlePendingAction();
}
updateUI();
}
private void updateUI() {
Session session = Session.getActiveSession();
boolean enableButtons = (session != null && session.isOpened());
postStatusUpdateButton.setEnabled(enableButtons);
postPhotoButton.setEnabled(enableButtons);
pickFriendsButton.setEnabled(enableButtons);
pickPlaceButton.setEnabled(enableButtons);
if (enableButtons && user != null) {
profilePictureView.setProfileId(user.getId());
greeting.setText(getString(R.string.hello_user, user.getFirstName()));
} else {
profilePictureView.setProfileId(null);
greeting.setText(null);
}
}
@SuppressWarnings("incomplete-switch")
private void handlePendingAction() {
PendingAction previouslyPendingAction = pendingAction;
// These actions may re-set pendingAction if they are still pending, but we assume they
// will succeed.
pendingAction = PendingAction.NONE;
switch (previouslyPendingAction) {
case POST_PHOTO:
postPhoto();
break;
case POST_STATUS_UPDATE:
postStatusUpdate();
break;
}
}
private interface GraphObjectWithId extends GraphObject {
String getId();
}
private void showPublishResult(String message, GraphObject result, FacebookRequestError error) {
String title = null;
String alertMessage = null;
if (error == null) {
title = getString(R.string.success);
String id = result.cast(GraphObjectWithId.class).getId();
alertMessage = getString(R.string.successfully_posted_post, message, id);
} else {
title = getString(R.string.error);
alertMessage = error.getErrorMessage();
}
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(alertMessage)
.setPositiveButton(R.string.ok, null)
.show();
}
private void onClickPostStatusUpdate() {
performPublish(PendingAction.POST_STATUS_UPDATE);
}
private void postStatusUpdate() {
if (user != null && hasPublishPermission()) {
final String message = getString(R.string.status_update, user.getFirstName(), (new Date().toString()));
Request request = Request
.newStatusUpdateRequest(Session.getActiveSession(), message, new Request.Callback() {
@Override
public void onCompleted(Response response) {
showPublishResult(message, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
private void onClickPostPhoto() {
performPublish(PendingAction.POST_PHOTO);
}
private void postPhoto() {
if (hasPublishPermission()) {
Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
Request request = Request.newUploadPhotoRequest(Session.getActiveSession(), image, new Request.Callback() {
@Override
public void onCompleted(Response response) {
showPublishResult(getString(R.string.photo_post), response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_PHOTO;
}
}
private void showPickerFragment(PickerFragment<?> fragment) {
fragment.setOnErrorListener(new PickerFragment.OnErrorListener() {
@Override
public void onError(PickerFragment<?> pickerFragment, FacebookException error) {
showAlert(getString(R.string.error), error.getMessage());
}
});
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit();
controlsContainer.setVisibility(View.GONE);
// We want the fragment fully created so we can use it immediately.
fm.executePendingTransactions();
fragment.loadData(false);
}
private void onClickPickFriends() {
final FriendPickerFragment fragment = new FriendPickerFragment();
setFriendPickerListeners(fragment);
showPickerFragment(fragment);
}
private void setFriendPickerListeners(final FriendPickerFragment fragment) {
fragment.setOnDoneButtonClickedListener(new FriendPickerFragment.OnDoneButtonClickedListener() {
@Override
public void onDoneButtonClicked(PickerFragment<?> pickerFragment) {
onFriendPickerDone(fragment);
}
});
}
private void onFriendPickerDone(FriendPickerFragment fragment) {
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack();
String results = "";
Collection<GraphUser> selection = fragment.getSelection();
if (selection != null && selection.size() > 0) {
ArrayList<String> names = new ArrayList<String>();
for (GraphUser user : selection) {
names.add(user.getName());
}
results = TextUtils.join(", ", names);
} else {
results = getString(R.string.no_friends_selected);
}
showAlert(getString(R.string.you_picked), results);
}
private void onPlacePickerDone(PlacePickerFragment fragment) {
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack();
String result = "";
GraphPlace selection = fragment.getSelection();
if (selection != null) {
result = selection.getName();
} else {
result = getString(R.string.no_place_selected);
}
showAlert(getString(R.string.you_picked), result);
}
private void onClickPickPlace() {
final PlacePickerFragment fragment = new PlacePickerFragment();
fragment.setLocation(SEATTLE_LOCATION);
fragment.setTitleText(getString(R.string.pick_seattle_place));
setPlacePickerListeners(fragment);
showPickerFragment(fragment);
}
private void setPlacePickerListeners(final PlacePickerFragment fragment) {
fragment.setOnDoneButtonClickedListener(new PlacePickerFragment.OnDoneButtonClickedListener() {
@Override
public void onDoneButtonClicked(PickerFragment<?> pickerFragment) {
onPlacePickerDone(fragment);
}
});
fragment.setOnSelectionChangedListener(new PlacePickerFragment.OnSelectionChangedListener() {
@Override
public void onSelectionChanged(PickerFragment<?> pickerFragment) {
if (fragment.getSelection() != null) {
onPlacePickerDone(fragment);
}
}
});
}
private void showAlert(String title, String message) {
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setPositiveButton(R.string.ok, null)
.show();
}
private boolean hasPublishPermission() {
Session session = Session.getActiveSession();
return session != null && session.getPermissions().contains("publish_actions");
}
private void performPublish(PendingAction action) {
Session session = Session.getActiveSession();
if (session != null) {
pendingAction = action;
if (hasPublishPermission()) {
// We can do the action right away.
handlePendingAction();
} else {
// We need to get new permissions, then complete the action when we get called back.
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSIONS));
}
}
}
}
答案 0 :(得分:1)
来自logcat
06-02 16:33:41.715: W/dalvikvm(536): Unable to resolve superclass of Lcom/facebook/samples/hellofacebook/HelloFacebookSampleActivity; (59)
06-02 16:33:41.715: W/dalvikvm(536): Link of class 'Lcom/facebook/samples/hellofacebook/HelloFacebookSampleActivity;' failed
您需要将外部.jar文件移动到'libs'文件夹,否则它们的类将不会包含在创建的.dex文件中。
从ADT 17开始会出现此问题。要解决此问题,请确保ADT正确,构建路径已正确配置,外部jar文件位于libs文件夹中。