从适配器调用插页式广告时出现Android java.lang.SecurityException错误

时间:2018-04-11 12:28:45

标签: java android android-studio

基本上,我们有一个卡片视图列表,其中包含图片,名称和两个下载按钮。我想要的是在点击2次后,应为用户显示插页式广告。代码对我来说效果很好(我正在使用模拟器),但我意识到它给了我很多用户的错误(通常使用android 6.0,7.0及以上版本)

以下是崩溃代码:

  

java.lang.SecurityException in   com.getlist.MyAdapter.InterstitialInsideshow

java.lang.SecurityException: 

  at android.os.Parcel.readException (Parcel.java:1620)

  at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:183)

  at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:135)

  at android.content.ContentProviderProxy.insert (ContentProviderProxy.java:476)

  at android.content.ContentResolver.insert (ContentResolver.java:1231)

  at android.app.DownloadManager.enqueue (DownloadManager.java:946)

  at com.getlist.MyAdapter.InterstitialInsideshow (MyAdapter.java)
  or                     .access$000 (MyAdapter.java)
  or                     .access$200 (MyAdapter.java)
  or                     .download (MyAdapter.java)
  or                     .onBindViewHolder (MyAdapter.java)
  or                     .onCreateViewHolder (MyAdapter.java)

  at com.getlist.MyAdapter.InterstitialInsideshow (MyAdapter.java)
  or                     .access$000 (MyAdapter.java)
  or                     .access$200 (MyAdapter.java)
  or                     .download (MyAdapter.java)
  or                     .onBindViewHolder (MyAdapter.java)
  or                     .onCreateViewHolder (MyAdapter.java)

  at com.m3u.getlists.MyAdapter$2.onClick (MyAdapter.java)

  at android.view.View.performClick (View.java:5204)

  at android.view.View$PerformClick.run (View.java:21153)

  at android.os.Handler.handleCallback (Handler.java:739)

  at android.os.Handler.dispatchMessage (Handler.java:95)

  at android.os.Looper.loop (Looper.java:148)

  at android.app.ActivityThread.main (ActivityThread.java:5480)

  at java.lang.reflect.Method.invoke (Method.java)

  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)

  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)
  

MyAdapter.java

package com.getlist;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.squareup.picasso.Picasso;

import java.util.List;

import static android.content.Context.DOWNLOAD_SERVICE;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<list> mylist;
    private Context context;
    private InterstitialAd interstitialinside;

    public MyAdapter(List<com.getlist.list> list, Context context) {
        mylist = list;
        this.context = context;
    }

    public void InterstitialInsideshow()
    {       AdRequest adRequestI = new AdRequest.Builder().build();
            interstitialinside = new InterstitialAd(context);
            interstitialinside.setAdUnitId("ca-app-pub-xxxx/xxxxx"); // I've hide it
            interstitialinside.loadAd(adRequestI);
            interstitialinside.setAdListener(new AdListener() {
                public void onAdLoaded() {  
                    if (interstitialinside.isLoaded()) {
                        interstitialinside.show();
                    }
                  }  
            });
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.mylist,null);
        return new ViewHolder(v);
    }

    private void download(String link, Context context){
        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(link));

        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
        final String filename= URLUtil.guessFileName(link, null, null);
        String file_extension = filename.replace("bin","zip");
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, file_extension);
        DownloadManager dm = (DownloadManager)context.getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
        Toast.makeText(context, "Downloading File", //To notify the Client that the file is being downloaded
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final list list = mylist.get(position);

        // Set Name
        holder.txtname.setText(list.getList_name());
        // Set Image
        Picasso.with(context).load(list.getList_image()).into(holder.img);
        // Set Links
        if(!(list.getList_link1().startsWith("http"))){
            holder.link1.setVisibility(View.INVISIBLE);
        }else{
            holder.link1.setBackgroundColor(holder.link1.getContext().getResources().getColor(R.color.color_nephritis));
            holder.link1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if ((recycleview.x >= 1)) {
                        InterstitialInsideshow();
                        recycleview.x = 0; // Recycleview.x is just a static integer called from another class
                    }else{
                    recycleview.x+=1;
                    }
                    download(list.getList_link1(),context);
                }
            });
        }

        if(!(list.getList_link2().startsWith("http"))){
            holder.link2.setVisibility(View.INVISIBLE);
        }else {
            holder.link2.setVisibility(View.VISIBLE);
            holder.link2.setBackgroundColor(holder.link2.getContext().getResources().getColor(R.color.color_nephritis));
            holder.link2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if ((recycleview.x >= 1)) {
                        InterstitialInsideshow();
                        recycleview.x = 0;
                    }else{
                        recycleview.x+=1;
                    }
                    download(list.getList_link2(),context);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mylist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView img;
        public TextView txtname;
        public Button link1,link2;

        public ViewHolder(View itemView) {
            super(itemView);

            img=(ImageView)itemView.findViewById(R.id.img);
            txtname=(TextView)itemView.findViewById(R.id.txtname);
            link1=(Button)itemView.findViewById(R.id.btnlink1);
            link2=(Button)itemView.findViewById(R.id.btnlink2);
        }
    }
}
  

list.java

    public class list extends AppCompatActivity {

    private String list_name;
    private String list_image;
    private String list_link1;
    private String list_link2;

   public list(String name,String image,String link1,String link2){
       list_name=name;
       list_image=image;
       list_link1=link1;
       list_link2=link2;
    }

    public String getList_name() {
       return Data_Decryption(list_name);
    }

    public String getList_image() {
        return Data_Decryption (list_image);
    }

    public String getList_link1() {
        return Data_Decryption (list_link1);
    }

    public String getList_link2() {
        return Data_Decryption( list_link2);
    }
}
  

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.getlist">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:node="remove"/>

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:xlargeScreens="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name="com.getlist.first_java" />
        <activity android:name="com.getlist.splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.getlist.privacy_java" />
        <activity android:name="com.getlist.help_java" />
        <activity android:name="com.getlist.recycleview" />
        <activity android:name="com.getlist.webview_java"/>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:1)

请勿使用AppCompatActivity扩展列表。

也在你的适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private List<list> mylist;
private Context context;
private InterstitialAd interstitialinside;

public MyAdapter(List<com.getlist.list> list, Context context) {
    mylist = list;
    this.context = context;
   }
 }

mylist的类型为List<list>,但在构造函数中为List<com.getlist.list> list

使两者相同

您也在下载适配器中的项目,但没有检查WRITE_EXTERNAL_STORAGE权限,请求WRITE_EXTERNAL_STORAGE的运行时权限

答案 1 :(得分:1)

该应用必须申请相应的权限。您通过在应用清单中列出权限,然后请求用户在运行时批准每个权限(在Android 6.0及更高版本上)来声明您的应用需要权限。

使用以下代码检查权限

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR)
    != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted

}

有关更多详细信息,请查看有关运行时权限的android开发人员文档

https://developer.android.com/training/permissions/requesting.html#java