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



我正在尝试使用'Picasso'从Url获取图像,但我在我的Logcat中收到一条消息,如

由:java.lang.IllegalArgumentException引起:Context不能为空。

下面是我的代码和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

这是我的代码:

    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);
    }
}

未在onCreate()方法中初始化Context。请初始化。

试试这个。

@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);
    }

所有

Android的IllegalArgumentException错误,Context不能为null

在您的情况下,您声明了Context类型的类成员context,而没有初始化它。它的默认值为空,因此崩溃。因为Activity继承自Context,所以你可以使用关键字this

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

始终在使用变量之前初始化它

将上下文初始化为:

context=SecondActivity.this;

before:

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

应该初始化上下文。SecondActivity扩展appcompactivityprivate ImageView;上下文语境;

@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);
}

}

或者你不需要声明上下文

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

相关内容

  • 没有找到相关文章

最新更新