我在我的android項目中使用viewpager。在片段中,我需要打開一個pdf文件。我用庫來處理PDF文件打開。沒有pdf打開方法(read()和open())這工作正常。但是當我調用這兩個方法這讓我異常我得到了java.lang.IllegalArgumentException:在嘗試使用viewpager時沒有找到id的視圖
以上這是我的主類
package com.android.pageviewers;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class PageViewActivity extends FragmentActivity implements Communicator{
MyPageAdapter pageAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View contentView = inflater.inflate(R.layout.activity_page_view, container, false);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)contentView.findViewById(R.id.pager);
pager.setAdapter(pageAdapter);
return contentView;
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(MyFragment.newInstance("Fragment 1"));
fList.add(MyFragment.newInstance("Fragment 2"));
//fList.add(MyFragment3.newInstance("Fragment 3"));
return fList;
}
private class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
@Override
public void viewPdf() {
//ShowPdf.read();
}
}
This is my fragment
package com.android.pageviewers;
import java.io.File;
import java.io.InputStream;
import com.epapyrus.plugpdf.SimpleDocumentReader;
import com.epapyrus.plugpdf.SimpleDocumentReaderListener;
import com.epapyrus.plugpdf.SimpleReaderFactory;
import com.epapyrus.plugpdf.core.viewer.DocumentState;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
// create a listener for receiving provide pdf loading results
SimpleDocumentReaderListener m_listener = new
SimpleDocumentReaderListener() {
@Override
public void onLoadFinish(DocumentState.OPEN state) {
}
};
public static final MyFragment newInstance(String message)
{
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.textView);
messageTextView.setText(message);
read();
return v;
}
public void read(){
String fileName = new String("1.pdf");
AssetManager am = getResources().getAssets();
try {
InputStream is = am.open(fileName);
//SimpleDocumentReader.openFile();
int size = is.available();
if (size > 0) {
byte[] data = new byte[size];
is.read(data);
open(data);
}
is.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void open(byte[] words) {
try {
// pdfviewer create.
SimpleDocumentReader viewer =
SimpleReaderFactory.createSimpleViewer(getActivity(), m_listener);
// pdf data load.
viewer.openData(words, words.length, "");
} catch (Exception ex) {
Log.e("KS", "error ", ex);
ex.printStackTrace();
}
}
}
當我運行這個項目它給了我所提到的error.I已經添加了ID爲pageviewer
。
10-01 08:56:45.273: E/AndroidRuntime(2824): java.lang.IllegalArgumentException: No view found for id 0x7f0a003c (com.android.pageviewers:id/pager) for fragment MyFragment{41468878 #1 id=0x7f0a003c android:switcher:2131361852:1}
這是我的佈局文件:
<RelativeLayout 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" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
我從我的主類中刪除onCreate方法和該onCreateView.Now沒有任何exception.but它不顯示我的PDF文件,只是空白頁,我無法刷卡。
你可以發佈佈局xml嗎? – 2014-10-01 01:20:25