内容解析器停止运行

iszxjhcz  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(205)

我为我的项目做了一个内容提供者,也为内容解析器做了另一个项目。
但突然间,内容解析器应用程序停止运行。
这是内容提供商应用程序:

package com.example.connectly.database;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import static android.content.Context.MODE_PRIVATE;

public class SeekersContactsProvider extends ContentProvider {

    SQLiteDatabase seekerSQLiteDatabase;
    private static final String AUTHORITY = "com.example.connectly";
    private static final String BASE_PATH = "seekers";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);

    private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int SEEKER = 0;
//    private static final String SEEKER_EMAIL = "email";
    private static final int SEEKER_EMAIL = 1;

    private static final String SEEKER_TABLE_NAME = "seeker";

    Cursor cursor;
    public static final String[] ALL_COLUMNS =
            {"email", "password", "firstname", "lastname","phone","age","dob","gender"};

    static {
        uriMatcher.addURI(AUTHORITY, BASE_PATH, SEEKER);
//        uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", EMAIL);
        uriMatcher.addURI(AUTHORITY, BASE_PATH + "/" + SEEKER_TABLE_NAME, SEEKER_EMAIL);
    }

    @Override
    public boolean onCreate() {

        seekerSQLiteDatabase = getContext().openOrCreateDatabase("Users", MODE_PRIVATE, null);
        seekerSQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS seeker (email VARCHAR, password VARCHAR, firstname VARCHAR, lastname VARCHAR, phone VARCHAR, age VARCHAR, dob VARCHAR, gender VARCHAR)");

        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {

        switch (uriMatcher.match(uri)) {

            case SEEKER:

                cursor = seekerSQLiteDatabase.query(SEEKER_TABLE_NAME, ALL_COLUMNS,selection
                        , null, null, null, "firstname" + " ASC");

                break;
            default:
                throw new IllegalArgumentException("This is an Unknown URI " + uri);
        }

        cursor.setNotificationUri(getContext().getContentResolver(), uri);

        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {

        switch (uriMatcher.match(uri)) {
            case SEEKER:
                return "com.example.connectly/seeker";
            default:
                throw new IllegalArgumentException("This is an Unknown URI " + uri);
        }
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {

        long id = seekerSQLiteDatabase.insert(SEEKER_TABLE_NAME, null, contentValues);

        if (id > 0) {
            Uri _uri = ContentUris.withAppendedId(CONTENT_URI, id);
            getContext().getContentResolver().notifyChange(_uri, null);

            return _uri;
        }
        throw new SQLException("Insertion Failed for URI :" + uri);
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        int delCount = 0;
        switch (uriMatcher.match(uri)) {
            case SEEKER:
                delCount = seekerSQLiteDatabase.delete(SEEKER_TABLE_NAME, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("This is an Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return delCount;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        int updCount = 0;
        switch (uriMatcher.match(uri)) {

            case SEEKER:
                updCount = seekerSQLiteDatabase.update(SEEKER_TABLE_NAME, values, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("This is an Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return updCount;
    }
}

这是内容解析器代码,但当我运行应用程序并单击“加载数据”按钮时,它突然停止:
p

ackage com.example.App2ContentProvider;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button loadBtn;
    Uri CONTENT_URI = Uri.parse("content://com.example.connectly/seekers");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadBtn = findViewById(R.id.load);
        loadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // inserting complete table details in this text field
                TextView resultView = (TextView) findViewById(R.id.textView);

                // creating a cursor object of the
                // content URI
                Cursor cursor = getContentResolver().query(Uri.parse("content://com.example.connectly/seekers"), null, null, null, null);

                // iteration of the cursor
                // to print whole table
                if (cursor.moveToFirst()) {
                    StringBuilder strBuild = new StringBuilder();
                    while (!cursor.isAfterLast()) {
                        strBuild.append("\n" + cursor.getString(
                                cursor.getColumnIndex("firstname")) + "-" + cursor.getString(cursor.getColumnIndex("lastname")) + cursor.getString(cursor.getColumnIndex("email")) + cursor.getString(cursor.getColumnIndex("phone")) +
                                cursor.getString(cursor.getColumnIndex("age")) + cursor.getString(cursor.getColumnIndex("dob")) +
                                cursor.getString(cursor.getColumnIndex("gender")) );
                        cursor.moveToNext();
                    }
                    resultView.setText(strBuild);
                } else {
                    resultView.setText("No Records Found");
                }
            }

        });

    }
}

这是显示的错误:

FATAL EXCEPTION: main
    Process: com.example.contentprovider_p2, PID: 9084
    java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
        at com.example.App2ContentProvider.MainActivity$1.onClick(MainActivity.java:35)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题