简体   繁体   English

Android中的IllegalArgumentException错误,Context不能为null

[英]IllegalArgumentException error in Android,Context must not be null

I am trying to fetch image from an Url using 'Picasso' but I am getting a message in My Logcat like 我正在尝试使用“ Picasso”从网址中获取图片,但在“我的Logcat”中收到一条消息,例如

Caused by: java.lang.IllegalArgumentException: Context must not be null. 原因:java.lang.IllegalArgumentException:上下文不能为null。

Below is my code and Logcat. 下面是我的代码和Logcat。

09-10 02:53:14.420  29527-    29527/green.example.tejask.asynctasknetworking I/art﹕ Late-enabling -Xcheck:jni
09-10 02:53:14.764  29527-29562/green.example.tejask.asynctasknetworking D/OpenGLRenderer﹕ Render dirty regions requested: true
09-10 02:53:14.788  29527-29527/green.example.tejask.asynctasknetworking D/﹕ HostConnection::get() New Host Connection established 0xabc8c8b0, tid 29527
09-10 02:53:14.799  29527-29527/green.example.tejask.asynctasknetworking D/Atlas﹕ Validating map...
09-10 02:53:14.879  29527-29562/green.example.tejask.asynctasknetworking D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
09-10 02:53:14.881  29527-29562/green.example.tejask.asynctasknetworking D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
09-10 02:53:14.894  29527-29562/green.example.tejask.asynctasknetworking D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
09-10 02:53:14.914  29527-29562/green.example.tejask.asynctasknetworking D/﹕ HostConnection::get() New Host Connection established 0xa3116120, tid 29562
09-10 02:53:14.962  29527-29562/green.example.tejask.asynctasknetworking I/OpenGLRenderer﹕ Initialized EGL, version 1.4
09-10 02:53:15.132  29527-29562/green.example.tejask.asynctasknetworking D/OpenGLRenderer﹕ Enabling debug mode 0
09-10 02:53:15.166  29527-29562/green.example.tejask.asynctasknetworking W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-10 02:53:15.166  29527-29562/green.example.tejask.asynctasknetworking W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3119220, error=EGL_SUCCESS
09-10 02:53:31.853  29527-29527/green.example.tejask.asynctasknetworking D/AndroidRuntime﹕ Shutting down VM
09-10 02:53:31.858  29527-29527/green.example.tejask.asynctasknetworking E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: green.example.tejask.asynctasknetworking, PID: 29527
    java.lang.RuntimeException: Unable to start activity ComponentInfo{green.example.tejask.asynctasknetworking/green.example.tejask.asynctasknetworking.SecondActivity}: java.lang.IllegalArgumentException: Context must not be null.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalArgumentException: Context must not be null.
            at com.squareup.picasso.Picasso$Builder.<init>(Picasso.java:701)
            at com.squareup.picasso.Picasso.with(Picasso.java:662)
            at green.example.tejask.asynctasknetworking.SecondActivity.onCreate(SecondActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
09-10 02:53:39.188  29527-29527/green.example.tejask.asynctasknetworking I/Process﹕ Sending signal. PID: 29527 SIG: 9

This is my code: 这是我的代码:

    package green.example.tejask.asynctasknetworking;
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.ImageView;

    import com.squareup.picasso.Picasso;

/**
 * Created by tejas k on 10-09-2015.
 */
    public class SecondActivity extends AppCompatActivity {
    private ImageView img;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String imgUri = "https://i.imgur.com/tGbaZCY.jpg";
        img= (ImageView) findViewById(R.id.img);
        Picasso.with(context).load(imgUri).into(img);

    }
}

You have not initialize Context in onCreate() method. 您尚未在onCreate()方法中初始化Context please initialize it. 请初始化它。

Try this. 尝试这个。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = SecondActivity.this;
        String imgUri = "https://i.imgur.com/tGbaZCY.jpg";
        img= (ImageView) findViewById(R.id.img);
        Picasso.with(context).load(imgUri).into(img);

    }

The exception says all 例外说明了一切

IllegalArgumentException error in Android,Context must not be null Android中的IllegalArgumentException错误,Context不能为null

in your case you declared a class member of type Context called context without initialising it. 在您的情况下,您声明了Context类型的名为context的类成员,而没有对其进行初始化。 Its default value is null, hence the crash. 它的默认值为null,因此崩溃。 Since Activity inherits from Context, you can use the keyword this . 由于Activity是从Context继承的,因此可以使用关键字this

Eg 例如

Picasso.with(this).load(imgUri).into(img);

Always initialize a variable before using it 始终在使用变量之前对其进行初始化

just put Context context=this; 只需将Context context = this;

Initialize you context as : 将上下文初始化为:

context=SecondActivity.this;

before : 之前:

Picasso.with(context).load(imgUri).into(img);

Either you should initialize your context. 您都应该初始化上下文。 public class SecondActivity extends AppCompatActivity { private ImageView img; 公共类SecondActivity扩展了AppCompatActivity {private ImageView img; Context context; 上下文上下文;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String imgUri = "https://i.imgur.com/tGbaZCY.jpg";
    img= (ImageView) findViewById(R.id.img);
    context = SecondActivity.this;
    Picasso.with(context).load(imgUri).into(img);

}

} }

or you dont need to declare the context. 或者你不需要申报的上下文。

Picasso.with(this).load(imgUri).into(img); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalArgumentException:上下文不得为null - java.lang.IllegalArgumentException: Context must not be null android 上的 Google Drive 错误:java.lang.IllegalArgumentException:名称不能为空:null - Google Drive on android error: java.lang.IllegalArgumentException: the name must not be empty: null Picasso IllegalArgumentException 目标不能是 null - Picasso IllegalArgumentException Target must not be null 上下文不能为null - Context must not be null java.lang.IllegalArgumentException:Android中的AppKey不能为null或为空错误 - java.lang.IllegalArgumentException: AppKey cannot be null or empty error in android java.lang.IllegalArgumentException:TwitterSession不能为null - java.lang.IllegalArgumentException: TwitterSession must not be null java.lang.IllegalArgumentException:目标不能为空 - java.lang.IllegalArgumentException: Target must not be null java.lang.IllegalArgumentException:视图不能为null; 在Android中实现UIL displayImage函数时 - java.lang.IllegalArgumentException: view must not be null; when implementing UIL displayImage function in Android java.lang.RuntimeException:无法启动接收器 xx.xx.ImediateSMSReceiver:java.lang.IllegalArgumentException:上下文不能为空 - java.lang.RuntimeException: Unable to start receiver xx.xx.ImediateSMSReceiver: java.lang.IllegalArgumentException: Context must not be null java.lang.IllegalArgumentException:名称不能为空:null - 一部测试手机出错但另一部没有 - java.lang.IllegalArgumentException: the name must not be empty: null - error on one test phone but not the other
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM