1

每当我尝试在警报对话框上显示我的位图图像时遇到问题,应用程序将通过显示“不幸 blabla .. 已停止”来强制关闭或崩溃,我猜我的位图图像有问题,any1 可以帮忙吗?谢谢,下面是我的代码:

enter code here



@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.marker_detail);

    String valueretrievee=null;



    //valueretrieve="2";




    //get the large image view
    picView = (ImageView) findViewById(R.id.picture);
    //get the gallery view
    picGallery = (Gallery) findViewById(R.id.gallery);

    //create a new adapter
    imgAdapt = new PicAdapter(this);
    //set the gallery adapter
    picGallery.setAdapter(imgAdapt);


    //set the click listener for each item in the thumbnail gallery
    picGallery.setOnItemClickListener(new OnItemClickListener() {
        //handle clicks
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {



            //set the larger image view to display the chosen bitmap calling method of adapter class
            picView.setImageBitmap(imgAdapt.getPic(position));
            //start

     AlertDialog.Builder alertadd = new AlertDialog.Builder(ARTAADAfterTouch.this);
     //ImageView imageView = (ImageView) findViewById(R.id.picture);


     ImageView imageView = (ImageView) findViewById(R.id.picture);
     final Bitmap bitmap = Bitmap.createBitmap(imgAdapt.getPic(position));

     imageView.setImageBitmap(bitmap);
     alertadd.setView(imageView);
    alertadd.setPositiveButton("Yes", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();    
        }
    });
    alertadd.setNegativeButton("No", null);
    alertadd.show();

    //end
    picView.setImageBitmap(bitmap);
        }
    });

}

public void setData(String valueretrieve)
{
    this.valueretrieve=valueretrieve;
}
public String getData()
{
    return valueretrieve;
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{


    if (resultCode == RESULT_OK) {
        //check if we are returning from picture selection
        if (requestCode == PICKER) {
                //import the image
        }
    }
    //superclass method
    super.onActivityResult(requestCode, resultCode, data);
    //the returned picture URI

    Uri pickedUri = data.getData();
    //declare the bitmap
    Bitmap pic = null;
    //declare the path string
    String imgPath = "";
    //retrieve the string using media data
    String[] medData = { MediaStore.Images.Media.DATA };
    //query the data
    Cursor picCursor = managedQuery(pickedUri, medData, null, null, null);
    if(picCursor!=null)
    {
        //get the path string
        int index = picCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        picCursor.moveToFirst();
        imgPath = picCursor.getString(index);
    }
    else
        imgPath = pickedUri.getPath();

    //pass bitmap to ImageAdapter to add to array
    imgAdapt.addPic(pic);
    //redraw the gallery thumbnails to reflect the new addition
    picGallery.setAdapter(imgAdapt);

    //display the newly selected image at larger size
    picView.setImageBitmap(pic);
    //scale options
    picView.setScaleType(ImageView.ScaleType.FIT_CENTER);
}





public class PicAdapter extends BaseAdapter 
{
    //use the default gallery background image
    int defaultItemBackground;
    //gallery context
    private Context galleryContext;
    //array to store bitmaps to display
    private Bitmap[] imageBitmaps;
    //placeholder bitmap for empty spaces in gallery
    Bitmap placeholder;


    //Intent ft= getIntent(); // gets the previously created intent
    //String firstKeyName = ft.getStringExtra("firstKeyName"); // will return "FirstKeyValue"



    public PicAdapter(Context c) {
        //instantiate context
        galleryContext = c;






        //copy right by aaron yong -> OVER HERE



        //String firstKeyName = intent.getStringExtra("firstKeyName"); // will return "SecondKeyValue"
        //create bitmap array
        imageBitmaps  = new Bitmap[5];
        //decode the placeholder image
        //placeholder  = BitmapFactory.decodeResource(getResources(), R.drawable.hotel1);
        //placeholder = BitmapFactory.decodeResource(getResources(), R.drawable.hotel2);
        //more processing



        //Intent intent = getIntent();
        //String valueretrieve = intent.getStringExtra("firstKeyName");



      //set placeholder as all thumbnail images in the gallery initially
        for(int i=0; i<imageBitmaps.length; i++)
        {

            Bundle extras = getIntent().getExtras();
            if (extras != null) {
               valueretrieve = extras.getString("myFirstKey");
            }

            TextView theTextView = (TextView) findViewById(R.id.textView2);
            theTextView.setText(valueretrieve);

            //copy right by aaron yong ->> YESS! msut use equal!!
            if(theTextView.getText().equals("1"))
            {
                imageBitmaps[0]  = BitmapFactory.decodeResource(getResources(), R.drawable.shop1);
                imageBitmaps[1] = BitmapFactory.decodeResource(getResources(), R.drawable.hotel1);
            }
            else if(theTextView.getText().equals("2"))
            {
                imageBitmaps[0]  = BitmapFactory.decodeResource(getResources(), R.drawable.it1);
                imageBitmaps[1] = BitmapFactory.decodeResource(getResources(), R.drawable.hotel2);
            }
            else if(theTextView.getText().equals("3"))
            {
                imageBitmaps[0]  = BitmapFactory.decodeResource(getResources(), R.drawable.transport1);
                imageBitmaps[1] = BitmapFactory.decodeResource(getResources(), R.drawable.transport2);
            }


            //imageBitmaps[0]  = BitmapFactory.decodeResource(getResources(), R.drawable.hotel1);
            //imageBitmaps[1] = BitmapFactory.decodeResource(getResources(), R.drawable.hotel2);
            imageBitmaps[i]=placeholder;
        }






      //get the styling attributes - use default Andorid system resources
        TypedArray styleAttrs = galleryContext.obtainStyledAttributes(R.styleable.PicGallery);
        //get the background resource
        defaultItemBackground = styleAttrs.getResourceId(
            R.styleable.PicGallery_android_galleryItemBackground, 0);
        //recycle attributes
        styleAttrs.recycle();
    }


    //*************************************************




    //return number of data items i.e. bitmap images
    public int getCount() {
        return imageBitmaps.length;
    }

    //return item at specified position
    public Object getItem(int position) {
        return position;
    }

    //return item ID at specified position
    public long getItemId(int position) {
        return position;
    }

    //get view specifies layout and display options for each thumbnail in the gallery
    public View getView(int position, View convertView, ViewGroup parent) {
        //create the view
        ImageView imageView = new ImageView(galleryContext);
        //specify the bitmap at this position in the array
        imageView.setImageBitmap(imageBitmaps[position]);
        //set layout options
        imageView.setLayoutParams(new Gallery.LayoutParams(470, 400));
        //scale type within view area
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        //set default gallery item background
        imageView.setBackgroundResource(defaultItemBackground);
        //return the view
        return imageView;
    }

    //helper method to add a bitmap to the gallery when the user chooses one
    public void addPic(Bitmap newPic)
    {
        //set at currently selected index
        imageBitmaps[currentPic] = newPic;
    }

    //return bitmap at specified position for larger display
    public Bitmap getPic(int posn)
    {
        //return bitmap at posn index
        return imageBitmaps[posn];
    }

}

}

