我得到java.lang.IllegalArgumentException:尝试使用viewpager时没有找到id的视图

时间:2014-10-01 01:14:50

标签: android

我在我的android项目中使用viewpager。在片段中我需要打开一个pdf文件。我使用库来处理pdf文件打开.without pdf打开方法(read()和open())这个工作正常。但是当我调用这两种方法时,它会给我带来异常

这是我的主要课程

    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();
        } 
    }


     }

当我运行这个项目时,它给出了我提到的错误。我已经为pageviewer添加了id。

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>

我从我的主类和onCreateView.Now中删除了onCreate方法没有任何异常。但它没有显示pdf文件,只是blan页面,我也无法刷卡。

1 个答案:

答案 0 :(得分:0)

[被修改]

尝试使用以下onCreate()替换onCreateView()方法。你需要自己夸大布局。

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;
}