当我尝试下载图像时,我的应用程序崩溃了

时间:2017-02-04 14:25:47

标签: android nullpointerexception

我目前正在使用Rob Percival的教程学习android。我通过了图像下载部分,但是当我启动应用程序时,按下“下载”按钮时崩溃了。

我的mainactivity java文件:

public class MainActivity extends AppCompatActivity {
    ImageView img;
    public void DownloadTask(View view)
    {
        ImageDownloader dlTask = new ImageDownloader();
        Bitmap myImg;
        try
        {
            myImg = dlTask.execute("https://www.android.com/static/img/android.png").get();
            img.setImageBitmap(myImg);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        catch (ExecutionException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        img = (ImageView) findViewById(R.id.imageView);


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public class ImageDownloader extends AsyncTask<String, Void, Bitmap>
    {

        @Override
        protected Bitmap doInBackground(String... urls) {
            try
            {
                URL url = new URL(urls[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                InputStream inputstream = connection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputstream);
                return bitmap;
            }
            catch (MalformedURLException e1)
            {
                e1.printStackTrace();
            }
            catch (IOException e1)
            {
                e1.printStackTrace();
            }

            return null;
        }
    }
}

我还给了INTERNET许可:

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

日志:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.swit.imagedownload, PID: 4410
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                      at android.view.View.performClick(View.java:4780)
                      at android.view.View$PerformClick.run(View.java:19866)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      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:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                      at android.view.View.performClick(View.java:4780) 
                      at android.view.View$PerformClick.run(View.java:19866) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      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:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
                      at com.swit.imagedownload.MainActivity.DownloadTask(MainActivity.java:25)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:4780) 
                      at android.view.View$PerformClick.run(View.java:19866) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      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:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
I/Process: Sending signal. PID: 4410 SIG: 9
Application terminated.

2 个答案:

答案 0 :(得分:1)

这就是你解决问题的方法, 您应该setContentView()之前findViewById()

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img = (ImageView) findViewById(R.id.imageView);

答案 1 :(得分:0)

由于ImageView img;

,您正在获得NPE

您在 setContentView(R.layout.activity_main);

之前将其初始化为