o.realm.exceptions.RealmFileException:无法在路径“ /data/data/com.apphoctienganhpro.firstapp/files/default.realm”下打开领域:

时间:2018-08-31 12:33:06

标签: java android

领域类

public class RealmManager

    private static Realm mRealm;

    public static Realm open() {
        mRealm = Realm.getDefaultInstance();
        return mRealm;
    }

    public static void close() {
        if (mRealm != null) {
            mRealm.close();
        }
    }

    public static RecordsDao recordsDao() {
        checkForOpenRealm();
        return new RecordsDao(mRealm);
    }

    private static void checkForOpenRealm() {
        if (mRealm == null || mRealm.isClosed()) {
            throw new IllegalStateException("RealmManager: Realm is closed, call open() method first");
        }
    }
}

当我致电RealmManager.open();时,我的应用程序因错误而崩溃

  

无法在路径上打开领域   '/data/data/com.apphoctienganhpro.firstapp/files/default.realm':   不支持的Realm文件格式版本。在   /用户/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_SharedRealm.cpp   第92行类型:ACCESS_ERROR

活动课

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lesson);

    ButterKnife.bind(this);
    RealmManager.open();
    NestedScrollView scrollView = findViewById(R.id.nest_scrollview);
    scrollView.setFillViewport(true);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayShowTitleEnabled(true);
    }
    final CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar);
    AppBarLayout appBarLayout = findViewById(R.id.appbar);

    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        boolean isShow = true;
        int scrollRange = -1;

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if (scrollRange == -1) {
                scrollRange = appBarLayout.getTotalScrollRange();
            }
            if (scrollRange + verticalOffset == 0) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                isShow = true;
            } else if (isShow) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                isShow = false;
            }
        }
    });
    arrayListCategory = Utility.getAppcon().getSession().arrayListCategory;
    arrayListGoal = Utility.getAppcon().getSession().arrayListGoal;

    arrayList = new ArrayList<>();
    arrayList = Utility.getAppcon().getSession().arrayListCategory;

    if (arrayListGoal.size() > 0) {
        goal_id = arrayListGoal.get(0).getSingleGoalId();
    }else{
        if(!arrayList.get(0).getCategory_description().equals("")) {
            if(!Utility.getAppcon().getSession().screen_name.equals("lessson_complete")) {
                displayDialog();
            }
        }
    }
    collapsingToolbarLayout.setTitle(arrayListCategory.get(0).getCategoryName());
    layoutManager = new LinearLayoutManager(this);
    recycler_view.setLayoutManager(layoutManager);

    apiservice = ApiServiceCreator.createService("latest");
    sessionManager = new SessionManager(this);
    getCategoryLesson();
}

private void displayDialog() {

    dialog = new Dialog(LessonActivity.this);
    dialog.getWindow();
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_layout_lesson_detail);
    dialog.setCancelable(true);

    txt_close = dialog.findViewById(R.id.txt_close);
    txt_title_d = dialog.findViewById(R.id.txt_title_d);
    txt_description_d = dialog.findViewById(R.id.txt_description_d);
    img_main_d = dialog.findViewById(R.id.img_main_d);
    img_play_d = dialog.findViewById(R.id.img_play_d);
    img_play_d.setVisibility(View.GONE);

    Picasso.with(LessonActivity.this).load(ApiConstants.IMAGE_URL + arrayList.get(0).getCategoryImage())
            .noFade()
            .fit()
            .placeholder(R.drawable.icon_no_image)
            .into(img_main_d);

    txt_title_d.setText(arrayList.get(0).getCategoryName());
    txt_description_d.setText(Html.fromHtml(arrayList.get(0).getCategory_description().replaceAll("\\\\", "")));
    dialog.show();
    Window window = dialog.getWindow();
    assert window != null;
    window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    txt_close.setOnClickListener(view -> dialog.dismiss());
}

