2012-01-19 19 views
0

私はficklrから画像をダウンロードしてImageViewに読み込むためにアンドロイドで簡単なアプリを開発しています。私は問題を避けるためにasyncTaskとブロードキャストレシーバを使用しています。私はそれを実行しようとすると、私はonReceiveでJSON応答witout問題を取得します。私はFlickrの画像をロードしonReceive別の方法(searchPhoto)で実行しようとした場合しかし、私はこのエラーを取得する:Androidブロードキャスト受信、インテントにエキストラがあります

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(); 
    } 
}; 

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 [email protected] 
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は、ほとんどの証明可能簡単な問題か、非常に明白なものですが、私はジュニアのAndroid開発者です。だから皆さんは忍耐強くてください。事前に感謝します

答えて

0

You'vは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); 
関連する問題