我有以下代码。其中SacIndiaHelper类位于SacIndiaDatabaseAdapter类中。
package com.simha.yatras.database;
public class SacIndiaDatabaseAdapter {
SacIndiaHelper helper;
public SacIndiaDatabaseAdapter(Context context) {
helper = new SacIndiaHelper(context);
}
static class SacIndiaHelper extends SQLiteOpenHelper {
private Context context;
public SacIndiaHelper(Context context) {
super(context, DATABASE_NAME_sacredindia, null, DATABASE_VERSION);
this.context = context;
Message.message(context, "Constructor called");
}
}
}
我有另一个包我试图访问帮助对象:
package com.simha.yatras;
import com.simha.yatras.database.SacIndiaDatabaseAdapter;
public class MainActivity extends AppCompatActivity {
SacIndiaDatabaseAdapter sacIndiaHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level1placesgridview);
sacIndiaHelper = new SacIndiaDatabaseAdapter(this);
SQLiteDatabase sqLiteDatabase = sacIndiaHelper.helper.getWritableDatabase();
}
}
当我尝试编译时。它显示错误。它说
helper is not public in SacIndiaDatabaseAdapter; cannot be accessed from outside package
我尝试public static class SacIndiaHelper extends SQLiteOpenHelper
但仍显示错误。是什么问题。
答案 0 :(得分:0)
在检查时,SacIndiaHelper
不公开,只是静态的。试试public static
。
或者......在public
SacIndiaHelper
内制作课程static
和变量。
答案 1 :(得分:0)
像这样打电话
package com.simha.yatras;
import com.simha.yatras.database.SacIndiaDatabaseAdapter;
public class MainActivity extends AppCompatActivity {
SacIndiaDatabaseAdapter sacIndiaHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level1placesgridview);
SacIndiaDatabaseAdapter.SacIndiaHelper helper =
new SacIndiaDatabaseAdapter.SacIndiaHelper(context);
SQLiteDatabase db = helper.getWritableDatabase();
}
}
由于您的class
为static
且inner
为SacIndiaDatabaseAdapter
,您可以使用与ClassName.fieldName
字段static
相同的字段来调用它