Android:在AsyncTask中将tweet对象添加到我的模型时出现NullPointerException

时间:2014-07-14 15:10:18

标签: android arraylist android-asynctask nullpointerexception

当我尝试将我的推文对象添加到我的模型中的arraylist时,我遇到了这个问题,但是我得到了一个N​​ullPointerException。这发生在我的SearchTweetsTask类中。 只有当我用twitter api搜索推文时才会发生这种情况。获取时间表和内容非常有效,没有任何问题。但只有当我搜索推文时,我才得到这个例外。 我通过我的操作栏中的搜索视图搜索推文,该操作栏是在我的MainActivity中创建的。 我希望你能帮助我。

以下是一些代码:

    protected String doInBackground(String... params)
    {
        meldingen = "";
        String result = "";
        String searchString = params[0];
        String decoded = "";
        try {
            decoded = URLEncoder.encode(searchString, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            Log.d("URL ENCODER", "De Search query is niet goed ingelezen");
        }
        String url = "https://api.twitter.com/1.1/search/tweets.json?q=";
        String searchURL = url + decoded + "&count=50";

        try{
            HttpGet get = new HttpGet(searchURL);
            Authenticate.authentication(get);
            ResponseHandler<String> handler = new BasicResponseHandler();
            result = httpClient.execute(get, handler);
            JSONObject json = new JSONObject(result);
            JSONArray jArray = json.getJSONArray("statuses");
            for (int i = 0; i < jArray.length(); i++)
            {
                JSONObject subObject = jArray.getJSONObject(i);
                Tweet tweet = new Tweet (subObject);
                publishProgress(tweet);
            }
        }
        catch(HttpResponseException e)
        {
           //all the catch exceptions
           ....
        }
    //GETTING NULLER POINTER HERE
    protected void onProgressUpdate(Tweet... tweets) {
        for (Tweet tweet : tweets) {
            model.addSearchTweets(tweet);
        }
    }

模特课:

   private ArrayList<Tweet> searchTweets = new ArrayList<Tweet>();

   public void addSearchTweets(Tweet tweet)
   {
       searchTweets.add(tweet);
   }

MainActivity:

   public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.actions, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) searchItem.getActionView();

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if(null != searchManager ) 
    {   
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String search) {
                model.clearSearchTweets();

                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);

                Fragment newFragment = new UserSearchTweetsFragment();
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.replace(R.id.frame_container, newFragment);
                transaction.addToBackStack(null);

                SearchTweetsTask searchTweets = new SearchTweetsTask(MainActivity.this);
                searchTweets.execute(search);

                transaction.commit();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String search) {
                return true;
            }
        });
    }
    searchView.setIconifiedByDefault(false);
    return true;

07-14 17:18:25.048:E / AndroidRuntime(9135):致命异常:主要 07-14 17:18:25.048:E / AndroidRuntime(9135):处理:nl.timkatgert.tweettweet,PID:9135 07-14 17:18:25.048:E / AndroidRuntime(9135):java.lang.NullPointerException 07-14 17:18:25.048:E / AndroidRuntime(9135):at nl.timkatgert.tweettweet.tasks.SearchTweetsTask.onProgressUpdate(SearchTweetsTask.java:117) 07-14 17:18:25.048:E / AndroidRuntime(9135):at nl.timkatgert.tweettweet.tasks.SearchTweetsTask.onProgressUpdate(SearchTweetsTask.java:1) 07-14 17:18:25.048:E / AndroidRuntime(9135):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:648) 07-14 17:18:25.048:E / AndroidRuntime(9135):在android.os.Handler.dispatchMessage(Handler.java:102) 07-14 17:18:25.048:E / AndroidRuntime(9135):在android.os.Looper.loop(Looper.java:157) 07-14 17:18:25.048:E / AndroidRuntime(9135):在android.app.ActivityThread.main(ActivityThread.java:5356) 07-14 17:18:25.048:E / AndroidRuntime(9135):at java.lang.reflect.Method.invokeNative(Native Method) 07-14 17:18:25.048:E / AndroidRuntime(9135):at java.lang.reflect.Method.invoke(Method.java:515) 07-14 17:18:25.048:E / AndroidRuntime(9135):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1265) 07-14 17:18:25.048:E / AndroidRuntime(9135):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 07-14 17:18:25.048:E / AndroidRuntime(9135):at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案