Android广播接收,意图有额外的

时间:2012-01-19 21:41:52

标签: android broadcastreceiver flickr android-asynctask

我正在android中开发一个简单的应用程序,从ficklr下载图像并将其加载到ImageView中。我正在使用asyncTask和广播接收器来避免问题。当我尝试执行它时,我得到一个JSON响应,没有onReceive中的问题。但是,如果我尝试在onReceive中执行另一个加载flickr图像的方法(searchPhoto),我会收到此错误:

01-19 22:24:25.914: E/AndroidRuntime(28284): FATAL EXCEPTION: main
01-19 22:24:25.914: E/AndroidRuntime(28284): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.jalbacar.restapi.SEARCH (has extras) } in com.jalbacar.restapi.SearchActivity$1@46e94708
01-19 22:24:25.914: E/AndroidRuntime(28284):    at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:905)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at android.os.Handler.handleCallback(Handler.java:587)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at android.os.Looper.loop(Looper.java:123)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at android.app.ActivityThread.main(ActivityThread.java:4668)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at java.lang.reflect.Method.invoke(Method.java:521)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at dalvik.system.NativeStart.main(Native Method)
01-19 22:24:25.914: E/AndroidRuntime(28284): Caused by: java.lang.NullPointerException
01-19 22:24:25.914: E/AndroidRuntime(28284):    at    com.jalbacar.restapi.SearchActivity.searchPhoto(SearchActivity.java:89)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at com.jalbacar.restapi.SearchActivity$1.onReceive(SearchActivity.java:110)
01-19 22:24:25.914: E/AndroidRuntime(28284):    at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:892)

SearchActivity

package com.jalbacar.restapi;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.methods.HttpGet;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jalbacar.restapi.RestTask;

public class SearchActivity extends Activity {

private static final String SEARCH_ACTION = "com.jalbacar.restapi.SEARCH";
private static final String SEARCH_URI = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=xxxxxxxxxxxxxxxxxx&photoset_id=72157624057488617&format=json&jsoncallback=?";

private TextView result;
private ProgressDialog progress;
private Bitmap bmFlickr;
private ImageView imageFlickrPhoto;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Activity");
    result = new TextView(this);
    setContentView(result);
    imageFlickrPhoto= (ImageView)findViewById(R.id.flickrPhoto);




  //Create the search request
    try{            

        HttpGet searchRequest = new HttpGet( new URI(SEARCH_URI));

        RestTask task = new RestTask(this,SEARCH_ACTION);
        task.execute(searchRequest);
        //Display progress to the user
        progress = ProgressDialog.show(this, "Searching", "Waiting For Results...", true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onResume() {
    super.onResume();
    registerReceiver(receiver, new IntentFilter(SEARCH_ACTION));
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
}

public void searchPhoto(){
    URL FlickrPhotoUrl;
    try {
        FlickrPhotoUrl = new URL("http://farm5.staticflickr.com/4056/4659741303_4e994ba9aa_s.jpg");


        HttpURLConnection httpConnection = (HttpURLConnection) FlickrPhotoUrl.openConnection();
        httpConnection.setDoInput(true);

        httpConnection.connect();
        InputStream inputStream = httpConnection.getInputStream();
        bmFlickr = BitmapFactory.decodeStream(inputStream);
        if (bmFlickr != null){
               imageFlickrPhoto.setImageBitmap(bmFlickr);
        }
        if(progress != null) {
            progress.dismiss();
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

//bradcast receiver
private BroadcastReceiver  receiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {

        //String response = intent.getStringExtra(RestTask.HTTP_RESPONSE);
        //result.setText(response);
        Toast.makeText(SearchActivity.this, "Galeria2", Toast.LENGTH_LONG).show();
        searchPhoto();
    }
};

最值得注意的是一个简单的问题或非常明显的问题,但我是一名初级Android开发人员。所以,请耐心等待。提前致谢

1 个答案:

答案 0 :(得分:0)

你在第89行得到NPE。所以我认为布局问题。看看onCreate():

result = new TextView(this);
setContentView(result);
imageFlickrPhoto= (ImageView)findViewById(R.id.flickrPhoto);

您将TextView设置为内容但尝试查找ImageView。 所以,你只需要从Xml设置布局或做这样的事情

LinearLayout conatiner = new LinearLayout(this);
container.setOrientation(LinearLayout.VERTICAL);
container.addView(result = new TextView(this));
container.addView(result = new ImageView(this));
setContentView(container);