2013-05-09 119 views
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

發佈整個堆棧跟蹤。 – hardartcore 2013-05-09 14:41:34

+1

可能是內存問題。比如說,你爲什麼要創建兩個相同的位圖?把最終的位圖位圖= Bitmap.createBitmap(imgAdapt.getPic(position));在您的方法開始並將它用於setImageBitmap()s – DigCamara 2013-05-09 14:43:51

+0

您沒有刪除//設置較大圖像視圖以顯示適配器類的選定位圖調用方法 picView.setImageBitmap(imgAdapt.getPic(position)) ; – DigCamara 2013-05-09 16:13:30

回答

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> 
+0

感謝您的答覆,但應用程序仍然崩潰,我有編輯和張貼我的網頁的所有代碼,請看看 – Aaron 2013-05-09 16:10:47

+0

對不起,如何發佈logcat在這裏?我把logcat粘貼到盒子上,但是它太多了,超過了30000個字的限制,有沒有辦法在這裏上傳一個txt文件? – Aaron 2013-05-09 16:19:13

+0

將它貼在http://pastebin.com上並粘貼鏈接。 – 2013-05-09 16:22:00

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> 
+0

謝謝你的回覆,兄弟。但它不工作,應用程序仍然崩潰時,警報對話框試圖彈出(實際上它沒有機會在崩潰之前彈出)... – Aaron 2013-05-09 15:51:05