txt格式的logcat链接:http: //pastebin.com/4DSwxXx8

相关的logcat

05-10 00:22:24.867: E/AndroidRuntime(2276): FATAL EXCEPTION: main
05-10 00:22:24.867: E/AndroidRuntime(2276): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.addView(ViewGroup.java:3210)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.addView(ViewGroup.java:3186)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at com.android.internal.app.AlertController.setupView(AlertController.java:413)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at com.android.internal.app.AlertController.installContent(AlertController.java:241)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.app.AlertDialog.onCreate(AlertDialog.java:337)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.app.Dialog.dispatchOnCreate(Dialog.java:355)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.app.Dialog.show(Dialog.java:260)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at com.apu.ARTAAD.activity.ARTAADAfterTouch$1.onItemClick(ARTAADAfterTouch.java:123)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.widget.Gallery.onSingleTapUp(Gallery.java:981)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.GestureDetector.onTouchEvent(GestureDetector.java:588)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.widget.Gallery.onTouchEvent(Gallery.java:958)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.View.dispatchTouchEvent(View.java:7246)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2090)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1417)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2038)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.View.dispatchPointerEvent(View.java:7426)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.os.MessageQueue.nativePollOnce(Native Method)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.os.MessageQueue.next(MessageQueue.java:125)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.os.Looper.loop(Looper.java:124)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at android.app.ActivityThread.main(ActivityThread.java:5195)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at java.lang.reflect.Method.invokeNative(Native Method)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at java.lang.reflect.Method.invoke(Method.java:511)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-10 00:22:24.867: E/AndroidRuntime(2276):     at dalvik.system.NativeStart.main(Native Method)
enter code here
4

2 回答 2

3

你应该把它改成

final View alertDialog= factory.inflate(R.layout.alert_dialog, null);
ImageView imageView= (ImageView) view
                .findViewById(R.id.selectedImage);
imageView.setImageBitmap(imgAdapt.getPic(position));

AlertDialog.Builder alertadd = new AlertDialog.Builder(ARTAADAfterTouch.this);

 alertadd.setView(alertDialog);
alertadd.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish();    
    }
});
alertadd.setNegativeButton("No", null);
alertadd.show();

将此添加到您的布局目录(将其命名为“alert_dialog.xml”)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/selectedImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />
</RelativeLayout>
于 2013-05-09T15:55:22.980 回答
0

您应该尝试使 imageView 无效。

尝试这个 :

imageView.setImageBitmap(bitmap);
imageView.invalidate();

我不确定这个解决方案是否确实有效。

但是这个有,

final View view = factory.inflate(R.layout.image_view, null);
            ImageView imageView = (ImageView) view
                    .findViewById(R.id.finalImage);
            imageView.setImageBitmap(bitmap);
            imageView.invalidate();

哪里,image_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/finalImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox"
        android:src="@drawable/img" />
</RelativeLayout>
于 2013-05-09T14:53:10.570 回答