从另一个活动中调用函数 [英] Calling function from another activity
问题描述
我有一个活动,其中包含用于控制数据库的所有功能,并且我希望所有其他活动在与数据库进行交互时都可以使用这些功能.下面是我的问题的一个例子.
I have an activity that contains all the functions for controlling my database, and I want all other activities to use to these functions when interacting with the database. Below is an example of my problem.
时钟脚本:
public class Clock extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clock);
Data.createTable(); //<<<
}
//...
}
数据脚本:
public class Data extends Activity
{
SQLiteDatabase mydb;
private static String DBNAME = "SHIFTS.db";
private static String TABLE = "MY_SHIFTS";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
}
public void createTable() //<<<
{
try
{
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
mydb.execSQL("CREATE TABLE IF NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY, STARTDATE TEXT, ENDDATE TEXT, LENGTH INTEGER, TYPE INTEGER);");
mydb.close();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Error in creating table", Toast.LENGTH_LONG).show();
}
}
// ... lots more functions
}
错误消息:
无法静态引用非静态方法createTable()从数据类型开始.
Cannot make a static reference to the non-static method createTable() from the type Data.
当我尝试将方法设为静态时,只会导致更多问题.
And when I trying to make method static, it just causes more problems.
如果我尝试Data data = new Data();data.createTable();我收到了NullPointerException.
Also if I try Data data = new Data(); data.createTable(); I get a NullPointerException.
这是什么问题?
推荐答案
将static关键字添加到共享方法中
Add the static keyword to your shared methods
public static void createTable()
然后使用:
Data.createTable();
在另一个类/片段/活动中的某个地方.
somewhere in another Class / Fragment / Activity.
我这样做,并且可以正常工作.
I do so and it works.
好吧,我的数据"是一个普通的类,不是一个Activity,但是我也有一个Activity,它以相同的方式共享方法.
Well, my "Data" is a normal Class, not an Activity, but I also have an Activity with shared methods in the very same way.
这是我的dbTableCreate方法:
This is my dbTableCreate method:
// Create the database table if it doesn't exist
protected final static void dbTableCreate(final Context ctx)
{
SQLiteDatabase db = null;
try
{
db = ctx.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
db.execSQL
(
"CREATE TABLE IF NOT EXISTS " + DB_TABLE +
" (date DATETIME PRIMARY KEY NOT NULL DEFAULT (CURRENT_DATE), " +
"score INTEGER DEFAULT (null));"
);
}
catch (final SQLiteException se)
{
System.out.println
(
ctx.getClass().getSimpleName() + "\n" +
"Could not create the database"
);
se.printStackTrace();
}
finally
{
db.close();
}
}
我这样称呼它:
Context ctx = getApplicationContext();
CLS_DB.dbTableCreate(ctx);
我班和你班的主要区别是我的被宣布为
The main difference between my class and yours is that mine is declared as
public final class CLS_DB
{
/* ----------------------------- Constants ------------------------------ */
private final static String DB_NAME = "pat.db";
private final static String DB_TABLE = "tests";
/* ------------------------------ Methods ------------------------------- */
而不是作为活动.
我没有初始化Class,它没有构造函数.
这只是共享方法的一个容器(不是,用于执行我的代码中其他地方都没有的操作).
And not as an Activity.
I'm not initializing the Class, and it has no constructor.
It's just a bin of shared (and not, for doing operations not seen anywhere else in my code) methods.
这篇关于从另一个活动中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!