从活动布局外部调用视图

时间:2014-01-14 05:24:08

标签: android android-layout android-inflate

我正在从我的活动布局外面调用一个视图,所以我正在做:

 View v = inflater.inflate(R.layout.image_view_layout, null);
 ImageView iv = (ImageView) v.findViewById(R.id.img_pager);

其中R.layout.image_view_layout是要调用的视图的布局,而R.id.img_pager是我要调用的视图,但是为什么会出现nullpointer异常?

这是我的image_view_layout.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >

    <ImageView
        android:id="@+id/img_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/desc_intro"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"


        />

</LinearLayout>

这是logcat:

01-14 07:33:19.204: E/AndroidRuntime(22767): FATAL EXCEPTION: main
01-14 07:33:19.204: E/AndroidRuntime(22767): java.lang.RuntimeException: Unable to start activity ComponentInfo{package_name/packagename.Introduction_Activity}: java.lang.NullPointerException
01-14 07:33:19.204: E/AndroidRuntime(22767):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at android.os.Looper.loop(Looper.java:137)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at android.app.ActivityThread.main(ActivityThread.java:4898)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at java.lang.reflect.Method.invokeNative(Native Method)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at java.lang.reflect.Method.invoke(Method.java:511)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at dalvik.system.NativeStart.main(Native Method)
01-14 07:33:19.204: E/AndroidRuntime(22767): Caused by: java.lang.NullPointerException
01-14 07:33:19.204: E/AndroidRuntime(22767):    at packagename.Introduction_Activity.onCreate(Introduction_Activity.java:45)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at android.app.Activity.performCreate(Activity.java:5206)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
01-14 07:33:19.204: E/AndroidRuntime(22767):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
01-14 07:33:19.204: E/AndroidRuntime(22767):    ... 11 more

这是oncreate方法:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_introduction);
        View v = inflater.inflate(R.layout.image_view_layout, null);
        iv = (ImageView) v.findViewById(R.id.img_pager);


        zc = (ZoomControls) findViewById(R.id.zoomControls1);
        zc.setOnZoomInClickListener(new OnClickListener() {
            public void onClick(View v) {
                float oldZoom = currentZoom;
                currentZoom = (float) (currentZoom * 1.25);
                zc.setIsZoomOutEnabled(true);
                if (3.0 < currentZoom) {
                    zc.setIsZoomInEnabled(false);
                }
                scaleAnim = new ScaleAnimation(oldZoom, currentZoom, oldZoom, currentZoom, 0, 0);
                scaleAnim.setFillAfter(true);
                iv.startAnimation(scaleAnim);
            }
        });
        zc.setOnZoomOutClickListener(new OnClickListener() {
            public void onClick(View v) {
                float oldZoom = currentZoom;
                currentZoom = (float) (currentZoom / 1.25);
                zc.setIsZoomInEnabled(true);
                if (0.33 > currentZoom) {
                    zc.setIsZoomOutEnabled(false);
                }
                scaleAnim = new ScaleAnimation(oldZoom, currentZoom, oldZoom, currentZoom, 0, 0);
                scaleAnim.setFillAfter(true);
                iv.startAnimation(scaleAnim);
            }
        });

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.imgs_viewpager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPager.setCurrentItem(NUM_PAGES-1,false);    
    }

2 个答案:

答案 0 :(得分:3)

这样做

 LayoutInflater layoutInflater = = (LayoutInflater)  yourclasscontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View view = layoutInflater.inflate(R.layout.image_view_layout, this);
 ImageView iv = (ImageView) view .findViewById(R.id.img_pager);

您可以点击此链接Inflating and Encapsulating Layout XML into View Object in Android

答案 1 :(得分:1)

试试这个,

LinearLayout mAddOnForSaladLnrLyt = (LinearLayout) findViewById(R.id.your_layout_id);

LayoutInflater inflater = this.getLayoutInflater();

View v = inflater.inflate(R.layout.image_view_layout, null);
ImageView iv = (ImageView) view .findViewById(R.id.img_pager);
mAddOnForSaladLnrLyt.addView(v);