@@ -87,7 +87,9 @@ protected void onCreate(Bundle savedInstanceState) {
8787 private void initView () {
8888 Toolbar toolbar = (Toolbar ) findViewById (R .id .toolbar_new );
8989 setSupportActionBar (toolbar );
90- getSupportActionBar ().setDisplayHomeAsUpEnabled (true );
90+ if (getSupportActionBar () != null ) {
91+ getSupportActionBar ().setDisplayHomeAsUpEnabled (true );
92+ }
9193 //toolbar.setNavigationIcon(R.drawable.ic_dialog_info);
9294 toolbar .setNavigationOnClickListener (new View .OnClickListener () {
9395 @ Override
@@ -96,15 +98,6 @@ public void onClick(View v) {
9698 }
9799 });
98100
99- FloatingActionButton fab = (FloatingActionButton ) findViewById (R .id .fab_new );
100- fab .setOnClickListener (new View .OnClickListener () {
101- @ Override
102- public void onClick (View view ) {
103- Snackbar .make (view , "Replace with your own action" , Snackbar .LENGTH_LONG )
104- .setAction ("Action" , null ).show ();
105- }
106- });
107-
108101 groupDao = new GroupDao (this );
109102 noteDao = new NoteDao (this );
110103 note = new Note ();
@@ -121,34 +114,6 @@ public void onClick(View view) {
121114 tv_new_time = (TextView ) findViewById (R .id .tv_new_time );
122115 tv_new_group = (TextView ) findViewById (R .id .tv_new_group );
123116
124- // 图片删除事件
125- et_new_content .setOnRtImageDeleteListener (new RichTextEditor .OnRtImageDeleteListener () {
126-
127- @ Override
128- public void onRtImageDelete (String imagePath ) {
129- if (!TextUtils .isEmpty (imagePath )) {
130- boolean isOK = SDCardUtil .deleteFile (imagePath );
131- if (isOK ) {
132- showToast ("删除成功:" + imagePath );
133- }
134- }
135- }
136- });
137- // 图片点击事件
138- et_new_content .setOnRtImageClickListener (new RichTextEditor .OnRtImageClickListener () {
139- @ Override
140- public void onRtImageClick (String imagePath ) {
141- myContent = getEditData ();
142- if (!TextUtils .isEmpty (myContent )){
143- List <String > imageList = StringUtils .getTextFromHtml (myContent , true );
144- if (!TextUtils .isEmpty (imagePath )) {
145- int currentPosition = imageList .indexOf (imagePath );
146- showToast ("点击图片:" + currentPosition + ":" + imagePath );
147- }
148- }
149- }
150- });
151-
152117 openSoftKeyInput ();//打开软键盘显示
153118
154119 Intent intent = getIntent ();
@@ -178,9 +143,7 @@ public void onRtImageClick(String imagePath) {
178143 et_new_content .post (new Runnable () {
179144 @ Override
180145 public void run () {
181- //showEditData(note.getContent());
182- et_new_content .clearAllLayout ();
183- showDataSync (note .getContent ());
146+ dealWithContent ();
184147 }
185148 });
186149 }
@@ -196,6 +159,40 @@ public void run() {
196159
197160 }
198161
162+ private void dealWithContent (){
163+ //showEditData(note.getContent());
164+ et_new_content .clearAllLayout ();
165+ showDataSync (note .getContent ());
166+
167+ // 图片删除事件
168+ et_new_content .setOnRtImageDeleteListener (new RichTextEditor .OnRtImageDeleteListener () {
169+
170+ @ Override
171+ public void onRtImageDelete (String imagePath ) {
172+ if (!TextUtils .isEmpty (imagePath )) {
173+ boolean isOK = SDCardUtil .deleteFile (imagePath );
174+ if (isOK ) {
175+ showToast ("删除成功:" + imagePath );
176+ }
177+ }
178+ }
179+ });
180+ // 图片点击事件
181+ et_new_content .setOnRtImageClickListener (new RichTextEditor .OnRtImageClickListener () {
182+ @ Override
183+ public void onRtImageClick (String imagePath ) {
184+ myContent = getEditData ();
185+ if (!TextUtils .isEmpty (myContent )){
186+ List <String > imageList = StringUtils .getTextFromHtml (myContent , true );
187+ if (!TextUtils .isEmpty (imagePath )) {
188+ int currentPosition = imageList .indexOf (imagePath );
189+ showToast ("点击图片:" + currentPosition + ":" + imagePath );
190+ }
191+ }
192+ }
193+ });
194+ }
195+
199196 /**
200197 * 关闭软键盘
201198 */
@@ -247,8 +244,10 @@ public void onComplete() {
247244 if (loadingDialog != null ){
248245 loadingDialog .dismiss ();
249246 }
250- //在图片全部插入完毕后,再插入一个EditText,防止最后一张图片后无法插入文字
251- et_new_content .addEditTextAtIndex (et_new_content .getLastIndex (), "" );
247+ if (et_new_content != null ) {
248+ //在图片全部插入完毕后,再插入一个EditText,防止最后一张图片后无法插入文字
249+ et_new_content .addEditTextAtIndex (et_new_content .getLastIndex (), "" );
250+ }
252251 }
253252
254253 @ Override
@@ -266,14 +265,16 @@ public void onSubscribe(Disposable d) {
266265
267266 @ Override
268267 public void onNext (String text ) {
269- if (text .contains ("<img" ) && text .contains ("src=" )) {
270- //imagePath可能是本地路径,也可能是网络地址
271- String imagePath = StringUtils .getImgSrc (text );
272- //插入空的EditText,以便在图片前后插入文字
273- et_new_content .addEditTextAtIndex (et_new_content .getLastIndex (), "" );
274- et_new_content .addImageViewAtIndex (et_new_content .getLastIndex (), imagePath );
275- } else {
276- et_new_content .addEditTextAtIndex (et_new_content .getLastIndex (), text );
268+ if (et_new_content != null ) {
269+ if (text .contains ("<img" ) && text .contains ("src=" )) {
270+ //imagePath可能是本地路径,也可能是网络地址
271+ String imagePath = StringUtils .getImgSrc (text );
272+ //插入空的EditText,以便在图片前后插入文字
273+ et_new_content .addEditTextAtIndex (et_new_content .getLastIndex (), "" );
274+ et_new_content .addImageViewAtIndex (et_new_content .getLastIndex (), imagePath );
275+ } else {
276+ et_new_content .addEditTextAtIndex (et_new_content .getLastIndex (), text );
277+ }
277278 }
278279 }
279280 });
@@ -315,7 +316,7 @@ private String getEditData() {
315316 /**
316317 * 保存数据,=0销毁当前界面,=1不销毁界面,为了防止在后台时保存笔记并销毁,应该只保存笔记
317318 */
318- private void saveNoteData (boolean isBackground ) {
319+ private void saveNoteData (boolean isBackground ) throws Exception {
319320 String noteTitle = et_new_title .getText ().toString ();
320321 String noteContent = getEditData ();
321322 String groupName = tv_new_group .getText ().toString ();
@@ -382,7 +383,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
382383 callGallery ();
383384 break ;
384385 case R .id .action_new_save :
385- saveNoteData (false );
386+ try {
387+ saveNoteData (false );
388+ } catch (Exception e ) {
389+ e .printStackTrace ();
390+ }
386391 break ;
387392 }
388393 return super .onOptionsItemSelected (item );
@@ -502,37 +507,45 @@ protected void onResume() {
502507 @ Override
503508 protected void onStop () {
504509 super .onStop ();
505- //如果APP处于后台,或者手机锁屏,则保存数据
506- if (CommonUtil .isAppOnBackground (getApplicationContext ()) ||
507- CommonUtil .isLockScreeen (getApplicationContext ())){
508- saveNoteData (true );//处于后台时保存数据
509- }
510+ try {
511+ //如果APP处于后台,或者手机锁屏,则保存数据
512+ if (CommonUtil .isAppOnBackground (getApplicationContext ()) ||
513+ CommonUtil .isLockScreeen (getApplicationContext ())){
514+ saveNoteData (true );//处于后台时保存数据
515+ }
510516
511- if (subsLoading != null && subsLoading .isDisposed ()){
512- subsLoading .dispose ();
513- }
514- if (subsInsert != null && subsInsert .isDisposed ()){
515- subsInsert .dispose ();
517+ if (subsLoading != null && subsLoading .isDisposed ()){
518+ subsLoading .dispose ();
519+ }
520+ if (subsInsert != null && subsInsert .isDisposed ()){
521+ subsInsert .dispose ();
522+ }
523+ } catch (Exception e ) {
524+ e .printStackTrace ();
516525 }
517526 }
518527
519528 /**
520529 * 退出处理
521530 */
522531 private void dealwithExit (){
523- String noteTitle = et_new_title .getText ().toString ();
524- String noteContent = getEditData ();
525- String groupName = tv_new_group .getText ().toString ();
526- String noteTime = tv_new_time .getText ().toString ();
527- if (flag == 0 ) {//新建笔记
528- if (noteTitle .length () > 0 || noteContent .length () > 0 ) {
529- saveNoteData (false );
530- }
531- }else if (flag == 1 ) {//编辑笔记
532- if (!noteTitle .equals (myTitle ) || !noteContent .equals (myContent )
533- || !groupName .equals (myGroupName ) || !noteTime .equals (myNoteTime )) {
534- saveNoteData (false );
532+ try {
533+ String noteTitle = et_new_title .getText ().toString ();
534+ String noteContent = getEditData ();
535+ String groupName = tv_new_group .getText ().toString ();
536+ String noteTime = tv_new_time .getText ().toString ();
537+ if (flag == 0 ) {//新建笔记
538+ if (noteTitle .length () > 0 || noteContent .length () > 0 ) {
539+ saveNoteData (false );
540+ }
541+ }else if (flag == 1 ) {//编辑笔记
542+ if (!noteTitle .equals (myTitle ) || !noteContent .equals (myContent )
543+ || !groupName .equals (myGroupName ) || !noteTime .equals (myNoteTime )) {
544+ saveNoteData (false );
545+ }
535546 }
547+ } catch (Exception e ) {
548+ e .printStackTrace ();
536549 }
537550 finish ();
538551 }
0 commit comments