08-23 10:58:51.210: ERROR/AndroidRuntime(8158): FATAL EXCEPTION: main
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
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2173)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at android.app.ActivityThread.access$2800(ActivityThread.java:123)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at android.os.Looper.loop(Looper.java:123)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at android.app.ActivityThread.main(ActivityThread.java:3839)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at java.lang.reflect.Method.invoke(Method.java:507)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at dalvik.system.NativeStart.main(Native Method)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): Caused by: java.lang.NullPointerException
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at android.app.IntentService.onStart(IntentService.java:110)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at android.app.IntentService.onStartCommand(IntentService.java:118)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2160)
08-23 10:58:51.210: ERROR/AndroidRuntime(8158): ... 10 more
package com.pec.testapp;
import java.util.ArrayList;
import com.pec.testapp.service.NewsService;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
public class PECNewsActivity extends Activity implements ListAdapter {
ArrayList<PECNewsArticle> articles = new ArrayList<PECNewsArticle>();
Context context = null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
updateNews();
context = getApplicationContext();
if(context == null){
Log.d("PECAPP","Context is null");
}
setContentView(R.layout.news_layout);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(PECNewsActivity.this, NewsService.class);
//Bundle b = new Bundle();
//b.putInt(NewsService.TASK_KEY, NewsService.UPDATE_NEWS);
//i.putExtra(NewsService.TASK, b);
if(i != null)
context.startService(i);
else
Log.d("PECAPP", "Intent was null... not starting service!");
}
});
Button bb = (Button) findViewById(R.id.button2);
bb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
updateNews();
}
});
}
protected void updateNews() {
Cursor c =
managedQuery(PECNews.Article.CONTENT_URI, null, null, null, null);
if(c == null){
Log.d("PECAPP", "Cursor returned false... there is a problem...");
return;
}
int id = c.getColumnIndexOrThrow(PECNews.Article._ID);
int title = c.getColumnIndexOrThrow(PECNews.Article.TITLE);
int content = c.getColumnIndexOrThrow(PECNews.Article.CONTENT);
int author = c.getColumnIndexOrThrow(PECNews.Article.AUTHOR);
int date = c.getColumnIndexOrThrow(PECNews.Article.DATE);
articles.clear();
c.moveToFirst();
while(c.moveToNext()){
articles.add(
new PECNewsArticle.Builder(c.getString(title))
.setId(c.getInt(id))
.setContent(c.getString(content))
.setAuthor(c.getString(author))
.setDate(c.getString(date))
.build()
);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Update");
return false;
}
@Override
public int getCount() {
return articles.size();
}
@Override
public PECNewsArticle getItem(int position) {
return articles.get(position);
}
@Override
public long getItemId(int position) {
return articles.get(position).id;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return false;
}
}
package com.pec.testapp.service;
import com.pec.json.NewsArticleElement;
import com.pec.testapp.providers.PECNewsProvider;
import com.pec.testapp.PECNews;
import android.app.IntentService;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import com.pec.helpers.NewsFetcher;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class NewsService extends IntentService {
public final static String TASK = "newsservice.task";
public final static String TASK_KEY = "newsservice.task.key";
public final static int UPDATE_NEWS = 1;
public NewsService() {
super("NewsService");
}
@Override
public void onCreate(){
}
@Override
public void onDestroy(){
}
@Override
protected void onHandleIntent(Intent intent) throws IllegalArgumentException {
Log.d("PECAPP", "GOT HERE");
if(intent == null)
Log.d("PECAPP", "INTENT IS NULL!");
Bundle b = intent.getBundleExtra(TASK);
if(b == null){
Log.d("PECAPP", "TASK Bundle is missing.");
return;
}
int task = b.getInt(TASK_KEY);
switch(task) {
case UPDATE_NEWS:
initiateUpdate();
break;
default:
throw new IllegalArgumentException("Illegal Argument: "+task);
}
}
public void initiateUpdate() {
new NewsUpdater().run();
}
private final class NewsUpdater implements Runnable {
@Override
public void run() {
NewsFetcher n = new NewsFetcher();
NewsArticleElement[] news = n.getArticles();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
for(NewsArticleElement a : news) {
values.put(PECNews.Article._ID, a.id);
values.put(PECNews.Article.TITLE, a.title);
values.put(PECNews.Article.CONTENT, a.content);
values.put(PECNews.Article.DATE, a.date);
values.put(PECNews.Article.AUTHOR, a.author);
cr.insert(Uri.parse(PECNewsProvider.AUTHORITY), values);
values.clear();
}
}
}
}