服务类中的游标是空的 - 但是在独立活动类中返回值

时间:2013-04-15 21:49:16

标签: java android service nullpointerexception alarmmanager

我的同学和我一直在处理我们正在构建的Android应用程序的问题。

我们已成功构建了一个Android应用,其中包含一项活动,可搜索我们的浏览器历史记录/书签以查找特定网址,并在找到匹配作为成人内容警告实施的示例时启动另一项活动(成功)。

这个功能正常。

现在我们正在实现一个服务类,每隔几秒就会持续执行一次 - 为了使用我们的新服务类执行我们最近历史的搜索 - 但是我们遇到了一些问题。

我们的问题是当我们尝试将Nanny.java中的“Nanny”源代码添加到服务类中时,“Nanny”脚本将连续搜索历史记录,而不是仅仅一次 - “Nanny”应用程序不断强制关闭 - 由于光标初始化时出现空指针异常:if (cursor.moveToFirst()) {

这有点奇怪,因为在我们拥有的测试文件(Main.java)中的服务类之外执行完全相同的脚本时,不会发生NullPointerException

我们工作的Main.java和我们的工作服务类[每隔几秒成功显示一次祝酒]以及我们[失败]两者的组合 - 如下所示:

空服务类:

public class Service_class extends Service {


    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

}

工作Nanny.java

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.widget.TextView;

public class Nanny extends Activity {

    String Dirty1 = "www.playboy.com";
    String Dirty2 = "www.penthouse.com";
    String Dirty3 = "www.pornhub.com";
    String Dirty4 = "www.playboy.com";
    String Dirty5 = "www.playboy.com";
    String Dirty6 = "www.playboy.com";
    String Dirty7 = "www.playboy.com";
    String Dirty8 = "www.playboy.com";
    String Dirty9 = "www.playboy.com";
    String Dirty10 = "www.playboy.com";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nanny);
TextView tv = (TextView) findViewById(R.id.hello);
String[] projection = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
Cursor cursor = managedQuery(android.provider.Browser.BOOKMARKS_URI,
        projection, null, null, null);
String urls = "";
if (cursor.moveToFirst()) {
String url1 = null;
String url2 = null;
do {
String url = cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.URL));

if (url.toLowerCase().contains(Dirty1)) {
} else if (url.toLowerCase().contains(Dirty2)) {
} else if (url.toLowerCase().contains(Dirty3)) {
} else if (url.toLowerCase().contains(Dirty4)) {
} else if (url.toLowerCase().contains(Dirty5)) {
} else if (url.toLowerCase().contains(Dirty6)) {
} else if (url.toLowerCase().contains(Dirty7)) {
} else if (url.toLowerCase().contains(Dirty8)) {
} else if (url.toLowerCase().contains(Dirty9)) {
} else if (url.toLowerCase().contains(Dirty10)) {
//if (url.toLowerCase().contains(Filthy)) {
urls = urls
+ cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.TITLE)) + " : "
+ url + "\n";
Intent intent = new Intent(Nanny.this, Warning.class);
Nanny.this.startActivity(intent);
}
} while (cursor.moveToNext()); 
tv.setText(urls);
}}}

我们试图实现的不成功方法(将有效的保姆脚本添加到服务类中)导致NullPointerException:if(cursor.moveToFirst()){

public class Service_class extends Service {
    String Dirty1 = "www.playboy.com";
    String Dirty2 = "www.penthouse.com";
    String Dirty3 = "www.pornhub.com";
    String Dirty4 = "www.playboy.com";
    String Dirty5 = "www.playboy.com";
    String Dirty6 = "www.playboy.com";
    String Dirty7 = "www.playboy.com";
    String Dirty8 = "www.playboy.com";
    String Dirty9 = "www.playboy.com";
    String Dirty10 = "www.playboy.com";

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        //setContentView(R.layout.main3);
      //  TextView tv = (TextView) findViewById(R.id.hello);
        String[] projection = new String[] { Browser.BookmarkColumns.TITLE,
        Browser.BookmarkColumns.URL };
        Cursor  cursor = managedQuery(android.provider.Browser.BOOKMARKS_URI,
                projection, null, null, null);
        String urls = "";
        if (cursor.moveToFirst()) {
        String url1 = null;
        String url2 = null;
        do {
        String url = cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.URL));

        if (url.toLowerCase().contains(Dirty1)) {
        } else if (url.toLowerCase().contains(Dirty2)) {
        } else if (url.toLowerCase().contains(Dirty3)) {
        } else if (url.toLowerCase().contains(Dirty4)) {
        } else if (url.toLowerCase().contains(Dirty5)) {
        } else if (url.toLowerCase().contains(Dirty6)) {
        } else if (url.toLowerCase().contains(Dirty7)) {
        } else if (url.toLowerCase().contains(Dirty8)) {
        } else if (url.toLowerCase().contains(Dirty9)) {
        } else if (url.toLowerCase().contains(Dirty10)) {
        urls = urls
        + cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.TITLE)) + " : "
        + url + "\n";
        Intent warning_intent = new Intent(Service_class.this, Warning.class);
        Service_class.this.startActivity(intent);
        }
        } while (cursor.moveToNext()); 
      //  tv.setText(urls);




        return START_STICKY;

        }
        return startId;}
    @Override
    public void onDestroy() {

        super.onDestroy();
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
    private void setContentView(int main3) {
        // TODO Auto-generated method stub

    }

    private TextView findViewById(int hello) {
        // TODO Auto-generated method stub
        return null;
    }

    private Cursor managedQuery(Uri bookmarksUri, String[] projection,
            Object object, Object object2, Object object3) {
        // TODO Auto-generated method stub
        return null;
    }}

