2011-12-01 144 views
1

我試圖編寫高音揚聲器源,但編譯時出錯。java.lang.RuntimeException:無法啓動活動ComponentInfo

我不會忘記將我的活動添加到清單中。

下面是活動代碼:

package fr.enstb.tp2; 

import java.net.URL; 
import java.util.ArrayList; 

import org.apache.http.client.HttpClient; 
import org.apache.http.client.ResponseHandler; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.BasicResponseHandler; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 

public class TweetsActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ArrayList<Tweet> tweets = getTweets("android", 1); 

     ListView listView = (ListView) findViewById(R.id.ListViewId); 
     listView.setAdapter(new UserItemAdapter(this, R.layout.tweets_layout, tweets)); 
    } 

    public class UserItemAdapter extends ArrayAdapter<Tweet> { 
     private ArrayList<Tweet> tweets; 

     public UserItemAdapter(Context context, int textViewResourceId, ArrayList<Tweet> tweets) { 
      super(context, textViewResourceId, tweets); 
      this.tweets = tweets; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = vi.inflate(R.layout.tweets_layout, null); 
      } 

      Tweet tweet = tweets.get(position); 
      if (tweet != null) { 
       TextView username = (TextView) v.findViewById(R.id.username); 
       TextView message = (TextView) v.findViewById(R.id.message); 
       ImageView image = (ImageView) v.findViewById(R.id.avatar); 

       if (username != null) { 
        username.setText(tweet.username); 
       } 

       if(message != null) { 
        message.setText(tweet.message); 
       } 

       if(image != null) { 
        image.setImageBitmap(getBitmap(tweet.image_url)); 
       } 
      } 
      return v; 
     } 
    } 

    public Bitmap getBitmap(String bitmapUrl) { 
     try { 
      URL url = new URL(bitmapUrl); 
      return BitmapFactory.decodeStream(url.openConnection() .getInputStream()); 
     } 
     catch(Exception ex) {return null;} 
    } 

    public ArrayList<Tweet> getTweets(String searchTerm, int page) { 
     String searchUrl = "http://search.twitter.com/[email protected]" 
          + searchTerm + "&rpp=100&page=" + page; 

     ArrayList<Tweet> tweets = new ArrayList<Tweet>(); 

     HttpClient client = new DefaultHttpClient(); 
     HttpGet get = new HttpGet(searchUrl); 

     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 

     String responseBody = null; 
     try{ 
      responseBody = client.execute(get, responseHandler); 
     }catch(Exception ex) { 
      ex.printStackTrace(); 
     } 

     JSONObject jsonObject = null; 
     JSONParser parser=new JSONParser(); 

     try { 
      Object obj = parser.parse(responseBody); 
      jsonObject=(JSONObject)obj; 

     }catch(Exception ex){ 
      Log.v("TEST","Exception: " + ex.getMessage()); 
     } 

     JSONArray arr = null; 

     try { 
      Object j = jsonObject.get("results"); 
      arr = (JSONArray)j; 
     }catch(Exception ex){ 
      Log.v("TEST","Exception: " + ex.getMessage()); 
     } 

     for(Object t : arr) { 
      Tweet tweet = new Tweet(
        ((JSONObject)t).get("from_user").toString(), 
        ((JSONObject)t).get("text").toString(), 
        ((JSONObject)t).get("profile_image_url").toString() 
        ); 
      tweets.add(tweet); 
     } 

     return tweets; 
    } 

    public class Tweet { 
     public String username; 
     public String message; 
     public String image_url; 

     public Tweet(String username, String message, String url) { 
      this.username = username; 
      this.message = message; 
      this.image_url = url; 
     } 
    } 
} 

這裏是日誌跟蹤:

: E/AndroidRuntime(884): FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.enstb.tp2/fr.enstb.tp2.TweetsActivity}: java.lang.NullPointerException 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) 
    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1796) 
    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135) 
    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347) 
    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682) 
    at android.widget.TabHost.setCurrentTab(TabHost.java:346) 
    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150) 
    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540) 
    at android.view.View.performClick(View.java:3460) 
    at android.view.View$PerformClick.run(View.java:13955) 
    at android.os.Handler.handleCallback(Handler.java:605) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4340) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
    at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
    at fr.enstb.tp2.TweetsActivity.getTweets(TweetsActivity.java:126) 
    at fr.enstb.tp2.TweetsActivity.onCreate(TweetsActivity.java:36) 
    at android.app.Activity.performCreate(Activity.java:4465) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) 
    ... 18 more 

Thankk您的幫助! for(Object t : arr)

步驟通過與調試代碼,看看你的服務調用會得到什麼:

回答

2

根據堆棧跟蹤,你arr在getTweets)變量(必須,因爲你在這一行一個NullPointerException爲空。

+0

實際上在這一行有一個問題: Object j = jsonObject.get(「results」); \t arr =(JSONArray)j; } catch(Exception ex){ 在第一行之後拋出異常。我不明白爲什麼.. 隨着調試器,我也看到變量respondeBody爲空.. 我試圖直接測試我的查詢瀏覽器,它的工作原理! 有沒有其他想法? – wawanopoulos

相關問題