如果我尝试在我的活动之外实例化并使用我的应用程序DBAdapter,则会抛出异常。我试图在我制作的一个简单的实用程序/辅助类中使用它:
public class MyClass
{
Context context;
DBAdapter db;
public MyClass(Context c)
{
// Get the application context that is passed to the class
context = c;
// Instantiate the database using the application context
db = new DBAdapter(context);
// Open the database and do something
db.open(); // <---- Exception thrown here
db.dosomething();
db.close();
}
}
我像这样打电话给MyClass
public class MainActivity extends Activity
{
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyClass whatever = new MyClass(this);
}
}
这是我的DBAdapter,非常简单和典型的任何Android SQLite教程:
public class DBAdapter
{
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context c)
{
context = c;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
public DatabaseHelper( Context context)
{
super(context, MY_DATABASE_NAME_HERE, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(/*CREATE TABLE CODE HERE*/);
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
/* UPDATE CODE HERE */
}
}
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
public void dosomething()
{
// code here
}
// Other database abstraction layer methods down here...
}
最后,例外。在我看来,db
对象是null或无效或我创建它时的东西。但似乎我做得对。我正在传递正确的应用程序/活动上下文。
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whatever.mysqltest/com.whatever.mysqltest.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.whatever.whatever.DBAdapter.open(DBAdapter.java:71)
at com.whatever.whatever.whatever.<init>(whatever.java:56)
at com.whatever.mysqltest.MainActivity.onCreate(MainActivity.java:22)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
... 11 more
或者,如果我从Activity中调用db
对象,一切都很好......
public class MainActivity extends Activity
{
DBAdapter db;
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new DBAdapter(this);
db.open(); // <--- does NOT thrown an exception.
db.dosomething();
db.close();
}
}
这里有什么问题?
答案 0 :(得分:1)
你的“MyClass”期待一个上下文,但你传递的是一个活动。尝试将上下文传递给构造函数,如下所示:
MyClass whatever = new MyClass(getContext());