0

我有一個由3個選項卡的tablayout組成的活動。每個選項卡都是一個片段,它們都包含一個pdf視圖來查看不同的pdf文件。如何從異步類訪問片段的視圖?

我已經爲Pdf Viewer寫了一個類。我需要在衆多片段中調用該類來打開該片段中的pdf文件。但我無法正確傳遞上下文並訪問fragmet的PDFview。我使用C.pdfviewer.PDFView作爲我的PDFviewer。

這裏是我的代碼:

MyFragment.class

public class MyFragment extends Fragment { 

PDFView pdfView; 
String url = "mysite.pdf"; 
public MyFragment() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_myfragment, container, false); 
    pdfView = (PDFView)view.findViewById(R.id.pdfViewer); 

    new RetrievePDFStream(this).execute(url); 

    return view; 
} 
} 

RetrievePDFStream.class

public class RetrievePDFStream extends AsyncTask<String, Void, byte[]> { 

public Fragment fContext; 
PDFView pdfView; 

public RetrievePDFStream(Fragment fContext) { 
    this.fContext = fContext; 
} 

@Override 
protected byte[] doInBackground(String... strings) { 
    InputStream inputStream = null; 
    try { 
     URL url = new URL(strings[0]); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     if (urlConnection.getResponseCode() == 200) { 
      inputStream = new BufferedInputStream(urlConnection.getInputStream()); 
     } 
    } catch (IOException ex) { 
     return null; 
    } 
    try { 
     return IOUtils.toByteArray(inputStream); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 

} 

@Override 
protected void onPostExecute(byte[] inputStream) { 
    View view = fContext.getView(); 
    pdfView = (PDFView)view.findViewById(R.id.pdfViewer); 
    pdfView.fromBytes(inputStream).load(); 
} 
} 

MyFragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="app.nvest.helpinsure.Fragment.MyFragment" 
android:orientation="vertical" 
android:background="#000000"> 

<com.github.barteksc.pdfviewer.PDFView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/pdfViewer"/> 

</LinearLayout> 

回答

0

嘗試像下面的代碼;

new RetrievePDFStream(getActivity()).execute(url); 
0

不像Activitiesfragment有不同的生命週期。在你的代碼

替換此:

new RetrievePDFStream(this).execute(url); //this 

隨着

new RetrievePDFStream(getActivity().execute(url)) 

您可以使用getActivity(),它返回一個片段相關聯的活動。

+0

我已經試過了。它沒有工作 – vinitpradhan18

+0

嘗試getActivity()。getBaseContext()。execute(url)。如果仍然沒有幫助,請發佈您的日誌 – Zoffa

0

您可以使用回調機制來實現此目的。使用下面的代碼。

public class MyFragment extends Fragment implements ApiResponseCallback { 

PDFView pdfView; 
String url = "mysite.pdf"; 
public MyFragment() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_myfragment, container, false); 
    pdfView = (PDFView)view.findViewById(R.id.pdfViewer); 

    new RetrievePDFStream(this).execute(url); 

    return view; 
} 
public void onByteArrayRecievedFromAPI(byte[] inputStream){ 
    pdfView = (PDFView)getView.findViewById(R.id.pdfViewer); 
    pdfView.fromBytes(inputStream).load(); 

} 
public interface ApiResponseCallback{ 
    void onByteArrayRecievedFromAPI(byte[] inputStream) 
} 
} 

並更改ASYC代碼這個

public class RetrievePDFStream extends AsyncTask<String, Void, byte[]> { 

//public Fragment fContext; 
//Change to This 
public ApiResponseCallback callback; 
PDFView pdfView; 

public RetrievePDFStream(ApiResponseCallback callback) { 
    this.callback = callback; 
} 

@Override 
protected byte[] doInBackground(String... strings) { 
    InputStream inputStream = null; 
    try { 
     URL url = new URL(strings[0]); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     if (urlConnection.getResponseCode() == 200) { 
      inputStream = new BufferedInputStream(urlConnection.getInputStream()); 
     } 
    } catch (IOException ex) { 
     return null; 
    } 
    try { 
     return IOUtils.toByteArray(inputStream); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 

} 

@Override 
protected void onPostExecute(byte[] inputStream) { 
    if(callback!=null){ 
     callback.onByteArrayRecievedFromAPI(inputStream); 
    } 
} 
} 
+0

在我的片段類中存在一個讀取'涉及循環繼承'的錯誤。此外,它在片段中以及在RetrievePDFStream類中實現ApiResponseCallback接口,如MyFragment.ApiResponseCallback。我需要它獨立於特定的片段,因爲我將從其他片段調用RetrievePDFStream類。 – vinitpradhan18

+0

所以請爲此回調創建新的Java文件,以便您可以根據需要使用。 –