没有从Thread接收值

时间:2014-11-10 06:02:20

标签: android

线程完成后,我想检索要放在通知中的位图值,但是我在 Log.d处得到NullPointerException(" Bitmap1",bitmap.toString() ); 。为什么位图值为空?

@Override
public void onReceive(final Context context, Intent intent) {
  Iterator itr = json.keys();
    while (itr.hasNext()) {
        String key = (String) itr.next();

        Log.d(TAG, "..."+key+ "=>" +json.getString(key));
        if (key.equals("customdata"))
        {
            msg=json.getString(key);    
            Log.d(TAG,msg.toString());
        }
        if(key.equals("image_url"))
        {
            msg1=json.getString(key);       
            Log.d("msg1",msg1.toString());
        }
        if(key.equals("alert"))
        {
            msg2=json.getString(key);
            Log.d(TAG,msg2.toString());
        }
        if(key.equals("title"))
        {
            msg3=json.getString(key);
            Log.d(TAG,msg3.toString());
        }
    }
    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
        try {
             bitmap=getBitmapFromURL(msg1);
             Log.d("Bitmap",bitmap.toString());

        } catch (Exception e) {
             e.printStackTrace();
        }
        }
    });
    thread.start();

    Log.d("Bitmap1",bitmap.toString());
    Notification noti = new NotificationCompat.Builder(context)
    .setContentTitle(msg3)
    .setContentText(msg2)
    .setContentIntent(pl)
    .setLargeIcon(bitmap)
    .setAutoCancel(true)
    .build();

    NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(50, noti);


    }
    } catch (JSONException e) {
        Log.d(TAG, "JSONException: " + e.getMessage());
    }        

public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}   

1 个答案:

答案 0 :(得分:1)

为获取位图创建自定义界面:

interface CallBackListener{
  public void onCallComplete(Bitmap bitmap);
}

使用界面返回位图:

public Void getBitmapFromURL(String strURL,CallBackListener callBackListener) {
  Bitmap myBitmap;
  try {
       URL url = new URL(strURL);
       HttpURLConnection connection = (HttpURLConnection) url.openConnection();
       connection.setDoInput(true);
       connection.connect();
       InputStream input = connection.getInputStream();
       myBitmap = BitmapFactory.decodeStream(input);
   } catch (IOException e) {
       e.printStackTrace();
   }
   callBackListener.onCallComplete(myBitmap);
}

实施界面:

Thread thread = new Thread(new Runnable(){
   @Override
   public void run() {
      try {
          bitmap=getBitmapFromURL(msg1,new CallBackListener() {
          @Override
          public void onCallComplete(Bitmap bitmap) {
               Log.d("Bitmap1",bitmap.toString());
               Notification noti = new NotificationCompat.Builder(context)
               .setContentTitle(msg3)
               .setContentText(msg2)
               .setContentIntent(pl)
               .setLargeIcon(bitmap)
               .setAutoCancel(true)
               .build();
               NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
               nm.notify(50, noti);
          }
        });
      } catch (Exception e) {
           e.printStackTrace();
      }
  }
});

thread.start();