LOGCAT(当使用上面显示的组合实现时)

第53行失败:if(cursor.moveToFirst()){

04-15 18:26:24.761: D/dalvikvm(7466): Late-enabling CheckJNI
04-15 18:26:25.651: D/dalvikvm(7466): GC_FOR_ALLOC freed 82K, 4% free 7379K/7608K, paused 13ms, total 13ms
04-15 18:26:25.651: I/dalvikvm-heap(7466): Grow heap (frag case) to 10.861MB for 3686416-byte allocation
04-15 18:26:25.661: D/dalvikvm(7466): GC_FOR_ALLOC freed 1K, 3% free 10977K/11212K, paused 13ms, total 13ms
04-15 18:26:25.681: D/dalvikvm(7466): GC_CONCURRENT freed <1K, 3% free 10977K/11212K, paused 3ms+2ms, total 17ms
04-15 18:26:25.901: D/dalvikvm(7466): GC_FOR_ALLOC freed <1K, 3% free 10977K/11212K, paused 11ms, total 11ms
04-15 18:26:25.911: I/dalvikvm-heap(7466): Grow heap (frag case) to 17.086MB for 6529744-byte allocation
04-15 18:26:25.931: D/dalvikvm(7466): GC_FOR_ALLOC freed 0K, 2% free 17354K/17592K, paused 13ms, total 13ms
04-15 18:26:25.941: D/dalvikvm(7466): GC_CONCURRENT freed <1K, 2% free 17354K/17592K, paused 3ms+2ms, total 16ms
04-15 18:26:26.051: D/libEGL(7466): loaded /system/lib/egl/libEGL_tegra.so
04-15 18:26:26.061: D/libEGL(7466): loaded /system/lib/egl/libGLESv1_CM_tegra.so
04-15 18:26:26.071: D/libEGL(7466): loaded /system/lib/egl/libGLESv2_tegra.so
04-15 18:26:26.091: D/OpenGLRenderer(7466): Enabling debug mode 0
04-15 18:26:31.181: D/AndroidRuntime(7466): Shutting down VM
04-15 18:26:31.181: W/dalvikvm(7466): threadid=1: thread exiting with uncaught exception (group=0x40f4f930)
04-15 18:26:31.181: E/AndroidRuntime(7466): FATAL EXCEPTION: main
04-15 18:26:31.181: E/AndroidRuntime(7466): java.lang.RuntimeException: Unable to start service com.ut.appdemo.Service_class@41698e90 with Intent { cmp=com.ut.appdemo/.Service_class }: java.lang.NullPointerException
04-15 18:26:31.181: E/AndroidRuntime(7466):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2673)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at android.app.ActivityThread.access$1900(ActivityThread.java:141)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at android.os.Looper.loop(Looper.java:137)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at java.lang.reflect.Method.invoke(Method.java:511)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at dalvik.system.NativeStart.main(Native Method)
04-15 18:26:31.181: E/AndroidRuntime(7466): Caused by: java.lang.NullPointerException
04-15 18:26:31.181: E/AndroidRuntime(7466):     at com.ut.appdemo.Service_class.onStartCommand(Service_class.java:53)
04-15 18:26:31.181: E/AndroidRuntime(7466):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2656)
04-15 18:26:31.181: E/AndroidRuntime(7466):     ... 10 more

1 个答案:

答案 0 :(得分:0)

您正在将光标设置为null ...

使用此方法实例化游标:

Cursor  cursor = managedQuery(android.provider.Browser.BOOKMARKS_URI,projection, null, null, null);

然后你定义这样的managedQuery()方法:

private Cursor managedQuery(Uri bookmarksUri, String[] projection, Object object, Object object2, Object object3) {
    // TODO Auto-generated method stub
    return null;
}}

基本上代码所说的是:

Cursor cursor = null;
curser.doSomething();

这将始终导致nullPointer。我强烈建议你在深入研究android之前回去学习java基础知识,如果你试图在android中完成某些东西并且缺乏对java的理解,你只是要求头痛。