在类之间传递参数

时间:2012-04-11 14:35:09

标签: android parameters

我正在尝试将两个参数(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“找不到文件”消息 `

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

2 个答案:

答案 0 :(得分:1)

使用getApplicationContext()而不是ListView_SqliteActivity.this

Intent i = new Intent(getApplicationContext(),pdfDownload.class); 

答案 1 :(得分:1)

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