java内部类:不公开,不能从外部包访问

时间:2016-01-20 04:26:48

标签: java android

我有以下代码。其中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但仍显示错误。是什么问题。

2 个答案:

答案 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();        
    }
}

由于您的classstaticinnerSacIndiaDatabaseAdapter,您可以使用与ClassName.fieldName字段static相同的字段来调用它