启动以下活动后,我的应用程序意外崩溃。基本上在此活动中,用户可以从Facebook上传他们的照片,并将他们的个人资料照片投射到图像视图。
我在log cat消息中包含了以下内容:
10-17 03:59:52.525: E/AndroidRuntime(1799): FATAL EXCEPTION: main
10-17 03:59:52.525: E/AndroidRuntime(1799): Process: com.dooba.beta, PID: 1799
10-17 03:59:52.525: E/AndroidRuntime(1799): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.ProfileCreation}: java.lang.ClassCastException: com.facebook.widget.ProfilePictureView cannot be cast to android.widget.ImageView
10-17 03:59:52.525: E/AndroidRuntime(1799): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-17 03:59:52.525: E/AndroidRuntime(1799): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-17 03:59:52.525: E/AndroidRuntime(1799): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-17 03:59:52.525: E/AndroidRuntime(1799): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-17 03:59:52.525: E/AndroidRuntime(1799): at android.os.Handler.dispatchMessage(Handler.java:102)
10-17 03:59:52.525: E/AndroidRuntime(1799): at android.os.Looper.loop(Looper.java:136)
10-17 03:59:52.525: E/AndroidRuntime(1799): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-17 03:59:52.525: E/AndroidRuntime(1799): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 03:59:52.525: E/AndroidRuntime(1799): at java.lang.reflect.Method.invoke(Method.java:515)
10-17 03:59:52.525: E/AndroidRuntime(1799): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-17 03:59:52.525: E/AndroidRuntime(1799): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-17 03:59:52.525: E/AndroidRuntime(1799): at dalvik.system.NativeStart.main(Native Method)
10-17 03:59:52.525: E/AndroidRuntime(1799): Caused by: java.lang.ClassCastException: com.facebook.widget.ProfilePictureView cannot be cast to android.widget.ImageView
10-17 03:59:52.525: E/AndroidRuntime(1799): at com.dooba.beta.ProfileCreation.onCreate(ProfileCreation.java:87)
10-17 03:59:52.525: E/AndroidRuntime(1799): at android.app.Activity.performCreate(Activity.java:5231)
10-17 03:59:52.525: E/AndroidRuntime(1799): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-17 03:59:52.525: E/AndroidRuntime(1799): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-17 03:59:52.525: E/AndroidRuntime(1799): ... 11 more
以下是活动代码:
public class ProfileCreation extends Activity {
private static final int RESULT_LOAD_IMAGE = 1;
FrameLayout layout;
Button save;
protected Number age;
private static final String TAG = "ProfileCreation";
protected Button mConfirm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_creation);
RelativeLayout v = (RelativeLayout) findViewById(R.id.main);
v.requestFocus();
Parse.initialize(this, "ID", "DLvq1OqSo87kzEw5j1XNBPKJdXF7dxwRtfEPkxWJ");
mName = (EditText)findViewById(R.id.etxtname);
mAge = (EditText)findViewById(R.id.etxtage);
mHeadline = (EditText)findViewById(R.id.etxtheadline);
mprofilePicture = (ImageView)findViewById(R.id.profilePicturePreview);
male = (RadioButton)findViewById(R.id.rimale);
female = (RadioButton)findViewById(R.id.rifemale);
lmale = (RadioButton)findViewById(R.id.rlmale);
lfemale = (RadioButton)findViewById(R.id.rlfemale);
seekBarMinimum = (SeekBar) findViewById(R.id.sbseekBarMinimumAge);
seekBarMaximum = (SeekBar) findViewById(R.id.sbseekBarMaximumAge);
seekBarDistance = (SeekBar) findViewById(R.id.sbseekBarDistance);
seekBarActivityDistance = (SeekBar) findViewById(R.id.sbseekBarActivityDistance);
mConfirm = (Button)findViewById(R.id.btnConfirm);
mConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mName.getText().toString();
// Number age = mAge.getText(;
String headline = mHeadline.getText().toString();
// age = ((String) age).trim();
name = name.trim();
headline = headline.trim();
if (name.isEmpty() || headline.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
builder.setMessage(R.string.signup_error_message)
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
// create the new user!
setProgressBarIndeterminateVisibility(true);
ParseUser currentUser = ParseUser.getCurrentUser();
if(male.isChecked())
gender = "Male";
else
gender = "Female";
if(lmale.isChecked())
lgender = "Male";
else
lgender = "Female";
age = Integer.parseInt(mAge.getText().toString());
/* This is the section where the images is converted, saved, and uploaded. I have not been able Locate the image from the ImageView, where the user uploads the picture to imageview from either their gallery and later on from facebook */
ImageView myImgView = (ImageView) findViewById(R.id.profilePicturePreview);
Bitmap bitmap = ((BitmapDrawable) myImgView.getDrawable()).getBitmap();
// Convert it to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Compress image to lower quality scale 1 - 100
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
// Create the ParseFile
ParseFile file = new ParseFile("profilePicture.png", image);
// Upload the image into Parse Cloud
file.saveInBackground();
// Create a column named "Profile Picture" and set the string
currentUser.put("ImageName", "Profile Picture");
// Create a column named "ImageFile" and insert the image
currentUser.put("ProfilePicture", file);
currentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Success!
Intent intent = new Intent(ProfileCreation.this, MoodActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(ProfileCreation.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.signup_error_message)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}
});
SeekBar seekBar = (SeekBar) findViewById(R.id.sbseekBarDistance);
final TextView seekBarValue = (TextView) findViewById(R.id.tvseekBarDistanceValue);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
Button mcancel = (Button)findViewById(R.id.btnBack);
mcancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProfileCreation.this.startActivity(new Intent(ProfileCreation.this, LoginActivity.class));
}
});
SeekBar seekBarActivity = (SeekBar) findViewById(R.id.sbseekBarActivityDistance);
final TextView seekBarActivityValue = (TextView) findViewById(R.id.tvseekBarActivityDistanceValue);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
seekBarValue.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
SeekBar seekBarMinimum = (SeekBar) findViewById(R.id.sbseekBarMinimumAge);
final TextView txtMinimum = (TextView) findViewById(R.id.tvMinAge);
seekBarMinimum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if (progress <= 18) {
seekBar.setProgress(18);
} else {
txtMinimum.setText(String.valueOf(progress));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
SeekBar seekBarMaximum = (SeekBar) findViewById(R.id.sbseekBarMaximumAge);
final TextView txtMaximum = (TextView) findViewById(R.id.tvMaxAge);
seekBarMaximum.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
txtMaximum.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}); // Add this
Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
final ProfilePictureView mprofilePictureView = (ProfilePictureView) findViewById(R.id.profilePicturePreview);
LoginButton authButton = (LoginButton) findViewById(R.id.btnFacebookUpload);
authButton.setSessionStatusCallback(new Session.StatusCallback() {
@SuppressWarnings("deprecation")
@Override
public void call(Session session, SessionState state,
Exception exception) {
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
mprofilePictureView.setProfileId(user.getId());
}
}
});
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
private byte[] readInFile(String path) throws IOException {
// TODO Auto-generated method stub
byte[] data = null;
File file = new File(path);
InputStream input_stream = new BufferedInputStream(new FileInputStream(
file));
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
data = new byte[16384]; // 16K
int bytes_read;
while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, bytes_read);
}
input_stream.close();
return buffer.toByteArray();
}
}
下面的是布局代码:
<com.facebook.widget.ProfilePictureView
android:id="@+id/profilePicturePreview"
android:layout_width="132dp"
android:layout_height="120dp"
android:layout_alignTop="@+id/btnFacebookUpload"
android:layout_toRightOf="@+id/btnPictureSelect"
android:alpha="1"
android:background="@drawable/border_image"
android:cropToPadding="true"
android:padding="3dp"
android:scaleType="centerCrop" />
答案 0 :(得分:1)
ProfilePictureView
延伸FrameLayout
而非ImageView
。这就是你获得ClassCastException
的原因。 Here您可以找到文档
答案 1 :(得分:1)
ImageView profilePicture ;
mprofilePicture = (ImageView)findViewById(R.id.profilePicturePreview);
替换为
ProfilePictureView mprofilePicture;
mprofilePicture = (ProfilePictureView) findViewById(R.id.profilePicturePreview);
并导入此
import com.facebook.widget.ProfilePictureView;