Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 片段中的ImageSwitcher抛出illegalstateexception,但在活动中工作_Android_Android Fragments_Imageswitcher - Fatal编程技术网

Android 片段中的ImageSwitcher抛出illegalstateexception,但在活动中工作

Android 片段中的ImageSwitcher抛出illegalstateexception,但在活动中工作,android,android-fragments,imageswitcher,Android,Android Fragments,Imageswitcher,我的代码在活动中有效。但是,我决定将其更改为一个片段,现在它从viewswitcher中抛出一个illegalstateexception。我想不出是怎么回事。 总之,我有一些图像用作占位符。单击“上载图像”按钮时,它从返回的路径获取uri,并使用该uri替换imageSwitcher中的占位符图像 代码如下: private Button buttonUpload; private ImageSwitcher imageSwitcher; private ImageButton imageBu

我的代码在活动中有效。但是,我决定将其更改为一个片段,现在它从viewswitcher中抛出一个illegalstateexception。我想不出是怎么回事。 总之,我有一些图像用作占位符。单击“上载图像”按钮时,它从返回的路径获取uri,并使用该uri替换imageSwitcher中的占位符图像

代码如下:

private Button buttonUpload;
private ImageSwitcher imageSwitcher;
private ImageButton imageButton0;
private ImageButton imageButton1;
private ImageButton imageButton2;
private ImageButton imageButton3;
private ImageButton imageButton4;
private ArrayList<String> photoArray;
int counter = 0;
private static int LOAD_IMAGE_RESULTS=1;
private static int PLACEHOLDER_IMAGE = R.drawable.placeholder_2;
int[] imgs = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,R.drawable.pic4,R.drawable.pic5};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View rootView = inflater.inflate(R.layout.new_item_fragment_1, container,false);
    return rootView;
}


public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

    //initialize variables and set default photos
    initVariables();
}




private void initVariables(){   
    //photo array
    photoArray = new ArrayList<String>();

    buttonUpload = (Button) getActivity().findViewById(R.id.button_upload_ni);
    buttonUpload.setOnClickListener(this);

    //image Buttons
    imageButton0 = (ImageButton) getActivity().findViewById(R.id.imageButton0);
    imageButton0.setImageResource(imgs[0]);
    imageButton0.setOnClickListener(this);
    imageButton1 = (ImageButton) getActivity().findViewById(R.id.imageButton1);
    imageButton1.setImageResource(imgs[1]);
    imageButton1.setOnClickListener(this);
    imageButton2 = (ImageButton) getActivity().findViewById(R.id.imageButton2);
    imageButton2.setOnClickListener(this);
    imageButton3 = (ImageButton) getActivity().findViewById(R.id.imageButton3);
    imageButton3.setOnClickListener(this);
    imageButton4 = (ImageButton) getActivity().findViewById(R.id.imageButton4);
    imageButton4.setOnClickListener(this);

    imageSwitcher = (ImageSwitcher) getActivity().findViewById(R.id.image_main_previewer);
    imageSwitcher.setFactory(this);

    //Add animation to the preview
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.abc_fade_in));
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.abc_fade_out));

    //set a default placeholder
    imageSwitcher.setImageResource(PLACEHOLDER_IMAGE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     System.out.println("Got to beginning of onAct");

    // Here we need to check if the activity that was triggers was the Image Gallery.
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
    if (requestCode == LOAD_IMAGE_RESULTS && resultCode == Activity.RESULT_OK && data != null) {
        // Let's read picked image data - its URI
        Uri pickedImageUri = data.getData();
        // Let's read picked image path using content resolver
    //    String[] filePath = { MediaStore.Images.Media.DATA };
    //    Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
   //     cursor.moveToFirst();
   //     String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        // Now we need to set the GUI ImageView data with data read from the picked file.
     //   uploadView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
        //setNewImage(pickedImageUri);

        String finalPath = getFinalFilePath(pickedImageUri);
        setNewImage(pickedImageUri, finalPath);

        // At the end remember to close the cursor or you will end with the RuntimeException!
 //       cursor.close();

        System.out.println("Got to end of onAct");

    }
}



