使用Volley加载图片时出错

时间:2015-05-26 08:36:20

标签: java android android-volley

我以编程方式创建了ImageView,然后我必须从URL添加图像。我使用Volley方法,但它抛出 NullPointerException 并关闭应用程序,当我尝试使用 drawable 图像时,它的工作正常,我给了Logcat以下。请帮助。

import...
    public class imageopen extends Activity {


        LinearLayout layout;
        LinearLayout.LayoutParams lparams;
        Intent intent = getIntent();
        String easy = "1";


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.imageopen);

            layout =new LinearLayout(this);
            lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            lparams.setMargins(0, 10, 0, 10);
            lparams.gravity=Gravity.CENTER;

            layout.setOrientation(LinearLayout.VERTICAL);
            layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));

            final ImageView imView = new ImageView(this);
            imView.setLayoutParams(lparams);
            imView.getLayoutParams().height = 600;
            imView.getLayoutParams().width = 600;

            ImageLoader imageLoader = AppController.getInstance().getImageLoader();


            imageLoader.get("http://sreeharsh.web44.net/images/image1.jpg", new ImageLoader.ImageListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("tttttttt", "Image Load Error: " + error.getMessage());
                }

                @Override
                public void onResponse(ImageLoader.ImageContainer response, boolean arg1) {
                    if (response.getBitmap() != null) {
                        // load image into imageview
                        imView.setImageBitmap(response.getBitmap());
                    }
                }
            });

            layout.addView(imView);


            setContentView(layout);


        }
    }

logcat的:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sreeharsh.saqavu/com.radix.saqavu.imageopen}: java.lang.NullPointerException
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
                at android.app.ActivityThread.access$600(ActivityThread.java:156)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:153)
                at android.app.ActivityThread.main(ActivityThread.java:5297)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                at dalvik.system.NativeStart.main(Native Method)
         Caused by: java.lang.NullPointerException
                at com.radix.saqavu.imageopen.onCreate(imageopen.java:51)
                at android.app.Activity.performCreate(Activity.java:5122)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
                at android.app.ActivityThread.access$600(ActivityThread.java:156)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:153)
                at android.app.ActivityThread.main(ActivityThread.java:5297)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:511)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案