私のアプリは、ユーザーがビデオを Facebook ウォールにアップロードできるようにするためのものです。次のコードは、動作するコードの他の多くの例と非常によく似ています。このコードでは、null ポインター例外が発生します。
これが私のメインクラスです:
public class MainActivity extends Activity {
Session session;
Facebook facebook=new Facebook("367714506687419");
private int mAuthAttempts = 0;
String access_token=null;
long expires=0;
Context context;
String Permissions[]=new String[] {
"publish_stream", "read_stream" , "video_upload" , "publish_actions" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button logout=(Button) findViewById(R.id.logout);
context=this;
Button upload_video=(Button) findViewById(R.id.upload_video);
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
@Override
public void call( Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@SuppressWarnings("deprecation")
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
TextView welcome = (TextView) findViewById(R.id.welcome);
welcome.setText("Hello " + user.getName() + "!");
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(MainActivity.this, Permissions, new DialogListener(){
@Override
public void onComplete(Bundle values) {
access_token=facebook.getAccessToken();
expires=facebook.getAccessExpires();
}
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
}
}
}
});
}
}
});
upload_video.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
if(session.isOpened()){
//String dataPath = "/mnt/sdcard/DCIM/Camera/VID_20130725_023450.mp4";
String path="/sdcard/DCIM/Camera/1.mp4";
byte[] data = null;
String dataPath = "/sdcard/DCIM/Camera/1.mp4";
String dataMsg = "Your video description here.";
String dataName = "1.mp4";
Bundle param;
facebook = new Facebook("367714506687419");
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
InputStream is = null;
try {
is = new FileInputStream(dataPath);
data = readBytes(is);
param = new Bundle();
param.putString(Facebook.TOKEN, access_token);
param.putString("message", dataMsg);
param.putByteArray("video", data);
//param.putString("filename", dataName);
mAsyncRunner.request("me/videos", param, "POST", new fbRequestListener(), null);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
else{
Log.d("Please Check ur access tocken", "");
}
}
});
そして、ここに fbRequestListener.java があります
public class fbRequestListener は RequestListener を実装します {
@Override
public void onComplete(String response, Object state) {
Log.e("response", response);
}
@Override
public void onIOException(IOException e, Object state) {
Log.e("", "onIOException");
e.printStackTrace();
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
Log.e("", "onFileNotFoundException");
e.printStackTrace();
}
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
Log.e("", "onMalformedURLException");
e.printStackTrace();
}
@Override
public void onFacebookError(FacebookError e, Object state) {
Log.e("", "onFacebookError");
e.printStackTrace();
}
}
ログキャットは:
致命的な例外: Thread-1044 java.lang.NullPointerException
libcore.net.UriCodec.decode (UriCodec.java:149) で
java.net.URLDecoder.decode(URLDecoder.java:45) で
com.facebook.android.Util.openUrl (Util.java:186) で
com.facebook.android.Facebook.requestImpl (Facebook.java:806) で
com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:** で