Android 将strin值从一个类传递到另一个类并在其中初始化db时出现空指针异常
第二个类是DatabaseManipulation.class,如下所示Android 将strin值从一个类传递到另一个类并在其中初始化db时出现空指针异常,android,sqlite,nullpointerexception,Android,Sqlite,Nullpointerexception,第二个类是DatabaseManipulation.class,如下所示 //packages //imports public class MainActivity extends Activity { // some string declarations; // object creations etc etc DatabaseManipulation dbManage = new DatabaseManipulation();//whereDatabaseManipula
//packages
//imports
public class MainActivity extends Activity
{
// some string declarations;
// object creations etc etc
DatabaseManipulation dbManage = new DatabaseManipulation();//whereDatabaseManipulation is a class bdManage -->object
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//creating contentViews;
}
public void methods()
{
//works
}
public void methods2(String name)
{
try{
dbManage.functioncall(name);
}
catch(Exception e)
{
System.out.println(e);// value of e is NullPointerException
}
}
}
我只想把这个函数从主活动调用到DatabaseManipulation.java,我做错了什么?
在日志中,cat Null指针异常显示为错误,因为我在try catch中调用了它
我们真的可以做这种类型的函数调用吗?请提供帮助您不能实例化这样的活动:
public class DatabaseManipulation extends Activity
{
public void functioncall(String name)
{
System.out.println(bookName+ " bookname in addBookmark");
SQLiteDatabase db;
db = openOrCreateDatabase("epub.db", SQLiteDatabase.CREATE_IF_NECESSARY,null);// error occurs here
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
String temp_address="nothing";
try
{
String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=?";
Cursor c = db.rawQuery(selectQuery, new String[] { name });
if (c.moveToFirst()) {
temp_address = c.getString(c.getColumnIndex("lastchapter")); // assigning value in lastchapter to temp_address
}
System.out.println(temp_address+" result of select Query"); //
}
catch(Exception e)
{
System.out.println(e);
}
try
{
System.out.println("BLOW FILE111");
System.out.println("BLOW FILE222");
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER UNIQUE AUTOINCREMENT, bookpath TEXT , lastchapter TEXT, PRIMARY KEY(bookpath,lastchapter));";
db.execSQL(createtabBook);
ContentValues val=new ContentValues();
val.put("bookpath",name );
val.put("lastchapter", temp_address);
db.insert("Bookdetails", val, "bookpath="+name, null);
System.out.println("BLOW FILE333");
}
catch(Exception e)
{
System.out.println(e+" errors happens");
}
finally
{
db.close();
}
}
}
DatabaseManipulation dbManage = new DatabaseManipulation();
你需要使用意图。大概是这样的:
public class DatabaseManipulation extends Activity
{
public void functioncall(String name)
{
System.out.println(bookName+ " bookname in addBookmark");
SQLiteDatabase db;
db = openOrCreateDatabase("epub.db", SQLiteDatabase.CREATE_IF_NECESSARY,null);// error occurs here
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
String temp_address="nothing";
try
{
String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=?";
Cursor c = db.rawQuery(selectQuery, new String[] { name });
if (c.moveToFirst()) {
temp_address = c.getString(c.getColumnIndex("lastchapter")); // assigning value in lastchapter to temp_address
}
System.out.println(temp_address+" result of select Query"); //
}
catch(Exception e)
{
System.out.println(e);
}
try
{
System.out.println("BLOW FILE111");
System.out.println("BLOW FILE222");
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER UNIQUE AUTOINCREMENT, bookpath TEXT , lastchapter TEXT, PRIMARY KEY(bookpath,lastchapter));";
db.execSQL(createtabBook);
ContentValues val=new ContentValues();
val.put("bookpath",name );
val.put("lastchapter", temp_address);
db.insert("Bookdetails", val, "bookpath="+name, null);
System.out.println("BLOW FILE333");
}
catch(Exception e)
{
System.out.println(e+" errors happens");
}
finally
{
db.close();
}
}
}
DatabaseManipulation dbManage = new DatabaseManipulation();
不能像这样实例化活动:
public class DatabaseManipulation extends Activity
{
public void functioncall(String name)
{
System.out.println(bookName+ " bookname in addBookmark");
SQLiteDatabase db;
db = openOrCreateDatabase("epub.db", SQLiteDatabase.CREATE_IF_NECESSARY,null);// error occurs here
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
String temp_address="nothing";
try
{
String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=?";
Cursor c = db.rawQuery(selectQuery, new String[] { name });
if (c.moveToFirst()) {
temp_address = c.getString(c.getColumnIndex("lastchapter")); // assigning value in lastchapter to temp_address
}
System.out.println(temp_address+" result of select Query"); //
}
catch(Exception e)
{
System.out.println(e);
}
try
{
System.out.println("BLOW FILE111");
System.out.println("BLOW FILE222");
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER UNIQUE AUTOINCREMENT, bookpath TEXT , lastchapter TEXT, PRIMARY KEY(bookpath,lastchapter));";
db.execSQL(createtabBook);
ContentValues val=new ContentValues();
val.put("bookpath",name );
val.put("lastchapter", temp_address);
db.insert("Bookdetails", val, "bookpath="+name, null);
System.out.println("BLOW FILE333");
}
catch(Exception e)
{
System.out.println(e+" errors happens");
}
finally
{
db.close();
}
}
}
DatabaseManipulation dbManage = new DatabaseManipulation();
你需要使用意图。大概是这样的:
public class DatabaseManipulation extends Activity
{
public void functioncall(String name)
{
System.out.println(bookName+ " bookname in addBookmark");
SQLiteDatabase db;
db = openOrCreateDatabase("epub.db", SQLiteDatabase.CREATE_IF_NECESSARY,null);// error occurs here
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
String temp_address="nothing";
try
{
String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=?";
Cursor c = db.rawQuery(selectQuery, new String[] { name });
if (c.moveToFirst()) {
temp_address = c.getString(c.getColumnIndex("lastchapter")); // assigning value in lastchapter to temp_address
}
System.out.println(temp_address+" result of select Query"); //
}
catch(Exception e)
{
System.out.println(e);
}
try
{
System.out.println("BLOW FILE111");
System.out.println("BLOW FILE222");
final String createtabBook="CREATE TABLE IF NOT EXISTS BookMark(lid INTEGER UNIQUE AUTOINCREMENT, bookpath TEXT , lastchapter TEXT, PRIMARY KEY(bookpath,lastchapter));";
db.execSQL(createtabBook);
ContentValues val=new ContentValues();
val.put("bookpath",name );
val.put("lastchapter", temp_address);
db.insert("Bookdetails", val, "bookpath="+name, null);
System.out.println("BLOW FILE333");
}
catch(Exception e)
{
System.out.println(e+" errors happens");
}
finally
{
db.close();
}
}
}
DatabaseManipulation dbManage = new DatabaseManipulation();
您不能在启动之前初始化数据库类
Intent intent = new Intent(this, DatabaseManipulation.class);
startActivity(intent);
方法。为数据库类生成全局变量
super.onCreate(savedInstanceState);
然后在onCreate()方法上初始化它
DatabaseManipulation dbManage;
方法
setContentView(R.layout.urlayout)
您不能在启动之前初始化数据库类
Intent intent = new Intent(this, DatabaseManipulation.class);
startActivity(intent);
方法。为数据库类生成全局变量
super.onCreate(savedInstanceState);
然后在onCreate()方法上初始化它
DatabaseManipulation dbManage;
方法
setContentView(R.layout.urlayout)
你在哪里扩展DatabaseManipulation类中的活动?可能与你在DatabaseManipulation类中扩展活动的位置重复?可能重复我确实喜欢你说的,但仍然是相同的错误,db初始化的位置我确实喜欢你说的,但仍然是相同的错误,db初始化的位置将启动TractivityForresult将控制权带回Main活动将启动TractivityForresult将控制权带回Main活动