致命错误:执行doInBackground()时发生错误

时间:2016-02-11 04:54:11

标签: android-asynctask fatal-error

我是Android新手,我收到此错误.Maths1代码:

package com.materialdesign.geniusmathsandphysics;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;



public class Maths1 extends Activity{

MainActivity async;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maths1);
    async=new MainActivity();
    Button m15,d15;

    m15 = (Button) findViewById(R.id.but_m1may15);
    d15 = (Button) findViewById(R.id.but_m1dec15);

    m15.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            File pdf = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/GeniusPhysicsandMaths/" + "M1_May_15.pdf");
            if (pdf.exists()) {

                //file= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/pdf/Area.pdf");

                Intent i=new Intent(Maths1.this,PdfViewer.class);
                i.putExtra("FileName",pdf);
                startActivity(i);
            } else {
                if (isNetworkConnected() == true) {
                    Toast.makeText(Maths1.this, "Starting Download....please wait.", Toast.LENGTH_LONG).show();
                    async.new Connect().execute("166.62.10.29", "M1_May_15.pdf");
                } else {
                    Toast.makeText(Maths1.this, "No internet connection \n Please try again", Toast.LENGTH_SHORT).show();
                }

            }
        }
                    });

    d15.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            File pdf = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/GeniusPhysicsandMaths/" + "M1_Dec_15.pdf");

            if (pdf.exists()) {
                ViewPDF();

            } else {
                if (isNetworkConnected() == true) {
                    async.new Connect().execute("166.62.10.29", "M1_Dec_15.pdf");
                    Toast.makeText(Maths1.this, "Starting Download....please wait.", Toast.LENGTH_LONG).show();
                    ViewPDF();
                } else {
                    Toast.makeText(Maths1.this, "No internet connection \n Please try again", Toast.LENGTH_SHORT).show();
                }

            }
        }
    });

        }
private boolean isNetworkConnected() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    return cm.getActiveNetworkInfo() != null;
}
  public void ViewPDF()
  {
      Intent i=new Intent(Maths1.this,PdfViewer.class);
        SavePreferences("name", "M1_Dec_15.pdf");
      startActivity(i);
  }

private void SavePreferences(String key, String value) {
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
}

    }

pdf查看器代码:

package com.materialdesign.geniusmathsandphysics;
import android.content.SharedPreferences;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.File;

public class PdfViewer extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.pdfviewer);
    com.joanzapata.pdfview.PDFView Pdf = (com.joanzapata.pdfview.PDFView) findViewById(R.id.pdfview1);
    //String filepath=getIntent().getExtras().getString("name");
    String file=showPreferences("name");
    File fd = new File(file);
    //Log.i("hhhhdsdada",filepath);
    Pdf.fromFile(fd).defaultPage(1).swipeVertical(true).load();
}
private String showPreferences(String key){
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    String savedPref = sharedPreferences.getString(key, "");
    File files= new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/GeniusPhysicsandMaths/"+savedPref);
    String file=files.toString();
    return file;

}
}

这是我的logcat:

FATAL EXCEPTION: AsyncTask #3 Process: com.materialdesign.geniusmathsandphysics, PID: 18871
                                               java.lang.RuntimeException: An error occured while executing doInBackground()
                                                   at android.os.AsyncTask$3.done(AsyncTask.java:300)
                                                   at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                   at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                   at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                   at java.lang.Thread.run(Thread.java:818)
                                                Caused by: java.lang.RuntimeException: PDF file is corrupted
                                                   at org.vudroid.pdfdroid.codec.PdfDocument.open(Native Method)
                                                   at org.vudroid.pdfdroid.codec.PdfDocument.openDocument(PdfDocument.java:28)
                                                   at org.vudroid.pdfdroid.codec.PdfContext.openDocument(PdfContext.java:18)
                                                   at org.vudroid.core.DecodeServiceBase.open(DecodeServiceBase.java:59)
                                                   at com.joanzapata.pdfview.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:52)
                                                   at com.joanzapata.pdfview.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:31)
                                                   at android.os.AsyncTask$2.call(AsyncTask.java:288)
                                                   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                   at java.lang.Thread.run(Thread.java:818) 

我正在使用joanzapata库来查看pdf文件。因为我正在使用trenter代码来传递按钮点击列表器上的maths 1类的文件名。该库将文件作为输入,所以我不得不先将字符串转换为文件使用intent我得到空指针异常,但现在有了共享首选项,我收到此错误。请帮我解决一下。提前谢谢。

0 个答案:

没有答案