Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.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
Java 无法将类强制转换异常按钮强制转换为Adview_Java_Android_Android Layout - Fatal编程技术网

Java 无法将类强制转换异常按钮强制转换为Adview

Java 无法将类强制转换异常按钮强制转换为Adview,java,android,android-layout,Java,Android,Android Layout,我试图在AdsView下放置一个按钮-download_btn,但在执行活动时 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" android:layout_widt

我试图在AdsView下放置一个按钮-download_btn,但在执行活动时

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/selected_imageview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/gallery_relative_layout"
            android:layout_marginLeft="30dip"
            android:layout_marginRight="30dip"
            android:layout_marginTop="30dip" />

        <RelativeLayout
            android:id="@+id/gallery_relative_layout"
            android:layout_width="fill_parent"
            android:layout_height="150dip"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/left_arrow_imageview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="15dip"
                android:src="@drawable/arrow_left_disabled" />

            <Gallery
                android:id="@+id/gallery"
                android:layout_width="0dip"
                android:layout_height="150dip"
                android:layout_marginLeft="20dip"
                android:layout_marginRight="20dip"
                android:layout_toLeftOf="@+id/right_arrow_imageview"
                android:layout_toRightOf="@+id/left_arrow_imageview"
                android:spacing="20dip" />

            <ImageView
                android:id="@+id/right_arrow_imageview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="15dip"
                android:src="@drawable/arrow_right_enabled" />

            <Button
                android:id="@+id/set_btn"
                android:layout_width="wrap_content"
                android:layout_height="35dip"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/right_arrow_imageview"
                android:onClick="onButtonClicked"
                android:text="Set as Wallpaper" />

            <com.google.ads.AdView
                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                ads:adSize="BANNER"
                ads:adUnitId="XXXXXXXXXXXX"
                ads:loadAdOnCreate="true" />

            <Button
                android:id="@+id/download_btn"
                android:layout_width="wrap_content"
                android:layout_height="35dip"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/adView"
                android:onClick="onDownloadButtonClicked"
                android:text="Load More Wallpaper" />
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>

编辑:以下是代码的活动:

public class GalleryDemoActivity extends Activity {

    public static final String COUNTURL = "http://wallpaper.hukumnama.in/sikhwallpaper.aspx";
    public static final String IMAGEURL = "XXXXXXXXXXXXX";
    public static final String DIRECTORYNAME = "XXXXXXXXXXXXXXXX";
    private ImageView selectedImageView;
    public static int networkCount =0;
    public static int dbCount =0;
    private ImageView leftArrowImageView;

    private ImageView rightArrowImageView;

    private Gallery gallery;
    Bitmap selectedImage =null;
    private int selectedImagePosition = 0;

