-1

我和我的同学都被我们正在构建的 android 应用程序所困扰。

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

这运行良好。

现在我们正在实现一个服务类,以每隔几秒钟连续执行一次——以便使用我们的新服务类执行对我们最近历史的搜索——但我们遇到了一些问题。

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

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

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

空服务类:

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();
    }

}

工作保姆.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);
}}}

我们尝试实现的不成功方法(将正常运行的 nanny 脚本添加到服务类)导致 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
4

1 回答 1

0

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

您使用此方法来实例化游标:

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 缺乏了解的事情,你只会感到头疼。

于 2013-04-16T01:50:24.403 回答