private void getCategoryLesson() {

    pDialog = new ProgressDialog(LessonActivity.this);
    pDialog.setTitle("Checking Data");
    pDialog.setMessage("Please Wait...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();

    Observable<LessonResponse> responseObservable = apiservice.getCategoryLesson(
            sessionManager.getKeyEmail(),
            arrayListCategory.get(0).getCategoryId(),
            goal_id,
            sessionManager.getUserData().get(0).getUserId(),
            sessionManager.getKeyToken());

    responseObservable.subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .onErrorResumeNext(throwable -> {
                if (throwable instanceof retrofit2.HttpException) {
                    retrofit2.HttpException ex = (retrofit2.HttpException) throwable;
                    Log.e("error", ex.getLocalizedMessage());
                }
                return Observable.empty();
            })
            .subscribe(new Observer<LessonResponse>() {
                @Override
                public void onCompleted() {
                    pDialog.dismiss();
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(LessonResponse lessonResponse) {
                    if (lessonResponse.getData().size() > 9) {
                        txt_total_lesson.setText(String.valueOf(lessonResponse.getData().size()));
                    } else {
                        txt_total_lesson.setText(String.valueOf("0" + lessonResponse.getData().size()));
                    }
                    if (lessonResponse.getStatusCode() == 200) {
                        adapter = new LessonAdapter(lessonResponse.getData(), LessonActivity.this);
                        recycler_view.setAdapter(adapter);
                    } else {
                        Utility.displayToast(getApplicationContext(), lessonResponse.getMessage());
                    }
                }
            });
}

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}

@Override
public void onClickFavButton(int position, boolean toggle) {

}


public boolean isFavourate(String LessionId,String UserId){
    if (arrayList!=null){
        for (int i=0;i<arrayList.size();i++)
            if (favarrayList.get(1).getLessonId().equals(LessionId) && favarrayList.get(0).getUserID().equals(UserId)){
                return true;
            }
    }
    return false;

}


@Override
protected void onDestroy() {
    super.onDestroy();
    RealmManager.close();
}

}

适配器类

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_lesson, parent, false);
    return new LessonAdapter.ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    dialog = new Dialog(context);
    dialog.getWindow();
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_layout_lesson_audio);

    txt_close = dialog.findViewById(R.id.txt_close);
    txt_title_d = dialog.findViewById(R.id.txt_title_d);
    txt_description_d = dialog.findViewById(R.id.txt_description_d);
    img_play_d = dialog.findViewById(R.id.img_play_d);
    frm_header = dialog.findViewById(R.id.frm_header);
    img_back_d = dialog.findViewById(R.id.img_back_d);

    holder.txt_lesson_title.setText(arrayList.get(position).getLessonName());

    holder.btn_view.setOnClickListener(view -> {
        txt_title_d.setText(arrayList.get(position).getLessonName());
        dialog.show();
        Window window = dialog.getWindow();
        assert window != null;
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        txt_description_d.setText(Html.fromHtml(arrayList.get(position).getLessonDescription().replaceAll("\\\\", "")));
        displayDialog(holder.getAdapterPosition());

    });



    holder.btn_go.setOnClickListener(view -> {
        Utility.getAppcon().getSession().arrayListLesson = new ArrayList<>();
        Utility.getAppcon().getSession().arrayListLesson.add(arrayList.get(position));
        Intent intent = new Intent(context, LessonCompleteActivity.class);
        context.startActivity(intent);
    });


    if (arrayList.get(position).isFavourate())
        holder.favCheck.setChecked(true);
    else
        holder.favCheck.setChecked(false);


    holder.favCheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CheckBox checkBox = (CheckBox) view;
            if (checkBox.isChecked()) {
                RealmManager.recordsDao().saveToFavorites(arrayList.get(position));
            } else {
                RealmManager.recordsDao().removeFromFavorites(arrayList.get(position));
            }
        }
    });

}








private void displayDialog(int Position) {

    final MediaPlayer mediaPlayer = new MediaPlayer();

    String url = ApiConstants.IMAGE_URL + arrayList.get(Position).getLesson_audio_url();
    //String url = "http://208.91.198.96/~diestechnologyco/apphoctienganhpro/uploads/mp3/mp3_1.mp3"; // your URL here
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        mediaPlayer.prepare(); // might take long! (for buffering, etc)
    } catch (IOException e) {
        e.printStackTrace();
    }

    img_play_d.setOnClickListener(view -> {
        if (audio_flag == 0) {
            mediaPlayer.start();
            audio_flag = 1;
            img_play_d.setImageResource(R.mipmap.pause_circle);
        } else {
            mediaPlayer.pause();
            audio_flag = 0;
            img_play_d.setImageResource(R.mipmap.icon_play);
        }
    });
    //img_play_d.setOnClickListener(view -> mediaPlayer.start());



    txt_close.setOnClickListener(view -> {
        mediaPlayer.stop();
        dialog.dismiss();
    });

    img_back_d.setOnClickListener(view -> {
        mediaPlayer.stop();
        dialog.dismiss();
    });
}

@Override
public int getItemCount() {
    return arrayList.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.txt_lesson_title)
    TextView txt_lesson_title;

    @BindView(R.id.txt_lesson_type)
    TextView txt_lesson_type;

    @BindView(R.id.btn_view)
    Button btn_view;

    @BindView(R.id.btn_go)
    Button btn_go;

    @BindView(R.id.image_favborder)
    CheckBox favCheck;



    ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        //RealmManager.open();

    }
}
public void getdata(){

}

public void addFavourate(String LessonId,String UserId) {

    if (mRealm != null) {

        mRealm.beginTransaction();
        favList_model = mRealm.createObject(FavList_model.class);
        favList_model.setLessonId(LessonId);
        favList_model.setUserID(UserId);
        mRealm.commitTransaction();
        mRealm.close();
    }
}

}

0 个答案:

没有答案