Skip to content

Commit

Permalink
Fix null pointer exception crash
Browse files Browse the repository at this point in the history
  • Loading branch information
ZYFDroid committed Jun 22, 2020
1 parent 883b2df commit fcd1ad6
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 8 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ android {

buildTypes {
release {
minifyEnabled true
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.zyfdroid.epub">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:name=".Program"
Expand Down
Binary file added app/src/main/assets/alice.epub
Binary file not shown.
12 changes: 8 additions & 4 deletions app/src/main/java/com/zyfdroid/epub/ReadingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,20 @@ public void run(String arg) {
int stack = 0;

void enumrateToc(TocEntry[] root) {
if(root==null){return;}
for (TocEntry toc : root) {
tocList.add((TocEntry) toc.clone());
String tabs = "";
for (int i = 0; i < stack; i++) {
tabs += " ";
}
tocList.get(tocList.size() - 1).label = tabs + tocList.get(tocList.size() - 1).label.trim();
tocHashMap.put(toc.href, toc);
String str = "";
try{
tocList.get(tocList.size() - 1).label = tabs + tocList.get(tocList.size() - 1).label.trim();
tocHashMap.put(toc.href, toc);
}catch (NullPointerException npe){
Log.e("Unknown toc","",npe);
}
stack++;
enumrateToc(toc.subitems);
stack--;
Expand Down Expand Up @@ -463,8 +469,6 @@ public void run(String arg) {
});

}


void displayPageInfo() {
getSupportActionBar().setSubtitle("[" + currentPage + "] " + currentChapter);
}
Expand Down
38 changes: 35 additions & 3 deletions app/src/main/java/com/zyfdroid/epub/SplashActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.StrictMode;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
Expand All @@ -13,20 +15,26 @@

import com.squareup.picasso.Picasso;
import com.zyfdroid.epub.utils.DBUtils;
import com.zyfdroid.epub.utils.SpUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class SplashActivity extends AppCompatActivity {


private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
"android.permission.READ_EXTERNAL_STORAGE" };
"android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE" };


public void verifyStoragePermissions(Activity activity) {
try {
//检测是否有写的权限
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.READ_EXTERNAL_STORAGE");
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 没有写的权限,去申请写的权限,会弹出对话框
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
Expand All @@ -40,7 +48,7 @@ public boolean hasStorage(){
try {
//检测是否有写的权限
int permission = ActivityCompat.checkSelfPermission(this,
"android.permission.READ_EXTERNAL_STORAGE");
"android.permission.WRITE_EXTERNAL_STORAGE");
return permission == PackageManager.PERMISSION_GRANTED;
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -63,6 +71,30 @@ protected void onCreate(Bundle savedInstanceState) {
}

public void jump(){
if(SpUtils.getInstance(this).firstRun()){
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().build());
File targetFile = new File(Environment.getExternalStorageDirectory(),"Books");
if(!targetFile.exists()){targetFile.mkdirs();}
targetFile = new File(targetFile,"AliceInWonderland.epub");
if(!targetFile.exists()){
try {
targetFile.createNewFile();
FileOutputStream fos = new FileOutputStream(targetFile);
InputStream is = getResources().getAssets().open("alice.epub");
byte[] buffer = new byte[2048];
int len=0;
while ((len = is.read(buffer))>0){
fos.write(buffer,0,len);
}
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

hWnd.postDelayed(new Runnable() {
@Override
public void run() {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/zyfdroid/epub/utils/SpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ public boolean shouldOpenWithExternalReader(){
return sp.getBoolean("openWithExternalReader",false);
}

public boolean firstRun(){
boolean firstRun = sp.getBoolean("firstRun",true);
sp.edit().putBoolean("firstRun",false);
return firstRun;
}

}

0 comments on commit fcd1ad6

Please sign in to comment.