private void setNewImage(Uri uri, String filePath){
    System.out.println("Got to beginning of setNewImage");

    imageSwitcher.setImageURI(uri);

    //also set the image for the thumb nail
    //int buttonId = getThumbButton();
//  ImageButton changeButton = (ImageButton) getActivity().findViewById(buttonId);
//  changeButton.setImageBitmap(decodeSampledBitmapFromFile(filePath,40,40));

    //finally save the Bitmap
    addPathToArray(filePath);
    System.out.println("Got to end of setNewImage");
}




private void addPathToArray(String filePath){
    //Bitmap tempItemImage = BitmapFactory.decodeFile(filePath);
    System.out.println("Entered addBit");
    photoArray.add(filePath);
    System.out.println("Exit addBit "+photoArray.size());
}




private int getThumbButton(){
    //get the size of the array
    int arraySize = photoArray.size();
    int buttonId = 0;
    System.out.println("Entered getThumb");
    //switch through to find button to change
    switch(arraySize){
    case 0:
        buttonId = R.id.imageButton0;
        break;
    case 1:
        buttonId = R.id.imageButton1;
        break;
    case 2:
        buttonId = R.id.imageButton2;
        break;
    case 3:
        buttonId = R.id.imageButton3;
        break;
    case 4:
        buttonId = R.id.imageButton4;
        break;
    default:;

    }

    return buttonId;
}




private String getFinalFilePath(Uri uri){
    String[] filePath = { MediaStore.Images.Media.DATA };
    Cursor cursor = getActivity().getContentResolver().query(uri, filePath, null, null, null);
    cursor.moveToFirst();
    String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
    cursor.close();

    //Now we need to set the GUI ImageView data with data read from the picked file.
      return imagePath;  
}




@Override
public void onClick(View view) {

    int viewId = view.getId();

    switch(viewId){
    case R.id.button_upload_ni:
        getImageFromGallery();
        break;
    case R.id.imageButton0:
        setImageAtPosition(0);
        break;
    case R.id.imageButton1:
        setImageAtPosition(1);
        break;
    case R.id.imageButton2:
        setImageAtPosition(2);
        break;
    case R.id.imageButton3:
        setImageAtPosition(3);
        break;
    case R.id.imageButton4:
        setImageAtPosition(4);
        break;
    default: //nothing here
        ;
    }

}




private void setImageAtPosition(int position){
//  imageSwitcher.setImageResource(imgs[position]);
//  imageSwitcher.setImageDrawable(Drawable.createFromPath(photoArray.get(position)));
}




private void getImageFromGallery(){
   // Create the Intent for Image Gallery.
   Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

   // Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.
   startActivityForResult(i, LOAD_IMAGE_RESULTS);   
}





@Override
public View makeView() {
    // create view
    System.out.println("Make view called");
    counter++;
    System.out.println("setNewImage counter= "+counter);
    ImageView view = new ImageView(getActivity());
    view.setScaleType(ImageView.ScaleType.FIT_CENTER);
    view.setLayoutParams(new ImageSwitcher.LayoutParams (android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.MATCH_PARENT));
    view.setBackgroundColor(0xFF000000);
    System.out.println("Make view ending");
    return view;
}




public static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight){

    //first check dimensions with inJustDecodeBounds=true 
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath,options);

    //calculate inSampledSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    //Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filePath, options);
}




public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight){
    //raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if(height>reqHeight || width > reqWidth){
        final int halfHeight = height/2;
        final int halfWidth = width/2;

        //calculate the highest insamplesize value...
        while((halfHeight/inSampleSize)>reqHeight && (halfWidth/inSampleSize)>reqWidth){
            inSampleSize *=2;
        }
    }
    return inSampleSize;
}
private按钮加载;
专用图像切换器;
私有ImageButtonImageButton0;
私有图像按钮imageButton1;
私有图像按钮imageButton2;
私有图像按钮图像按钮3;
私有图像按钮图像按钮4;
私有阵列列表光阵列;
int计数器=0;
私有静态整型加载\图像\结果=1;
私有静态整型占位符_IMAGE=R.drawable.PLACEHOLDER_2;
int[]imgs={R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5};
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
//TODO自动生成的方法存根
视图根视图=充气机。充气(R.layout.new_item_fragment_1,容器,错误);
返回rootView;
}
public void onStart(){
//TODO自动生成的方法存根
super.onStart();
//初始化变量并设置默认照片
initVariables();
}
私有void initVariables(){
//照片阵列
photoArray=新的ArrayList();
buttonUpload=(Button)getActivity().findViewById(R.id.Button\u upload\u ni);
buttonUpload.setOnClickListener(这个);
//图像按钮
imageButton0=(ImageButton)getActivity().findViewById(R.id.imageButton0);
imageButton0.setImageResource(imgs[0]);
imageButton0.setOnClickListener(此);
imageButton1=(ImageButton)getActivity().findViewById(R.id.imageButton1);
imageButton1.setImageResource(imgs[1]);
imageButton1.setOnClickListener(此);
imageButton2=(ImageButton)getActivity().findViewById(R.id.imageButton2);
imageButton2.setOnClickListener(此);
imageButton3=(ImageButton)getActivity().findViewById(R.id.imageButton3);
imageButton3.setOnClickListener(此);
imageButton4=(ImageButton)getActivity().findViewById(R.id.imageButton4);
imageButton4.setOnClickListener(此);
imageSwitcher=(imageSwitcher)getActivity().findViewById(R.id.image\u main\u previewer);
imageSwitcher.setFactory(本);
//将动画添加到预览中
设置动画(AnimationUtils.loadAnimation(getActivity(),R.anim.abc_fade_in));
setOutAnimation(AnimationUtils.loadAnimation(getActivity(),R.anim.abc_fade_out));
//设置默认占位符
设置图像资源(占位符图像);
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
System.out.println(“到达onAct的开始”);
//这里我们需要检查触发的活动是否是图像库。
//如果是,requestCode将与LOAD_IMAGE_RESULTS值匹配。
//如果resultCode是RESULT_OK,并且有一些数据,我们知道已拾取图像。
if(requestCode==LOAD\u IMAGE\u RESULTS&&resultCode==Activity.RESULT\u OK&&data!=null){
//让我们读取拾取的图像数据-其URI
Uri pickedImageUri=data.getData();
//让我们使用内容解析器读取拾取的图像路径
//字符串[]文件路径={MediaStore.Images.Media.DATA};
//Cursor Cursor=getContentResolver().query(pickeImage,filePath,null,null);
//cursor.moveToFirst();
//String imagePath=cursor.getString(cursor.getColumnIndex(filePath[0]);
//现在我们需要使用从拾取的文件读取的数据设置GUI ImageView数据。
//uploadView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
//setNewImage(pickedImageUri);
字符串finalPath=getFinalFilePath(pickedImageUri);
setNewImage(pickedImageUri,finalPath);
//最后请记住关闭光标,否则将以RuntimeException结束!
//cursor.close();
System.out.println(“到达onAct的末尾”);
}
}
私有void setNewImage(Uri、字符串文件路径){
System.out.println(“到达setNewImage的开头”);
setImageURI(uri);
//同时设置拇指指甲的图像
//int buttonId=getThumbButton();
//ImageButton changeButton=(ImageButton)getActivity().findViewById(buttonId);
//setImageBitmap(decodeSampledBitmapFromFile(filePath,40,40));
//最后保存位图
addPathToArray(文件路径);
System.out.println(“到达setNewImage的末尾”);
}
私有void addPathToArray(字符串文件路径){
//位图tempItemImage=BitmapFactory.decodeFile(文件路径);
System.out.println(“输入的addBit”);
添加(文件路径);
System.out.println(“Exit addBit”+photoArray.size());
}
私有整型按钮(){
//获取数组的大小
int arraySize=photoArray.size();
int buttonId=0;
System.out.println(“输入getThumb”);
//切换到查找要更改的按钮
开关(阵列化){
案例0:
buttonId=R.id.imageButton0;
打破
案例1:
buttonId=R.id.imageButton1;
打破
案例2:
buttonId=R.id.imageButton2;
打破
案例3:
buttonId=R.id.imageButton3;
打破
案例4:
buttonId=R.id.imageButton4;
打破
违约:;
}
返回按钮;
}
私有字符串getFinalFilePath(Uri){
字符串[]文件路径={MediaStore.Images.Media.DATA};
Cursor Cursor=getActivity().getContentResolver().query(uri、文件路径、null、null、null);
cursor.moveToFirst();
String imagePath=cursor.getString(cursor.getColumnIndex(filePath[0]);
cursor.close();
//现在我们需要使用从中读取的数据设置GUI ImageView数据