代码之家  ›  专栏  ›  技术社区  ›  Abd EL Magied

尝试在空对象[重复]上调用虚方法

  •  0
  • Abd EL Magied  · 技术社区  · 7 年前

    这是我的日志

    致命异常:main 过程:com.example.abdelmagied。myapplication,PID:16160 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void com.example.abdelmagied.myapplication.recyclerview.swapcursor(android.database.Cursor)” 在com.example.abdelmagied.myapplication.MainActivity.onLoadFinished(MainActivity.java:17) 在android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:444) 在android.support.v4.content.Loader.deliverResult(Loader.java:126) 在android.support.v4.content.cursorload.deliverResult(cursorload.java:105) 在android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:80) 在android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:485) 在android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:502) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 位于java.lang.reflect.Method。调用(本机方法) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

    package com.example.abdelmagied.myapplication;
    
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.net.Uri;
    import android.support.v4.app.LoaderManager;
    import android.support.v4.content.CursorLoader;
    import android.support.v4.content.Loader;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
    
        public static final String[] MAIN_STUDENT_PROJECTION = {
    
                studentContract.StudentEntry._ID ,
    
                studentContract.StudentEntry.COLUMN_NAME ,
    
                studentContract.StudentEntry.COLUMN_DEGREE ,
    
        };
    
        public static final int INDEX_COLUMN_ID      = 0;
        public static final int INDEX_COLUMN_NAME    = 1;
        public static final int INDEX_COLUMN_DEGREE  = 2;
    
    
        private recyclerview recyclerAdapter;
        private int mposition = RecyclerView.NO_POSITION;
    
        public static final int ID_STUDNET_LOADER = 99;
    
        // details of the recyclerview
    
         RecyclerView recycler;
         RecyclerView.Adapter myadapter;
         RecyclerView.LayoutManager mymanager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
    
            // adjust the recyclerview
            recycler  = (RecyclerView) findViewById(R.id.recyclerid);
            myadapter = new recyclerview(this);
            mymanager = new LinearLayoutManager(this);
    
            recycler.setAdapter(myadapter);
            recycler.setLayoutManager(mymanager);
    
            getSupportLoaderManager().initLoader(ID_STUDNET_LOADER , null , this);
        }
    
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            switch(id)
            {
                case ID_STUDNET_LOADER :
    
                    Uri  Query    = studentContract.StudentEntry.CONTENT_URI;
    
    
                    return new CursorLoader(this  ,
                                  Query ,
                                  MAIN_STUDENT_PROJECTION ,
                                  null ,
                                  null  ,
                                  null
                               );
    
                 default:
                      throw new RuntimeException("Loader is not implemented : " + id);
            }
        }
    
        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    
            recyclerAdapter.swapcursor(data);
            if(mposition == RecyclerView.NO_POSITION) mposition = 0;
            recycler.smoothScrollToPosition(mposition);
        }
    
        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
               recyclerAdapter.swapcursor(null);
        }
    
        public void addtodatabase(View view) {
    
            EditText name = (EditText) findViewById(R.id.name);
            EditText degree=(EditText) findViewById(R.id.degree);
    
            // make a contentvalues and put name  , degree , salary on  it...
            ContentValues values = new ContentValues();
            values.put(studentContract.StudentEntry.COLUMN_NAME   , name.getText().toString());
            values.put(studentContract.StudentEntry.COLUMN_DEGREE , degree.getText().toString());
            getContentResolver().insert(studentContract.StudentEntry.CONTENT_URI , values);
        }
    }
    

    这是recyclerview适配器

    package com.example.abdelmagied.myapplication;
    
    import android.content.Context;
    import android.database.Cursor;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    /**
     * Created by AbdELMagied on 8/4/2017.
     */
    
    public class recyclerview extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
    
        Context context;
    
        private Cursor mycursor;
        public recyclerview(Context context) {
            this.context = context;
        }
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
            View view =   LayoutInflater.from(context).inflate(R.layout.recyclerrow , parent , false);
            return new viewholder(view);
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    
             mycursor.move(position);
             viewholder myholder = (viewholder) holder;
            myholder.mytxt.setText(mycursor.getString(MainActivity.INDEX_COLUMN_NAME));
    
        }
    
        void swapcursor(Cursor mycurso)
        {
            this.mycursor = mycurso;
            notifyDataSetChanged();
        }
    
        @Override
        public int getItemCount() {
            if(mycursor == null) return 0;
            return mycursor.getCount();
        }
    
        public class viewholder extends RecyclerView.ViewHolder
        {
            public  TextView mytxt;
            public viewholder(View itemView) {
                super(itemView);
                mytxt = (TextView) itemView.findViewById(R.id.textView);
            }
        }
    }
    

    这是我的内容提供商

    package com.example.abdelmagied.myapplication;
    
    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 android.support.annotation.Nullable;
    import android.text.TextUtils;
    
    /**
     * Created by AbdELMagied on 8/4/2017.
     */
    
    public class studentprovider extends ContentProvider {
    
        databaseopenhelper mydatabase;
    
        static final int STUDENT = 100;
    
        static final int STUDENT_WITH_ID = 101;
    
        private UriMatcher urimatcher = matcher();
    
        private UriMatcher matcher() {
    
            UriMatcher mymatcher = new UriMatcher(UriMatcher.NO_MATCH);
            mymatcher.addURI(studentContract.authority, "student", STUDENT);
            mymatcher.addURI(studentContract.authority, "student/#", STUDENT_WITH_ID);
            return mymatcher;
    
        }
    
        @Override
        public boolean onCreate() {
            mydatabase = new databaseopenhelper(getContext());
            return true;
        }
    
    
        @Nullable
        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            Cursor returncursor;
            switch (urimatcher.match(uri)) {
    
                case STUDENT:
    
                    returncursor = mydatabase.getReadableDatabase().query(
                            studentContract.StudentEntry.TABLE_NAME,
                            projection,
                            selection,
                            selectionArgs,
                            null,
                            null,
                            sortOrder
                    );
                    break;
    
                case STUDENT_WITH_ID:
    
                    String where = "_id = " + uri.getLastPathSegment() + (!TextUtils.isEmpty(selection)? " AND " +  selection : "");
    
                    returncursor = mydatabase.getReadableDatabase().query(
    
                            studentContract.StudentEntry.TABLE_NAME,
                            projection,
                            where,
                            selectionArgs,
                            null,
                            null,
                            sortOrder
    
                    );
                    break;
    
                default:
                    throw new UnsupportedOperationException("not found  uri " + uri);
            }
    
            returncursor.setNotificationUri(getContext().getContentResolver() , uri);
            return returncursor;
        }
    
    
        @Nullable
        @Override
        public String getType(Uri uri) {
            return null;
        }
    
        @Nullable
        @Override
        public Uri insert(Uri uri, ContentValues values) {
             long rowid =  mydatabase.getWritableDatabase().insert(studentContract.StudentEntry.TABLE_NAME, null , values);
              if(rowid > 0)
              {
                  Uri _uri = ContentUris.withAppendedId(studentContract.StudentEntry.CONTENT_URI, rowid);
                  getContext().getContentResolver().notifyChange(_uri, null);
                  return _uri;
              }
    
            throw new SQLException("Faild to add a record " + uri);
        }
    
        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            if(selection == "") selection= "1";
            int numberofdeleted = 0;
            switch (urimatcher.match(uri))
            {
                case STUDENT:
                     String where = "_id = " + uri.getLastPathSegment()  + (!TextUtils.isEmpty(selection)? "AND " + selection : "");
                     numberofdeleted = mydatabase.getWritableDatabase().delete("students" , where , selectionArgs);
                    break;
                default:
                    throw new UnsupportedOperationException("uri not found  " + uri);
            }
               if(numberofdeleted != 0)
               {
                   getContext().getContentResolver().notifyChange(uri , null);
               }
            return numberofdeleted;
        }
    
    
        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            return 0;
        }
    }
    

    package com.example.abdelmagied.myapplication;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    /**
     * Created by AbdELMagied on 8/4/2017.
     */
    
    public class databaseopenhelper extends SQLiteOpenHelper {
    
    
        public databaseopenhelper(Context context) {
    
            super(context, "student.db", null, 1);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
    
    
              final String SQL_CREATE_STUDENT_TABLE =
    
                              "CREATE TABLE " + studentContract.StudentEntry.TABLE_NAME + " ("  +
    
                               studentContract.StudentEntry._ID + " INTEGER  PRIMARY KEY , "  +
    
                               studentContract.StudentEntry.COLUMN_NAME + " TEXT , " +
    
                               studentContract.StudentEntry.COLUMN_DEGREE + " TEXT ); " ;
    
    
              db.execSQL(SQL_CREATE_STUDENT_TABLE);
    
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
             db.execSQL("drop database if exists student");
    
             onCreate(db);
        }
    }
    

    这是学生合同课

    
    导入android.content.ContentUris;
    导入android.content.ContentValues;
    导入android.database.Cursor;
    导入android.database.SQLException;
    导入android.database.sqlite.sqlite数据库;
    导入android.net.Uri;
    导入android.support.annotation.Nullable;
    导入android.text.TextUtils;
    
    *由AbdELMagied于2017年8月4日创建。
    */
    
    
    
    静态最终int学生=100;
    
    静态最终int STUDENT_,_ID=101;
    
    
    私有UriMatcher matcher(){
    
    UriMatcher mymatcher=新的UriMatcher(UriMatcher.NO\u MATCH);
    mymatcher.addURI(studentContract.authority,“student/#”,student_WITH_ID);
    
    }
    
    @覆盖
    公共布尔onCreate(){
    mydatabase=新的databaseopenhelper(getContext());
    返回true;
    
    
    @覆盖
    公共游标查询(Uri Uri、字符串[]投影、字符串选择、字符串[]selectionArgs、字符串排序器){
    光标返回光标;
    开关(urimatcher.match(uri)){
    
    案例学生:
    
    returncursor=mydatabase.getReadableDatabase()。查询(
    studentContract.Studentry.TABLE_名称,
    选择
    无效的
    无效的
    分拣机
    );
    打破
    
    
    字符串,其中=“_id=”+uri。getLastPathSegment()+(!TextUtils.isEmpty(选择)?和“+选择:”);
    
    returncursor=mydatabase.getReadableDatabase()。查询(
    
    studentContract.Studentry.TABLE_名称,
    哪里
    selectionArgs,
    无效的
    
    打破
    
    }
    
    returncursor.setNotificationUri(getContext()。getContentResolver(),uri);
    }
    
    
    @覆盖
    公共字符串getType(Uri Uri){
    返回null;
    
    @可为空
    long rowid=mydatabase.getWritableDatabase().insert(studentContract.studentry.TABLE_NAME,null,values);
    if(rowid>0)
    {
    Uri _Uri=ContentUris.withAppendedId(studentContract.studentry.CONTENT_Uri,rowid);
    }
    
    抛出新的SQLException(“未能添加记录”+uri);
    }
    
    public int delete(Uri Uri,String selection,String[]selectionArgs){
    if(selection=“”)selection=“1”;
    int numberofdeleted=0;
    案例学生:
    字符串,其中=“_id=”+uri。getLastPathSegment()+(!TextUtils.isEmpty(选择)?和“+选择:”);
    打破
    }
    {
    }
    已删除的返回编号;
    
    
    公共int更新(Uri Uri、ContentValues值、字符串选择、字符串[]selectionArgs){
    返回0;
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Stuckzilla    7 年前