我正在做一个应用程序,它将让 imageButton 从我的项目中调用相机活动捕获图像并将捕获的图像返回给 imageButton。我的问题是我无法调用它不断收到所有这些错误的相机。
错误:
16:29:50.953: D/AndroidRuntime(644): Shutting down VM
08-10 16:29:50.953: W/dalvikvm(644): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-10 16:29:51.003: E/AndroidRuntime(644): FATAL EXCEPTION: main
08-10 16:29:51.003: E/AndroidRuntime(644): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bodapps.kinkytimefree/com.bodapps.kinkytimefree.Player_at_3_Spinner_Menu}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.ImageButton
08-10 16:29:51.003: E/AndroidRuntime(644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-10 16:29:51.003: E/AndroidRuntime(644): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-10 16:29:51.003: E/AndroidRuntime(644): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-10 16:29:51.003: E/AndroidRuntime(644): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-10 16:29:51.003: E/AndroidRuntime(644): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 16:29:51.003: E/AndroidRuntime(644): at android.os.Looper.loop(Looper.java:137)
08-10 16:29:51.003: E/AndroidRuntime(644): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-10 16:29:51.003: E/AndroidRuntime(644): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 16:29:51.003: E/AndroidRuntime(644): at java.lang.reflect.Method.invoke(Method.java:511)
08-10 16:29:51.003: E/AndroidRuntime(644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-10 16:29:51.003: E/AndroidRuntime(644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-10 16:29:51.003: E/AndroidRuntime(644): at dalvik.system.NativeStart.main(Native Method)
08-10 16:29:51.003: E/AndroidRuntime(644): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.ImageButton
08-10 16:29:51.003: E/AndroidRuntime(644): at com.bodapps.kinkytimefree.Player_at_3_Spinner_Menu.onCreate(Player_at_3_Spinner_Menu.java:150)
08-10 16:29:51.003: E/AndroidRuntime(644): at android.app.Activity.performCreate(Activity.java:5008)
08-10 16:29:51.003: E/AndroidRuntime(644): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-10 16:29:51.003: E/AndroidRuntime(644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-10 16:29:51.003: E/AndroidRuntime(644): ... 11 more
08-10 16:29:55.024: I/Process(644): Sending signal. PID: 644 SIG: 9
代码:
package com.bodapps.kinkytimefree;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class Player_at_3_Spinner_Menu extends Activity
{
//Spinners for Players
// public Spinner spinner_1;
// public Spinner spinner_2;
// public Spinner spinner_3;
//TextView for Players
public TextView player_1;
public TextView player_2;
public TextView list_label;
public Animation fade;
//SFX
public static MediaPlayer game_signal, BG;
//Intent
public Intent next_intent;
//Button to Start
private Button play_it;
//For Displaying Text
public String SUMMON_PICK_UP_1, SUMMON_PICK_UP_2, SUMMON_PICK_UP_3;
//Integers
public int waited;
//Text Response from a Spinner
public final static String EXTRA_NEXT_MESSAGE_1 = "com.bodapps.kinkytime.LABEL_2_1";
public final static String EXTRA_NEXT_MESSAGE_2 = "com.bodapps.kinkytime.LABEL_2_2";
private static final int GET_CODE = 0;
//public ImageView image1, image2;
public ImageButton image1, image2;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.players_3);
//Setting the typeface. Put it before finding the ID of the text.
Typeface type_face = Typeface.createFromAsset(this.getAssets(), "fonts/MA Sexy.ttf");
//TextView Initialization
player_1 = (TextView) findViewById(R.id.player_1_spinner);
player_2 = (TextView) findViewById(R.id.player_2_spinner);
list_label = (TextView) findViewById(R.id.textView1);
play_it = (Button) findViewById(R.id.START_GAME);
//Typeface Settings
list_label.setTypeface(type_face);
player_1.setTypeface(null, Typeface.BOLD);
player_2.setTypeface(null, Typeface.BOLD);
//Getting proportional size according to the screen resolution to get the right dimension.
player_1.setTextSize(getResources().getDimension(R.dimen.textsize2));
player_2.setTextSize(getResources().getDimension(R.dimen.textsize2));
list_label.setTextSize(getResources().getDimension(R.dimen.textsize3));
//Set the SFX
game_signal = MediaPlayer.create(this, R.raw.female_sigh_moan);
BG = MediaPlayer.create(this, R.raw.chill_out);
//Play Sequence
BG.setLooping(true); //No loop.
BG.setVolume(22, 22); //Sets the loudness L/R.
//BG.start();
//Set intent.
Intent intent = getIntent();
next_intent = new Intent(Player_at_3_Spinner_Menu.this, Loading_Screen_to_Game.class);
//Searching for ID... (Button)
play_it = (Button) findViewById(R.id.START_GAME);
//Edit Color for Buttons and Text Views
play_it.setTextColor(Color.MAGENTA);
play_it.setBackgroundColor(Color.DKGRAY);
play_it.setTextSize(getResources().getDimension(R.dimen.textsize2));
list_label.setTextColor(Color.YELLOW);
//Adding listener to the button(s).
play_it.setOnClickListener(new trigger_happy_start());
//Retrieve the message.
SUMMON_PICK_UP_1 = intent.getStringExtra(from_3_players.EXTRA_MESSAGE_1);
SUMMON_PICK_UP_2 = intent.getStringExtra(from_3_players.EXTRA_MESSAGE_2);
//Display the retrieved names.
set_info_1(SUMMON_PICK_UP_1);
set_info_2(SUMMON_PICK_UP_2);
//Set the text for the next activity (KinkyTimeActivity.java).
String label_2_1 = player_1.getText().toString();
String label_2_2 = player_2.getText().toString();
next_intent.putExtra(EXTRA_NEXT_MESSAGE_1, label_2_1);
next_intent.putExtra(EXTRA_NEXT_MESSAGE_2, label_2_2);
//Set Animation
fade = AnimationUtils.loadAnimation(this, R.anim.fader);
fade.setFillAfter(true);
//images for the image avatar
image1 = (ImageButton) findViewById(R.id.imageBoy);
image2 = (ImageButton) findViewById(R.id.imageGirl);
}
public void handleClick(View v){
Intent intent = new Intent();
intent.setClass(this, Capture_Main_Activity.class);
startActivityForResult(intent, GET_CODE);
}
public void handleClickOne(View v){
Intent i = new Intent();
i.setClass(this, Capture_Main_Activity.class);
startActivityForResult(i, GET_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GET_CODE)
{
if(resultCode == RESULT_OK)
{
startActivity(getIntent());
}
}
}
//public void onClick(View v)
//{
//Call camera after info has been set
//switch(v.getId()){
//case R.id.imageBoy:
//startActivity(new Intent(Player_at_3_Spinner_Menu.this, Capture_Main_Activity.class));
//break;
//case R.id.imageGirl:
//startActivity(new Intent(Player_at_3_Spinner_Menu.this, Capture_Main_Activity.class));
//break;
//}
//return;
//}
//Set for Name of Player 1
public void set_info_1(String player1)
{
player_1.setText(SUMMON_PICK_UP_1);
}
//Set for Name of Player 2
public void set_info_2(String player2)
{
player_2.setText(SUMMON_PICK_UP_2);
}
private class trigger_happy_start implements OnClickListener
{
public void onClick(View v)
{
//BG.stop();
//Play Sequence
game_signal.setLooping(false); //No loop.
game_signal.setVolume(55, 55); //Sets the loudness L/R.
//game_signal.start();
//Logo played by the animation.
player_1.startAnimation(fade);
player_2.startAnimation(fade);
list_label.startAnimation(fade);
play_it.startAnimation(fade);
Thread entering = new Thread()
{
@Override
public void run()
{
try
{
waited = 0;
while(waited < 3000) //Waits for 3.0 seconds.
{
sleep(100);
waited += 100;
}
}
catch(InterruptedException e)
{
//Do nothing.
}
finally
{
finish();
startActivity(next_intent);;
}
}
};
entering.start();
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageGirl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/girl_6" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="68dp"
android:text="List of Players:"
android:textSize="15sp" />
<LinearLayout
android:id="@+id/spinner_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp"
android:orientation="vertical" >
<TextView
android:id="@+id/player_1_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="empty"
android:textSize="15sp" />
<TextView
android:id="@+id/player_2_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="empty"
android:textSize="15sp" />
</LinearLayout>
<Button
android:id="@+id/START_GAME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp"
android:text="START GAME" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
<ImageButton
android:id="@+id/imageBoy"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_alignBottom="@+id/spinner_layout"
android:layout_alignParentRight="true"
android:src="@drawable/profilepicboy2"
android:onClick="handleClickOne"/>
<ImageButton
android:id="@+id/imageGirl"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/spinner_layout"
android:src="@drawable/profilepicboy2"
android:onClick="handeClick" />
</RelativeLayout>
相机活动代码:
package com.bodapps.kinkytimefree;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Path;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Capture_Main_Activity extends Activity
{
//Variables needed for the camera capture.
private Uri mImageCaptureUri;
private ImageView mImageView;
private String path;
private Bitmap bitmap;
//Camera Status
private static final int PICK_FROM_CAMERA = 1;
private static final int CROP_FROM_CAMERA = 2;
private static final int PICK_FROM_FILE = 3;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.capture_layout);
//Array for the camera images.
final String [] items = new String [] {"Take from camera", "Select from gallery"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Image");
builder.setAdapter( adapter, new DialogInterface.OnClickListener()
{
public void onClick( DialogInterface dialog, int item )
{
//Pick an image taken from the camera.
if (item == 0)
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else { //pick from file
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
} );
final AlertDialog dialog = builder.create();
Button button = (Button) findViewById(R.id.btn_crop);
mImageView = (ImageView) findViewById(R.id.iv_photo); //Checking for player one's image.
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
switch (requestCode) {
case PICK_FROM_CAMERA:
doCrop();
break;
case PICK_FROM_FILE:
mImageCaptureUri = data.getData();
doCrop();
break;
case CROP_FROM_CAMERA:
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
//saveBitmapToFile("/sdcard/bodapps/cropped_img.jpg", photo);
//Place player's image here!
mImageView.setImageBitmap(photo);
}
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) f.delete();
break;
}
}
//private void saveBitmapToFile(String string, Bitmap photo) {
// File file = new File(path);
//boolean res = false; if (!file.exists()) {
//try {
//FileOutputStream fos = new FileOutputStream(file); res = bitmap.compress(CompressFormat.JPEG, 100, fos); fos.close();
// } catch (Exception e) { }
//} return;
//}
//}
private void doCrop()
{
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
if (size == 0)
{
Toast.makeText(this, "Cannot find image crop app.", Toast.LENGTH_SHORT).show();
return;
}
else
{
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (size == 1)
{
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
}
else
{
for (ResolveInfo res : list)
{
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
co.appIntent= new Intent(intent);
co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter( adapter, new DialogInterface.OnClickListener()
{
public void onClick( DialogInterface dialog, int item )
{
startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
}
});
builder.setOnCancelListener( new DialogInterface.OnCancelListener()
{
public void onCancel( DialogInterface dialog )
{
if (mImageCaptureUri != null ) {
getContentResolver().delete(mImageCaptureUri, null, null );
mImageCaptureUri = null;
}
}
} );
AlertDialog alert = builder.create();
alert.show();
}
}
}
}