如何从另一个类及其上下文中调用函数(Android,Java)

时间:2019-02-24 21:23:55

标签: java android android-viewpager android-adapter android-context

每当用户从viewPager中按下项目内的按钮时,我都试图重新加载viewPager。这种情况下的相关类是SetupActivityAdapter。在活动类中,我已经制作了两个函数来重新加载viewPager:

public void deleteSession(String title) {
        dbHelper = new DatabaseHelper(this);
        ((DatabaseHelper) dbHelper).deleteRow(title);
    }

public void refreshAdapter () {

        dbHelper = new DatabaseHelper(this); 
        Cursor dataCursor = ((DatabaseHelper) dbHelper).getData();
        models = new ArrayList<>(); 

        while (dataCursor.moveToNext()) { 
            models.add(new Model(
                    dataCursor.getString(1),
                    dataCursor.getInt(2),
                    dataCursor.getInt(3),
                    dataCursor.getInt(4)
            ));
        }
        adapter = new Adapter(models, this);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0, true);
    }

因此,基本上,deleteSession()从数据库中删除特定项目。 refreshAdapter()从数据库中读取更新的数据,并将新适配器设置为viewPager。 如果我从SetupActivity类的OnClick事件中调用它们,这些函数将正常工作。但是,如果我尝试通过Adapter类中的OnClick事件来调用它们,则应用程序将崩溃并出现以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
        at android.content.ContextWrapper.getDatabasePath(ContextWrapper.java:327)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:352)
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:322)
        at com.example.android.slink.DatabaseHelper.getData(DatabaseHelper.java:69)
        at com.example.android.slink.SetupActivity.refreshAdapter(SetupActivity.java:436)
        at com.example.android.slink.Adapter$3.onClick(Adapter.java:111)
        at android.view.View.performClick(View.java:6669)
        at android.view.View.performClickInternal(View.java:6638)
        at android.view.View.access$3100(View.java:789)
        at android.view.View$PerformClick.run(View.java:26145)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6863)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是我的SetupActiviy课(简化为相关部分):

package com.example.android.slink;

public class SetupActivity extends AppCompatActivity {

    private SQLiteOpenHelper dbHelper;
    private SQLiteDatabase database;

    ViewPager viewPager;
    Adapter adapter;
    static ArrayList<Model> models;

    public void deleteSession(String title) {
        dbHelper = new DatabaseHelper(this);
        ((DatabaseHelper) dbHelper).deleteRow(title);
    }

    public void refreshAdapter () {
        dbHelper = new DatabaseHelper(this);
        Cursor dataCursor = ((DatabaseHelper) dbHelper).getData();
        models = new ArrayList<>();
        models.add(new Model("", 0,0,0));
        while (dataCursor.moveToNext()) {
            models.add(new Model(
                    dataCursor.getString(1),
                    dataCursor.getInt(2),
                    dataCursor.getInt(3),
                    dataCursor.getInt(4)
            ));
        }
        adapter = new Adapter(models, this);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0, true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);

        editTextScreenTitle = findViewById(R.id.editTextScreenTitle);
        relativeLayoutCard = findViewById(R.id.relativeLayoutCard);
        viewPager = findViewById(R.id.viewPager);

        dbHelper = new DatabaseHelper(this);
        Cursor dataCursor = ((DatabaseHelper) dbHelper).getData();

        models = new ArrayList<>();
        models.add(new Model("", 0,0,0));
        while (dataCursor.moveToNext()) {
            models.add(new Model(
                    dataCursor.getString(1),
                    dataCursor.getInt(2),
                    dataCursor.getInt(3),
                    dataCursor.getInt(4)
            ));
        }
        adapter = new Adapter(models, this);
        viewPager.setAdapter(adapter);
    }
}

这是我的Adapter类(也简化为相关部分):

package com.example.android.slink;

public class Adapter extends PagerAdapter {

    private List<Model> models;
    private LayoutInflater layoutInflater;
    private Context context;

    SetupActivity setupActivity = new SetupActivity();

    public Adapter(List<Model> models, Context context) {
        this.models = models;
        this.context = context;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
        layoutInflater = LayoutInflater.from(context);
        final View view = layoutInflater.inflate(R.layout.item, container, false);

        TextView buttonDeleteCard = view.findViewById(R.id.textViewDeleteCard);

        buttonDeleteCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setupActivity.deleteSession([...some title...]);
                setupActivity.refreshAdapter();
            }
        });

        container.addView(view, 0);
        return view;
    }
}

我认为这与上下文有关。 我是否必须将上下文从SetupActiity传递到Adapter类,如果是,该怎么做? 还是不可能,我需要从Adapter类中的SetupActivity类中捕获OnClick事件? 我是Java编程的新手,如果您能帮助我解决此问题,将不胜感激!

1 个答案:

答案 0 :(得分:1)

像这样更改您的Adapter

public class Adapter extends PagerAdapter {

    private List<Model> models;
    private LayoutInflater layoutInflater;
    private Context context;

    private SetupActivity setupActivity;

    public Adapter(List<Model> models, Context context) {
        this.models = models;
        this.context = context;
        setupActivity = (SetupActivity) context;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
        layoutInflater = LayoutInflater.from(context);
        final View view = layoutInflater.inflate(R.layout.item, container, false);

        TextView buttonDeleteCard = view.findViewById(R.id.textViewDeleteCard);

        buttonDeleteCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setupActivity.deleteSession([...some title...]);
                setupActivity.refreshAdapter();
            }
        });

        container.addView(view, 0);
        return view;
    }
}

为什么会出错?

您将Context的{​​{1}}传递到SetupActivity,将该引用保存在字段(Adapter)中,但错误地创建了{{1 }}。

这就是为什么您获得context的原因。该活动是您的SetupActivity。它继承自它。因此,只需将其转换为构造函数,然后再使用即可。