如何使用其上下文(android,java)从另一个类调用函数

每当用户从 viewPager 按下项目内的按钮时,我都会尝试重新加载 viewPager。这种情况下的相关类是SetupActivity和Adapter。在活动类中,我已经创建了两个函数来重新加载 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 设置一个新的适配器。如果我从类中的 OnClick 事件中调用它们,这些函数工作正常SetupActivity。但是,如果我尝试从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)


慕尼黑的夜晚无繁华
浏览 124回答 1
1回答

小怪兽爱吃肉

像这样改变你Adapter:public class Adapter extends PagerAdapter {&nbsp; &nbsp; private List<Model> models;&nbsp; &nbsp; private LayoutInflater layoutInflater;&nbsp; &nbsp; private Context context;&nbsp; &nbsp; private SetupActivity setupActivity;&nbsp; &nbsp; public Adapter(List<Model> models, Context context) {&nbsp; &nbsp; &nbsp; &nbsp; this.models = models;&nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; setupActivity = (SetupActivity) context;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; @Override&nbsp; &nbsp; public Object instantiateItem(@NonNull final ViewGroup container, final int position) {&nbsp; &nbsp; &nbsp; &nbsp; layoutInflater = LayoutInflater.from(context);&nbsp; &nbsp; &nbsp; &nbsp; final View view = layoutInflater.inflate(R.layout.item, container, false);&nbsp; &nbsp; &nbsp; &nbsp; TextView buttonDeleteCard = view.findViewById(R.id.textViewDeleteCard);&nbsp; &nbsp; &nbsp; &nbsp; buttonDeleteCard.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setupActivity.deleteSession([...some title...]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setupActivity.refreshAdapter();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; container.addView(view, 0);&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }}为什么会出错?您将Contextfrom传递SetupActivity给您的Adapter,将该引用保存在字段 ( context) 中,但您错误地创建了SetupActivity.这就是为什么你得到NullPointerException. 活动是你的Context。它继承自它。因此,只需将其转换为您的构造函数并稍后使用它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java