-3

我的任务是解决我大学的一个小组项目的问题,但是我似乎无法解决我的服务类中的 NullPointerException 问题。我们的目标是创建一个脚本,它会持续监控 android 历史记录,直到找到匹配项 - 如果服务类在浏览器历史记录中找到匹配项,则执行警告类。该问题发生在第 71 行(服务类),但我不知道如何解决该问题。

服务等级:

public class Service_class extends Service {
    String Dirty1 = "www.pornhub.com";
    String Dirty2 = "www.playboy.com";
    String Dirty3 = "www.playboy.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();




        return START_STICKY;
    }

    @Override
    public void onDestroy() {

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

    }

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

        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(Service_class.this, Warning.class);
        Service_class.this.startActivity(intent);
        }
        } while (cursor.moveToNext()); 
     //   tv.setText(urls);
    }

}

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

主.java

import java.util.Calendar;
import com.parse.ParseAnalytics;
import com.parse.ParseObject;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.TrafficStats;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

public class Main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main3);

    // Start service using AlarmManager

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);
    Intent intent = new Intent(Main.this, Service_class.class);
    PendingIntent pintent = PendingIntent.getService(Main.this, 0, intent,
            0);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            36000 * 1000, pintent);

    // click listener for the button to start service
    Button btnStart = (Button) findViewById(R.id.button1);
    btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getBaseContext(), Service_class.class));

        }
    });

    // click listener for the button to stop service
    Button btnStop = (Button) findViewById(R.id.button2);
    btnStop.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(getBaseContext(), Service_class.class));
        }
    });
}}

日志猫:

04-15 13:58:49.980: D/AndroidRuntime(1994): Shutting down VM
04-15 13:58:50.000: W/dalvikvm(1994): threadid=1: thread exiting with uncaught exception (group=0x40cc7930)
04-15 13:58:50.000: E/AndroidRuntime(1994): FATAL EXCEPTION: main
04-15 13:58:50.000: E/AndroidRuntime(1994): java.lang.RuntimeException: Unable to create service com.nfc.linked.Service_class: java.lang.NullPointerException
04-15 13:58:50.000: E/AndroidRuntime(1994):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2539)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at android.app.ActivityThread.access$1600(ActivityThread.java:141)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at android.os.Looper.loop(Looper.java:137)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at java.lang.reflect.Method.invoke(Method.java:511)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at dalvik.system.NativeStart.main(Native Method)
04-15 13:58:50.000: E/AndroidRuntime(1994): Caused by: java.lang.NullPointerException
04-15 13:58:50.000: E/AndroidRuntime(1994):     at com.nfc.linked.Service_class.onCreate(Service_class.java:71)
04-15 13:58:50.000: E/AndroidRuntime(1994):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2529)
04-15 13:58:50.000: E/AndroidRuntime(1994):     ... 10 more
04-15 14:00:23.770: D/AndroidRuntime(2047): Shutting down VM
04-15 14:00:23.770: W/dalvikvm(2047): threadid=1: thread exiting with uncaught exception (group=0x40cc7930)
4

1 回答 1

3

您的方法managedQuery()返回 null

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

所以当你尝试执行一个方法时,null你会得到一个NullPointerException.

Cursor cursor = managedQuery(android.provider.Browser.BOOKMARKS_URI,
            projection, null, null, null); // cursor will be null
String urls = "";
if (cursor.moveToFirst()) { // this will be NPE

让你的managedQuery()方法返回一个实例化的Cursor对象。

于 2013-04-15T18:20:38.663 回答