1

我正在尝试将两个参数(fileURL 和 fileNAme)从一个类传递到另一个类,但是我遇到了错误,

   04-11 17:19:45.510: E/AndroidRuntime(2958): FATAL EXCEPTION: AsyncTask #1
    04-11 17:19:45.510: E/AndroidRuntime(2958): java.lang.RuntimeException: An error occured while 
executing doInBackground()
04-11 17:19:45.510: E/AndroidRuntime(2958):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at java.lang.Thread.run(Thread.java:856)
04-11 17:19:45.510: E/AndroidRuntime(2958): Caused by: java.lang.NullPointerException: println needs a message
04-11 17:19:45.510: E/AndroidRuntime(2958):     at android.util.Log.println_native(Native Method)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at android.util.Log.e(Log.java:231)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at com.androidhive.androidlistview.pdfDownload$DownloadFileFromURL.doInBackground(pdfDownload.java:155)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at com.androidhive.androidlistview.pdfDownload$DownloadFileFromURL.doInBackground(pdfDownload.java:1)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-11 17:19:45.510: E/AndroidRuntime(2958):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-11 17:19:45.510: E/AndroidRuntime(2958):     ... 4 more

这里是

我的第一个将参数传递给另一个类的类

        list.setOnItemClickListener(new ListView.OnItemClickListener() { 
         public void onItemClick(AdapterView<?> parent, View view, 
                  int position, long id) { 
                if (position == 0) { 
                    // pass parameters file name & file url 
                    Intent i = new Intent(ListView_SqliteActivity.this,pdfDownload.class); 
                    i.putExtra("FileName","hello.pdf"); 
                    i.putExtra("FileURL","http://www.gradsch.ohio-state.edu/depo/etd_tutorial/lesson2.pdf"); 
                    startActivity(i); 

                  } 

                    else{
                          Toast.makeText(ListView_SqliteActivity.this,
                                    " toast me " + listItemAdapter.getItem(position),
                                    Toast.LENGTH_SHORT).show();
                    }               }
        });

}

我的第二个带参数的类

public class pdfDownload extends Activity {

// button to show progress dialog
Button btnShowProgress;

// Progress Dialog
private ProgressDialog pDialog;
ImageView my_image;
// Progress dialog type (0 - for Horizontal progress bar)
public static final int progress_bar_type = 0; 
public static String fileURL;

// File url to download
public static String file_url = fileURL;
public static String fileName;



public static String file_name = fileName;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.download);

    // show progress bar button
    btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
    // Image view to show image after downloading
    my_image = (ImageView) findViewById(R.id.my_image);
    /**
     * Show Progress bar click event
     * */
    btnShowProgress.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) { 
                file_name = getIntent().getStringExtra("fileName");  
                file_url = getIntent().getStringExtra("fileURL"); 
        // starting new Async Task 
        new DownloadFileFromURL().execute(file_url); 

            // starting new Async Task
            new DownloadFileFromURL().execute(file_url);
        }
    });
}

在修复了之前的错误之后,一个新的错误来了,那就是来自这个类的 toast "file not found" 消息

protected void onPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
            dismissDialog(progress_bar_type);

        }
        File pdfFile = new File("/mnt/sdcard/pass/"+ file_name);{ 
        if(pdfFile.exists()) { 
            Uri path = Uri.fromFile(pdfFile);  
            Intent pdfIntent = new Intent(Intent.ACTION_VIEW); 
            pdfIntent.setDataAndType(path, "application/pdf"); 
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            try { 
               startActivity(pdfIntent); 
            } catch(ActivityNotFoundException e) { 
               Toast.makeText(pdfDownload.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();  
            } 
        } else { 
            Toast.makeText(pdfDownload.this, "File not found", Toast.LENGTH_LONG).show();  
        } 
    }`

带有新的错误消息

    04-11 18:06:40.809: E/AndroidRuntime(12884): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
04-11 18:06:40.809: E/AndroidRuntime(12884):    at android.app.LoadedApk.makeApplication(LoadedApk.java:482)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3938)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at android.app.ActivityThread.access$1300(ActivityThread.java:123)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at android.os.Looper.loop(Looper.java:137)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at java.lang.reflect.Method.invoke(Method.java:511)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at dalvik.system.NativeStart.main(Native Method)
04-11 18:06:40.809: E/AndroidRuntime(12884): Caused by: java.lang.NullPointerException
04-11 18:06:40.809: E/AndroidRuntime(12884):    at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:362)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at android.app.LoadedApk.getClassLoader(LoadedApk.java:305)
04-11 18:06:40.809: E/AndroidRuntime(12884):    at android.app.LoadedApk.makeApplication(LoadedApk.java:474)
04-11 18:06:40.809: E/AndroidRuntime(12884):    ... 11 more
4

2 回答 2

1

使用 getApplicationContext() 而不是 ListView_SqliteActivity.this

Intent i = new Intent(getApplicationContext(),pdfDownload.class); 
于 2012-04-11T14:40:29.210 回答
1

在您DownloadFileFromURL班级的第 155 行,您Log.e()使用无效参数调用。标记或消息为空,您可以使用以下代码解决问题:Log.e("tag",String.valueOf(errormessage));

于 2012-04-11T14:57:00.827 回答