简体   繁体   English

Android Dagger2空指针异常

[英]Android dagger2 null pointer exception

Details 细节

I got a null pointer exception on Userdaoimpl while usig containValue and dbhelper object.Userdaoimpl extends the baseActivity where all the inject had been done. 我在Userdaoimpl上遇到了空指针异常,而usig containsValue和dbhelper对象。Userdaoimpl扩展了所有已完成注入的baseActivity。 If i use a ContainValue object on MainActivity then containValue(object) does not contain null.The problem is if i use dbhelper and containValue object on Userdaoimpl class then it show a null pointer exception but i had handled that exception. 如果我在MainActivity上使用ContainValue对象,那么containsValue(object)不包含null。问题是如果我在Userdaoimpl类上使用dbhelper和containValue对象,则显示空指针异常,但我已经处理了该异常。

AppComponent AppComponent

@Singleton @Component(modules={AppModule.class})
public interface AppComponent{
void inject(DaggerApplication application);
void inject(BaseActivity baseActivity);
}

AppModule 的AppModule

@Module
public class AppModule{
private final DaggerApplication application;
public AppModule(DaggerApplication application){
this.application = application;
}
@Provides @Singleton
Context providesApplicationContext(){return application;}
@Provides @Singleton
Context providesApplicationContext(){return application;}
@Provides @Singleton
SharedPreferences providesSharedPreferences(Context app){
return app.getSharedPreferences("MY_Prefs_Title",Context.MODE_PRIVATE)
;}
@Provides @Singleton
Resources provideResources(){
return application.getResources();
}
@Singleton @Provides
Dbhelper providesDbhelper(){
return new Dbhelper(application);
}
@Singleton @Provides
ContentValues provideContainValues(){
return new ContentValues();
}
@Singleton @Provides
User provideUserObject(){
return new User();
}
@Singleton @Provides
Userdao provideUserDaoimpl(){
return new Userdaoimpl();
}}

Constant 不变

public static final String DATABASE_NAME = "dragle_database";
public static final Integer DATABASE_VERSION = 1;
public static final String TABLE_NAME = "dagger";
public static final String FIRST_NAME ="first_name";
public static final String LAST_NAME ="last_name";
public static final String  ID = "id";
}

Userdao userDAO的

public interface Userdao{public boolean insertData(User user);}

Userdaoimpl 在UserDAOImpl

public class Userdaoimpl extends BaseActivity implements Userdao{
@Override
public boolean insertData(User user){
try{
contentValues.put(Constant.FIRST_NAME,user.getFirstname());
contentValues.put(Constant.LAST_NAME,user.getLastname());
dbhelper.getWritableDatabase().insert(Constant.TABLE_NAME,null,content
Values);
dbhelper.close();
return true;}
catch (Exception e){
Log.e("Exception",e.getMessage());
return false;
}
finally{
try{dbhelper.close();
}catch(Exception e)
Log.e("Exception",e.getMessage());
}
}}}

Dbhelper Dbhelper

public class Dbhelper extends SQLiteOpenHelper{
public Dbhelper(Context context){
super(context,Constant.DATABASE_NAME,null,Constant.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
Strng query ="CREATE TABLE IF NOT EXISTS "
+ Constant.TABLE_NAME + "("
+ Constant.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ Constant.FIRST_NAME + " VARCHAR(20), "
+ Constant.LAST_NAME + " VARCHAR(50))";
sqLiteDatabase.excSQL(query);
             sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase,int i,int i1){
}
}

User class 用户类别

public class User{
public String getFirstname(){
return firstname;
}
public User(String firstname,String lastname){
this.firstname = firstname;
this.lastname = lastname;
}
public void setFirstname(String firstname){
this.firstname = firstname;
}
public String getLastname(){
return lastname;
}
public void setLastname(String lastname){
this.lastname = lastname;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
private String firstname;
private String lastname;
private id id;
public User(){}
}

BaseActivity BaseActivity

public class BasActivity exteds AppComatActivity{
@Inject
public ContentValues contentValues;
@Inject
public Dbhelper dbhelper;
@Inject
Userdao userdao;
@Inject
User user;
@Inject
public SharedPreferences prefs;
@Inject
public Context context;
@Inject
Resources res;
@Override
public void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
((DaggerApplication)getApplication()).getAppComponent().inject(this);
}
}

DaggerApplication DaggerApplication

public class DaggerApplication extends Application{
AppComponent appComponent;
@Override
public void onCreate(){
super.onCreate();
appComponent =
DaggerAppComponent.builder().appModule(new AppModule(this).build();
appComponent.inject(this);
}
public AppComponent getAppComponent(){return appComponent;}
}

MainActivity 主要活动

public class MainActivity extends BasActivity{
private TextView textView;
@Override
public void onCreate(Bundle saveInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textview1);//this is from myxml
prefs.edit().putInt("Number",6).apply();
textView.setBackgroundColor(res.getColor(android.R.color.holo_red_dar
));
Toast.makeText(context,"onCreate:"The value is Number is"+String
.valueOf(prefs.getInt("Number",0)),Toast.LENGTH_SHORT).show();
userdao.insertData(user);
user.setFirstname("Machhindra");
user.setLastname("Neupane");
if(userdao.inserData(user)){
Toast.makeText(context,"data inserted",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context,"data not inserted",Toast.LENGTH_SHORT).show();
}}}

Instead of @providing a BaseActivity and passing Userdaoimpl as its polymorphic base class into the generated Dagger component, simply @provide a Userdaoimpl . 无需@providing一个BaseActivity并将Userdaoimpl作为其多态基类传递到生成的Dagger组件中,只需@provide一个Userdaoimpl Userdaoimpl is @provided then dagger will be able to inject it. Userdaoimpl@provided然后匕首将能够注入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM