Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.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.lang.IllegalStateException:方法调用不应在piccaso的主线程中发生_Java_Android_Picasso - Fatal编程技术网

java.lang.IllegalStateException:方法调用不应在piccaso的主线程中发生

java.lang.IllegalStateException:方法调用不应在piccaso的主线程中发生,java,android,picasso,Java,Android,Picasso,我试图在用户登录时创建一个用户对象,并将其保存在共享优先级中。user对象有name和last name,我从json对象中检索到,还有一个位图配置文件pic,我试图用piccaso获取它(图片的线条也在json对象中)。我用截击和异步任务进行了尝试,在这两种情况下我都获得了经验 我的用户模型: public class User{ private String name,lastName; private Bitmap profilePicture; Context

我试图在用户登录时创建一个用户对象,并将其保存在共享优先级中。user对象有name和last name,我从json对象中检索到,还有一个位图配置文件pic,我试图用piccaso获取它(图片的线条也在json对象中)。我用截击和异步任务进行了尝试,在这两种情况下我都获得了经验

我的用户模型:

public class User{

    private String name,lastName;
    private Bitmap profilePicture;
    Context context;

    public User(JSONObject object , Context context){
        this.context = context;

        try {
            this.name = object.getString("firstName");
            this.lastName = object.getString("lastName");
            this.profilePicture = Picasso.with(context).load(object.getString("image")).get();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
还有我的截击请求:

  JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {

                            Intent intent = new Intent(LoginActivityScreen.this, HomeActivityScreen.class);

                            SharedPreferences.Editor editor = preferences.edit();

                            RoomateModel model = new RoomateModel(response, context);


                            Gson gson = new Gson();
                            String json = gson.toJson(model);
                            editor.putString("USER", json);
                            editor.commit();


                            startActivity(intent);


                        }

                    }
JsonObjectRequest-JsonObjectRequest=new-JsonObjectRequest(Request.Method.GET,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
意向意向=新意向(LoginActivityScreen.this、HomeActivityScreen.class);
SharedReferences.Editor=首选项.edit();
RoomateModel=新的RoomateModel(响应、上下文);
Gson Gson=新的Gson();
字符串json=gson.toJson(model);
putString(“用户”,json);
commit();
星触觉(意向);
}
}

基本上它是
NetworkOnMainThreadException
的封装

您需要检查调用
User(…)
构造函数的位置,因为它们有以下行:

this.profilePicture = Picasso.with(context).load(object.getString("image")).get();
它可以有效地进行远程调用,从
object.getString(“image”)
URL检索图片。确保该调用在非UI线程上执行。这是使用毕加索的主要优势,因为它可以确保网络处理和数据缓冲在一个线程内完成,并增加资源(图片)Android内部UI元素(
ImageView
)是在UI线程上完成的,而无需库用户进行任何额外的工作

或者,您可以尝试使用毕加索的
Target
界面:

Picasso.with(context).load(object.getString("image").into(new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        //This gets called when your application has the requested resource in the bitmap object
        User.this.profilePicture = bitmap;
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        //This gets called if the library failed to load the resource
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        //This gets called when the request is started
    }
});

尝试在代码加载图像之前添加此部分

if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy =
                    new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
}

我在一个异步任务中尝试从doInBackground()调用它,但我遇到了相同的异常。@NitronBG检查编辑以查看其他选项。但是我不建议发出异步调用,即使在对象的构造函数中有回调。您可以只存储
字符串
(URL)在
User
对象中创建配置文件图片,然后发出
Piccasso.with(context).load(User.getProfilePicture()).into(imageView)
当您将要在应用程序中显示该用户的图片时。但这样,每次创建homeActivity时,我都需要加载图片。我不知道应用程序的架构和设计,但缓存资源是另一个问题,我们需要更多细节来帮助您。我会尝试解释。我在我想在home activity的onCreate()方法中初始化的侧菜单标题。因此,在用户首次登录时,我想创建一个用户对象并将其保存在SharedReferences中,以便每次创建HomeActivity时,我都从该对象获取图像