NullPointerException imageView.setImageBitmap(bitmap)

时间:2014-05-08 08:18:32

标签: android image nullpointerexception downloading

我试图从网站下载图片。这是我的代码: MainActivity

...
public class MainActivity extends Activity {
public final static String URL = "com.example.test3.URL";
ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

// execute by pressing button
public void send(View view){
    EditText enter = (EditText) findViewById(R.id.enter);
    String url = enter.getText().toString();
    ImageView imageView = (ImageView) findViewById(R.id.image1);
    new ImageDownload(imageView).execute(url);      
}

}

ImageDownload

public class ImageDownload extends AsyncTask<String, Void, Bitmap>{
ImageView imageView = null;
public ImageDownload(ImageView imageView){
    this.imageView = imageView;
}

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new    PlaceholderFragment()).commit();
    }
    Intent intent = getIntent();
    String urlString = intent.getStringExtra(MainActivity.URL); */


@Override
protected Bitmap doInBackground(String... url) {
    if(url.length <= 0){        
        return null;
    }
    return downloadBitmap(url[0]);
}

public Bitmap downloadBitmap(String s) {
    String urlString = s;
    URL url;
    InputStream i;
    Bitmap bitmap = null;
    try { 
        url = new URL(urlString);       
        i = url.openStream();
        bitmap = BitmapFactory.decodeStream(i);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e){
        e.printStackTrace();
    }

    return bitmap;
}

public void onPostExecute(Bitmap bitmap){
    Log.d("done", "onPostExecute");
    if(bitmap != null){
        this.imageView.setImageBitmap(bitmap);
    }
}   
}

程序停止并显示以下日志:

05-08 09:52:04.477: D/done(8534): onPostExecute
05-08 09:52:04.477: D/AndroidRuntime(8534): Shutting down VM
05-08 09:52:04.477: W/dalvikvm(8534): threadid=1: thread exiting with uncaught exception (group=0x415e7d88)
05-08 09:52:04.497: E/AndroidRuntime(8534): FATAL EXCEPTION: main
05-08 09:52:04.497: E/AndroidRuntime(8534): Process: com.example.test3, PID: 8534
05-08 09:52:04.497: E/AndroidRuntime(8534): java.lang.NullPointerException
05-08 09:52:04.497: E/AndroidRuntime(8534):     at  com.example.test3.ImageDownload.onPostExecute(ImageDownload.java:90)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at com.example.test3.ImageDownload.onPostExecute(ImageDownload.java:1)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at android.os.AsyncTask.finish(AsyncTask.java:632)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at  android.os.Looper.loop(Looper.java:212)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at android.app.ActivityThread.main(ActivityThread.java:5151)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at java.lang.reflect.Method.invokeNative(Native Method)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at java.lang.reflect.Method.invoke(Method.java:515)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
05-08 09:52:04.497: E/AndroidRuntime(8534):     at dalvik.system.NativeStart.main(Native Method)
05-08 09:52:05.707: I/Process(8534): Sending signal. PID: 8534 SIG: 9

fragment_image_download.xml:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test3.ImageDownload$PlaceholderFragment" >

<ImageView
  android:contentDescription="@string/desc"
  android:id="@+id/image1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
</ImageView>

</RelativeLayout>

这个NullPointerException来自哪里?有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:1)

错误来自ImageDownload的第90行:

at  com.example.test3.ImageDownload.onPostExecute(ImageDownload.java:90)

修改

片段的布局image1中不存在

fragment_main而不是活动的布局activity_main,这就是NullPointerException的原因。

EditText enter置于片段布局中或更改image1访问逻辑。

答案 1 :(得分:1)

此处的问题是您正在错误地处理片段中的Viewssend函数是Activity类的成员,而不是Fragment类,但ImageView已在Fragment内声明,因此您应该提供它{39} ; Activity调用之前的根视图(不等于findViewById的根视图)。像这样:

ImageView imageView = getFragmentManager().findFragmentById(...).getView().findViewById(R.id.image1);

但最好的解决方案是将所有与片段相关的逻辑移到Fragment类中。