从另一个活动调用函数 [英] Calling function from another activity

查看:19
本文介绍了从另一个活动调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,其中包含用于控制我的数据库的所有功能,并且我希望所有其他活动在与数据库交互时使用这些功能.下面是我的问题的一个例子.

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();数据.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.

我这样做了并且有效.

嗯,我的数据"是一个普通的类,而不是一个活动,但我也有一个以同样方式共享方法的活动.

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() + "
" +
                "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 ------------------------------- */

而不是作为活动.
我没有初始化类,它没有构造函数.
它只是一堆共享(而不是,用于执行我的代码中其他任何地方都没有看到的操作)方法.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