这是MainActivity的onCreate方法,它位于LoginActivity之后。
public class MainActivity extends AppCompatActivity {
GridView medicineGrid;
ArrayList<Medicine> medicineArrayList;
GridAdapter adapter;
SQLiteHelper sqLiteHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
medicineGrid = (GridView) findViewById(R.id.gv_med_list);
try {
sqLiteHelper = new SQLiteHelper(MainActivity.this);
}catch (NullPointerException e){
e.printStackTrace();
}
medicineArrayList = new ArrayList<Medicine>();
medicineArrayList = sqLiteHelper.getAllMedicine();
adapter = new GridAdapter(MainActivity.this, medicineArrayList);
medicineGrid.setAdapter(adapter);
}
当MainActivity被执行时,应用程序将被NullPointer异常崩溃,如下所示:
致命的例外:主要 处理:com.example.amitava.medikart,PID:4662 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.amitava.medikart / com.example.amitava.medikart.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'android.content.res.Resources空对象引用上的android.content.Context.getResources()' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.content.res.Resources android.content.Context.getResources()' 在com.example.amitava.medikart.SQLiteHelper.onCreate(SQLiteHelper.java:57) 在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251) 在android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187) 在com.example.amitava.medikart.SQLiteHelper.getAllMedicine(SQLiteHelper.java:170) 在com.example.amitava.medikart.MainActivity.onCreate(MainActivity.java:40) 在android.app.Activity.performCreate(Activity.java:6662) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 申请已终止。
这是我写的SQLiteOpenHelper类,请检查我是否在这里做错了 -
public class SQLiteHelper extends SQLiteOpenHelper{
//Database details
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "MedicineDatabase.db";
//Table details within the database
public static final String TABLE_NAME = "MEDICINE";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_MEDICINE_NAME = "NAME";
public static final String COLUMN_CATEGORY = "CATEGORY";
public static final String COLUMN_DESCRIPTION = "DESCRIPTION";
public static final String COLUMN_IMAGE_BITMAP = "IMAGE";
public static final String COLUMN_STRENGTH = "STRENGTH";
public static final String COLUMN_PRICE = "PRICE";
public Context context;
public SQLiteHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//Create New Table
sqLiteDatabase.execSQL("Create Table " + TABLE_NAME + "(" + COLUMN_ID + " Integer Primary Key Autoincrement, " +
COLUMN_MEDICINE_NAME + " Varchar, " + COLUMN_CATEGORY + " Varchar, " + COLUMN_DESCRIPTION + " Varchar, " +
COLUMN_STRENGTH + " Varchar, " + COLUMN_PRICE + " Varchar, " + COLUMN_IMAGE_BITMAP + " Blob);");
//Insert static data into the table which would work as a API for the dummy project
//First row
Bitmap bitmap1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.atorvastatin);
byte[] picture1 = getBytes(bitmap1);
addEntry("Atorvastatin", "Cardiac", "Atorvastatin is used along with a proper diet to help" +
" lower \"bad\" cholesterol and fats (such as LDL, triglycerides) and raise \"good\" cholesterol (HDL) in the blood." +
" It belongs to a group of drugs known as \"statins.\" It works by reducing the amount of cholesterol made by the liver. " +
"Lowering \"bad\" cholesterol and triglycerides and raising \"good\" cholesterol decreases the risk of heart disease and helps" +
" prevent strokes and heart attacks.", "10 mg", "610", picture1);
//Second Row
Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.amlodipine_besylate_atenolol);
byte[] picture2 = getBytes(bitmap2);
addEntry("Amlodipine Besylate Atenolol", "Cardiac", "Amlodipine ATenolol Tablet is used for Hypertension, " +
"Elevated blood pressure, Blood pressure, Chest pain, Coronary artery disease, High blood pressure and other conditions. Amlodipine" +
" ATenolol Tablet works by inhibiting the influx of calcium ions into vascular smooth muscle and cardiac muscles; blocking receptors" +
" resulting in slowing of heart rate and reducing oxygen demand", "50 mg", "350", picture2);
//Third Row...}
请帮助我理解为什么我得到NullPointerException来调用getResources()方法