Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 23rd, 2011  |  syntax: None  |  size: 8.44 KB  |  views: 101  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158): FATAL EXCEPTION: main
  2. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158): java.lang.RuntimeException: Unable to start service com.pec.testapp.service.NewsService@406bd940 with Intent { cmp=com.pec.testapp/.service.NewsService }: java.lang.NullPointerException
  3. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2173)
  4. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at android.app.ActivityThread.access$2800(ActivityThread.java:123)
  5. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
  6. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at android.os.Handler.dispatchMessage(Handler.java:99)
  7. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at android.os.Looper.loop(Looper.java:123)
  8. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at android.app.ActivityThread.main(ActivityThread.java:3839)
  9. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at java.lang.reflect.Method.invokeNative(Native Method)
  10. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at java.lang.reflect.Method.invoke(Method.java:507)
  11. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
  12. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  13. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at dalvik.system.NativeStart.main(Native Method)
  14. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158): Caused by: java.lang.NullPointerException
  15. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at android.app.IntentService.onStart(IntentService.java:110)
  16. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at android.app.IntentService.onStartCommand(IntentService.java:118)
  17. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2160)
  18. 08-23 10:58:51.210: ERROR/AndroidRuntime(8158):     ... 10 more
  19.  
  20. package com.pec.testapp;
  21.  
  22. import java.util.ArrayList;
  23.  
  24. import com.pec.testapp.service.NewsService;
  25.  
  26. import android.app.Activity;
  27.  
  28. import android.content.Context;
  29. import android.content.Intent;
  30. import android.database.Cursor;
  31. import android.database.DataSetObserver;
  32.  
  33. import android.os.Bundle;
  34.  
  35. import android.util.Log;
  36.  
  37. import android.view.Menu;
  38. import android.view.View;
  39.  
  40. import android.view.View.OnClickListener;
  41. import android.view.ViewGroup;
  42.  
  43. import android.widget.Button;
  44. import android.widget.ListAdapter;
  45.  
  46. public class PECNewsActivity extends Activity implements ListAdapter {
  47.  
  48.   ArrayList<PECNewsArticle> articles = new ArrayList<PECNewsArticle>();
  49.   Context context = null;
  50.  
  51.   @Override
  52.     public void onCreate(Bundle savedInstanceState){
  53.       super.onCreate(savedInstanceState);
  54.  
  55.       updateNews();
  56.       context = getApplicationContext();
  57.       if(context == null){
  58.         Log.d("PECAPP","Context is null");
  59.       }
  60.  
  61.       setContentView(R.layout.news_layout);
  62.       Button b = (Button) findViewById(R.id.button1);
  63.       b.setOnClickListener(new OnClickListener() {
  64.         @Override
  65.         public void onClick(View arg0) {
  66.           Intent i = new Intent(PECNewsActivity.this, NewsService.class);
  67.           //Bundle b = new Bundle();
  68.           //b.putInt(NewsService.TASK_KEY, NewsService.UPDATE_NEWS);
  69.           //i.putExtra(NewsService.TASK, b);
  70.           if(i != null)
  71.             context.startService(i);
  72.           else
  73.             Log.d("PECAPP", "Intent was null... not starting service!");
  74.         }
  75.       });
  76.  
  77.       Button bb = (Button) findViewById(R.id.button2);
  78.       bb.setOnClickListener(new OnClickListener() {
  79.         @Override
  80.         public void onClick(View view) {
  81.           updateNews();
  82.         }
  83.       });
  84.  
  85.     }
  86.  
  87.   protected void updateNews() {
  88.     Cursor c =
  89.       managedQuery(PECNews.Article.CONTENT_URI, null, null, null, null);
  90.  
  91.     if(c == null){
  92.       Log.d("PECAPP", "Cursor returned false... there is a problem...");
  93.       return;
  94.     }
  95.     int id = c.getColumnIndexOrThrow(PECNews.Article._ID);
  96.     int title = c.getColumnIndexOrThrow(PECNews.Article.TITLE);
  97.     int content = c.getColumnIndexOrThrow(PECNews.Article.CONTENT);
  98.     int author = c.getColumnIndexOrThrow(PECNews.Article.AUTHOR);
  99.     int date = c.getColumnIndexOrThrow(PECNews.Article.DATE);
  100.  
  101.     articles.clear();
  102.  
  103.     c.moveToFirst();
  104.     while(c.moveToNext()){
  105.       articles.add(
  106.           new PECNewsArticle.Builder(c.getString(title))
  107.           .setId(c.getInt(id))
  108.           .setContent(c.getString(content))
  109.           .setAuthor(c.getString(author))
  110.           .setDate(c.getString(date))
  111.           .build()
  112.           );
  113.     }
  114.   }
  115.  
  116.   @Override
  117.     public boolean onCreateOptionsMenu(Menu menu) {
  118.       menu.add("Update");
  119.       return false;
  120.     }
  121.  
  122.   @Override
  123.     public int getCount() {
  124.       return articles.size();
  125.     }
  126.  
  127.   @Override
  128.     public PECNewsArticle getItem(int position) {
  129.       return articles.get(position);
  130.     }
  131.  
  132.   @Override
  133.     public long getItemId(int position) {
  134.       return articles.get(position).id;
  135.     }
  136.  
  137.   @Override
  138.     public int getItemViewType(int position) {
  139.       return 0;
  140.     }
  141.  
  142.   @Override
  143.     public View getView(int position, View convertView, ViewGroup parent) {
  144.       // TODO Auto-generated method stub
  145.       return null;
  146.     }
  147.  
  148.   @Override
  149.     public int getViewTypeCount() {
  150.       // TODO Auto-generated method stub
  151.       return 0;
  152.     }
  153.  
  154.   @Override
  155.     public boolean hasStableIds() {
  156.       // TODO Auto-generated method stub
  157.       return false;
  158.     }
  159.  
  160.   @Override
  161.     public boolean isEmpty() {
  162.       // TODO Auto-generated method stub
  163.       return false;
  164.     }
  165.  
  166.   @Override
  167.     public void registerDataSetObserver(DataSetObserver observer) {
  168.       // TODO Auto-generated method stub
  169.  
  170.     }
  171.  
  172.   @Override
  173.     public void unregisterDataSetObserver(DataSetObserver observer) {
  174.       // TODO Auto-generated method stub
  175.  
  176.     }
  177.  
  178.   @Override
  179.     public boolean areAllItemsEnabled() {
  180.       // TODO Auto-generated method stub
  181.       return false;
  182.     }
  183.  
  184.   @Override
  185.     public boolean isEnabled(int position) {
  186.       // TODO Auto-generated method stub
  187.       return false;
  188.     }
  189. }
  190.  
  191.  
  192. package com.pec.testapp.service;
  193.  
  194. import com.pec.json.NewsArticleElement;
  195.  
  196. import com.pec.testapp.providers.PECNewsProvider;
  197.  
  198. import com.pec.testapp.PECNews;
  199.  
  200. import android.app.IntentService;
  201. import android.content.ContentResolver;
  202. import android.content.ContentValues;
  203. import android.content.Intent;
  204.  
  205. import com.pec.helpers.NewsFetcher;
  206.  
  207. import android.net.Uri;
  208.  
  209. import android.os.Bundle;
  210.  
  211. import android.util.Log;
  212.  
  213. public class NewsService extends IntentService {
  214.  
  215.   public final static String TASK = "newsservice.task";
  216.   public final static String TASK_KEY = "newsservice.task.key";
  217.   public final static int UPDATE_NEWS = 1;
  218.  
  219.   public NewsService() {
  220.     super("NewsService");
  221.   }
  222.  
  223.   @Override
  224.     public void onCreate(){
  225.  
  226.     }
  227.  
  228.   @Override
  229.     public void onDestroy(){
  230.  
  231.     }
  232.  
  233.   @Override
  234.     protected void onHandleIntent(Intent intent) throws IllegalArgumentException {
  235.       Log.d("PECAPP", "GOT HERE");
  236.       if(intent == null)
  237.         Log.d("PECAPP", "INTENT IS NULL!");
  238.  
  239.       Bundle b = intent.getBundleExtra(TASK);
  240.       if(b == null){
  241.         Log.d("PECAPP", "TASK Bundle is missing.");
  242.         return;
  243.       }
  244.       int task = b.getInt(TASK_KEY);
  245.  
  246.       switch(task) {
  247.         case UPDATE_NEWS:
  248.           initiateUpdate();
  249.           break;
  250.         default:
  251.           throw new IllegalArgumentException("Illegal Argument: "+task);
  252.       }
  253.     }
  254.  
  255.   public void initiateUpdate() {
  256.     new NewsUpdater().run();
  257.   }
  258.  
  259.   private final class NewsUpdater implements Runnable {
  260.     @Override
  261.       public void run() {
  262.         NewsFetcher n = new NewsFetcher();
  263.         NewsArticleElement[] news = n.getArticles();
  264.         ContentResolver cr = getContentResolver();
  265.         ContentValues values = new ContentValues();
  266.         for(NewsArticleElement a : news) {
  267.           values.put(PECNews.Article._ID, a.id);
  268.           values.put(PECNews.Article.TITLE, a.title);
  269.           values.put(PECNews.Article.CONTENT, a.content);
  270.           values.put(PECNews.Article.DATE, a.date);
  271.           values.put(PECNews.Article.AUTHOR, a.author);
  272.           cr.insert(Uri.parse(PECNewsProvider.AUTHORITY), values);
  273.           values.clear();
  274.         }
  275.       }
  276.   }
  277. }
clone this paste RAW Paste Data