    private List<Bitmap> drawables;
    MyDB db = null;
    private GalleryImageAdapter galImageAdapter;
    NetworkAccess networkAccess = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);          
        getDrawablesList();
        setupUI();
    }


    private void setupUI() {

        try{
        selectedImageView = (ImageView) findViewById(R.id.selected_imageview);
        leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);
        rightArrowImageView = (ImageView) findViewById(R.id.right_arrow_imageview);
        gallery = (Gallery) findViewById(R.id.gallery);

        leftArrowImageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (selectedImagePosition > 0) {
                    --selectedImagePosition;
                }

                gallery.setSelection(selectedImagePosition, false);
            }
        });

        rightArrowImageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (selectedImagePosition < drawables.size() - 1) {
                    ++selectedImagePosition;

                }

                gallery.setSelection(selectedImagePosition, false);

            }
        });

        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

                selectedImagePosition = pos;

                if (selectedImagePosition > 0 && selectedImagePosition < drawables.size() - 1) {

                    leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                    rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));

                } else if (selectedImagePosition == 0) {

                    leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));

                } else if (selectedImagePosition == drawables.size() - 1) {

                    rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
                }

                changeBorderForSelectedImage(selectedImagePosition);
                setSelectedImage(selectedImagePosition);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }

        });

        galImageAdapter = new GalleryImageAdapter(this, drawables);

        gallery.setAdapter(galImageAdapter);

        if (drawables.size() > 0) {

            gallery.setSelection(selectedImagePosition, false);

        }

        if (drawables.size() == 1) {

            rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
        }

        }catch(Exception e){
            System.out.println("Exception in setting up UI"+e);
        }
        AdView adView = (AdView)this.findViewById(R.id.adView);
        AdRequest adrequest = new AdRequest();
        adView.loadAd(adrequest);
    }

    private void changeBorderForSelectedImage(int selectedItemPos) {

        int count = gallery.getChildCount();

        for (int i = 0; i < count; i++) {

            ImageView imageView = (ImageView) gallery.getChildAt(i);
            imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_border));
            imageView.setPadding(3, 3, 3, 3);

        }

        ImageView imageView = (ImageView) gallery.getSelectedView();
        imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.selected_image_border));
        imageView.setPadding(3, 3, 3, 3);
    }

    private void getDrawablesList() {

        try{
        drawables = new ArrayList<Bitmap>();
        /* String ExternalStorageDirectoryPath = Environment
         .getExternalStoragePublicDirectory(Environment.getExternalStorageDirectory() + DIRECTORYNAME)
         .getAbsolutePath();
       */

       String ExternalStorageDirectoryPath =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+DIRECTORYNAME;
       String targetPath = ExternalStorageDirectoryPath ;

       Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
       File targetDirector = new File(targetPath);

       File[] files = targetDirector.listFiles();
       files = sortFiles(files, targetPath+"/");
       if(files!=null)
       {
       for (File file : files){
           System.out.println(file.getName());
           Bitmap bm = decodeSampledBitmapFromUri(file.getAbsolutePath(), 520, 520);
           drawables.add(bm); 

       }    
       }

       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image1)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image2)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image3)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image4)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image5)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image6)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image7)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image8)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image9)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image10)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image11)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image12)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image13)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image14)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image15)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image16)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image17)).getBitmap());
       drawables.add(((BitmapDrawable)getResources().getDrawable(R.drawable.image18)).getBitmap());
        }catch(Exception e){System.out.println("Exception in drawable list creation"+e);}
    }

     public 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) {
              if (width > height) {
               inSampleSize = Math.round((float)height / (float)reqHeight);   
              } else {
               inSampleSize = Math.round((float)width / (float)reqWidth);   
              }   
             }

             return inSampleSize;   
            }
      public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
             Bitmap bm = null;

             // First decode with inJustDecodeBounds=true to check dimensions
             final BitmapFactory.Options options = new BitmapFactory.Options();
             options.inJustDecodeBounds = true;
             BitmapFactory.decodeFile(path, options);

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

             // Decode bitmap with inSampleSize set
             options.inJustDecodeBounds = false;
             bm = BitmapFactory.decodeFile(path, options); 

             return bm;  
            }
    private void setSelectedImage(int selectedImagePosition) {


        selectedImage = drawables.get(selectedImagePosition);
        selectedImageView.setImageBitmap(selectedImage);
        selectedImageView.setScaleType(ScaleType.FIT_XY);

    }
    public void onButtonClicked(View view)
    {
        switch(view.getId())
        {
        case R.id.set_btn:
            System.out.println("On button clicked....Wallpaper");
             WallpaperManager wm = WallpaperManager.getInstance(this);
                try {
                    System.out.println("Hieght"+wm.getDesiredMinimumHeight());
                    System.out.println("width"+wm.getDesiredMinimumWidth());
                    wm.setBitmap(selectedImage);

                    Toast.makeText(this, "Wallpaper Set SuccessFully", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    System.out.println( "Cannot set image as wallpaper"+e);
                }

        }       
    }

    private File[] sortFiles(File[] inputFiles,String directoryName){

     ArrayList<Integer> fileNames = new ArrayList<Integer>();
     File[] sortedArray = new File[inputFiles.length];
     for(File f : inputFiles){
         if(f !=null){
         fileNames.add( Integer.parseInt(f.getName().replace(".jpg", "")));
         }
     }
        Collections.sort(fileNames);
        Collections.reverse(fileNames);
        for(int i=0; i<fileNames.size();i++){
            sortedArray[i] = new File (directoryName+fileNames.get(i)+".jpg");
        }
        return sortedArray;
    } 
公共类GalleryDemoActivity扩展活动{
公共静态最终字符串COUNTURL=”http://wallpaper.hukumnama.in/sikhwallpaper.aspx";
公共静态最终字符串IMAGEURL=“xxxxxxxxxxxx”;
公共静态最终字符串DIRECTORYNAME=“XXXXXXXXXXXXXX”;
私有图像视图选择图像视图;
公共静态int networkCount=0;
公共静态int dbCount=0;
私有图像视图左箭头图像视图;
private ImageView右箭头ImageView;
私人画廊;
位图selectedImage=null;
private int selectedImagePosition=0;
私人名单提款;
MyDB=null;
私人厨房图像适配器;
NetworkAccess NetworkAccess=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getDrawablesList();
setupUI();
}
私有void setupUI(){
试一试{
selectedImageView=(ImageView)findViewById(R.id.selected\u ImageView);
leftArrowImageView=(ImageView)findViewById(R.id.LeftArrow\u ImageView);
rightArrowImageView=(ImageView)findViewById(R.id.right\u arrow\u ImageView);
画廊=(画廊)findViewById(R.id.gallery);
leftArrowImageView.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(选择图像位置>0){
--选择图像位置;
}
gallery.setSelection(selectedImagePosition,false);
}
});
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(选择图像位置0&&selectedImagePosition0){
gallery.setSelection(selectedImagePosition,false);
}
if(drawables.size()==1){
rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
}
}捕获(例外e){
System.out.println(“设置UI时出现异常”+e);
}
AdView AdView=(AdView)this.findviewbyd(R.id.AdView);
AdRequest AdRequest=新AdRequest();
adView.loadAd(adrequest);
}
SelecteImage的私有无效更改边界(int-selectedItemPos){
int count=gallery.getChildCount();
for(int i=0;i   <LinearLayout android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:id="@+id/LLadView">
        <com.google.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="XXXXXXXXXXXX"
            ads:loadAdOnCreate="true" />
   </LinearLayout>
        <Button
            android:id="@+id/download_btn"
            android:layout_width="wrap_content"
            android:layout_height="35dip"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/LLadView"
            android:onClick="onDownloadButtonClicked"
            android:text="Load More Wallpaper" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/selected_imageview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/gallery_relative_layout"
        android:layout_marginLeft="30dip"
        android:layout_marginRight="30dip"
        android:layout_marginTop="30dip" />

    <RelativeLayout
        android:id="@+id/gallery_relative_layout"
        android:layout_width="fill_parent"
        android:layout_height="150dip"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/left_arrow_imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dip"
            android:src="@drawable/arrow_left_disabled" />

        <Gallery
            android:id="@+id/gallery"
            android:layout_width="0dip"
            android:layout_height="150dip"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_toLeftOf="@+id/right_arrow_imageview"
            android:layout_toRightOf="@+id/left_arrow_imageview"
            android:spacing="20dip" />

        <ImageView
            android:id="@+id/right_arrow_imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dip"
            android:src="@drawable/arrow_right_enabled" />

        <Button
            android:id="@+id/set_btn"
            android:layout_width="wrap_content"
            android:layout_height="35dip"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/right_arrow_imageview"
            android:onClick="onButtonClicked"
            android:text="Set as Wallpaper" />

            </Relative Layout>
            <LinearLayout> 
        <com.google.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="XXXXXXXXXXXX"
            ads:loadAdOnCreate="true" />
         </LinearLayout>

         <RelativeLayout
        <Button
            android:id="@+id/download_btn"
            android:layout_width="wrap_content"
            android:layout_height="35dip"
            android:onClick="onDownloadButtonClicked"
            android:text="Load More Wallpaper" />
    </RelativeLayout>
</RelativeLayout>

</LinearLayout>