在ViewPager中将值设置为TextView会强制关闭

时间:2014-07-08 10:19:11

标签: android android-fragments nullpointerexception android-viewpager

我在我的应用中做了以下事情:

  1. 我有一个名为FragmentOne的片段,其布局为list_view_details
  2. 我在我的片段中使用ViewPager
  3. 我在ViewPager中生成了一个名为details的布局。我在详细信息布局中有一个带有id android:id="@+id/page"和其他小部件的textview。
  4. 我想将android:id="@+id/page"中标识为details.xml的textview设置为值" hii"编程。我尝试进行以下编码,但它在第103行给了我nullpointerexception,即tv.setText(" hiii");我发布了我的代码和错误日志。我是这个概念的新手,所以请一步一步指导我。

    FragmentOne extends Fragment类如下:

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.list_view_details, container,
                false);
            final int imageArra[] = { R.drawable.leftbar_logo ,R.drawable.leftbar_logo};
            ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity(),imageArra);
            ViewPager myPager = (ViewPager)view.findViewById(R.id.pagers2);
    
           myPager.setAdapter(adapter);
          TextView tv=(TextView)myPager.findViewById(R.id.page);
          tv.setText("hiii");
          myPager.setCurrentItem(0);    
    
        return view;
    }
    

    ViewPagerAdapter扩展PagerAdapter类如下

             public Object instantiateItem(View collection, int position) {
    
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        int resId = 0,textviews;
        switch (position) {
        case 0:
            resId = R.layout.details;
    
            break;
        case 1:
            resId = R.layout.activity_main__load;
            break;
        case 2:
            resId = R.layout.feedback_email;
            break;
        case 3:
            resId = R.layout.feedback_email;
            break;
        case 4:
            resId = R.layout.menulist;
            break;
        }
    
        View view = inflater.inflate(resId, null);
    
        ((ViewPager) collection).addView(view, 0);
    
        return view;
    }
    

    错误日志

        E/AndroidRuntime(12230): FATAL EXCEPTION: main
        E/AndroidRuntime(12230): Process: com.alrimal, PID: 12230
        E/AndroidRuntime(12230): java.lang.NullPointerException
        E/AndroidRuntime(12230):    at com.alrimal.FragmentOne.onCreateView(FragmentOne.java:103)
        : E/AndroidRuntime(12230):  at android.app.Fragment.performCreateView(Fragment.java:1700)
         E/AndroidRuntime(12230):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
         E/AndroidRuntime(12230):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
         E/AndroidRuntime(12230):   at android.app.BackStackRecord.run(BackStackRecord.java:684)
         E/AndroidRuntime(12230):   at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
         E/AndroidRuntime(12230):   at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
         E/AndroidRuntime(12230):   at android.os.Handler.handleCallback(Handler.java:733)
         E/AndroidRuntime(12230):   at android.os.Handler.dispatchMessage(Handler.java:95)
         E/AndroidRuntime(12230):   at android.os.Looper.loop(Looper.java:136)
         E/AndroidRuntime(12230):   at android.app.ActivityThread.main(ActivityThread.java:5001)
         E/AndroidRuntime(12230):   at java.lang.reflect.Method.invokeNative(Native Method)
         E/AndroidRuntime(12230):   at java.lang.reflect.Method.invoke(Method.java:515)
        E/AndroidRuntime(12230):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
         E/AndroidRuntime(12230):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        E/AndroidRuntime(12230):    at dalvik.system.NativeStart.main(Native Method)
    

    第103行是tv.setText("hiii");

    list_view_details.xml

     <?xml version="1.0" encoding="utf-8"?>
       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@drawable/category_bg"
    
    >
    
    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="fill_parent"
        android:layout_height="70dp"
        android:layout_alignParentLeft="true"
        android:background="@android:color/white"
        android:layout_alignParentTop="true" >
    
        <LinearLayout
             android:id="@+id/linear"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
    
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>
    
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/horizontalScrollView1" >
     <android.support.v4.view.ViewPager
        android:id="@+id/pagers2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
     </RelativeLayout>
    
    </RelativeLayout>
    

    details.xml

      <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           >
    
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="250dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/categoryitem_bg" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:layout_marginLeft="23dp"
            android:layout_marginTop="18dp"
            android:src="@drawable/alrimal_logo" />
    
        <TextView
            android:id="@+id/page"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageView1"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="18dp"
            android:layout_marginRight="28dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
         </RelativeLayout>
    
        </RelativeLayout>
    

2 个答案:

答案 0 :(得分:0)

更改

TextView tv=(TextView)myPager.findViewById(R.id.page);

TextView tv=(TextView)view.findViewById(R.id.page);

答案 1 :(得分:0)

您正在使用viewpager作为视图来引用视图。使用viewpager引用类似textview的视图可能是问题所在。