每当用户从viewPager中按下Item内的按钮时,我都会尝试重新加载viewPager。本案的相关类别是SetupActivity
和Adapter
。在activity类中,我已经使两个函数重新加载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编程的新手,如果你们中的任何人能帮助我解决这个问题,我将非常感激!
像这样改变你的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
从SetupActivity
传递到你的Adapter
,你在场上保存那个引用(context
),但是你错误地创建了一个新的SetupActivity
实例。
这就是你得到NullPointerException
的原因。活动是你的Context
。它继承了它。所以只需将它转换为构造函数并稍后使用它。