Domanda

I am calling a view from outside the layout of my activity so I am doing:

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

where R.layout.image_view_layout is the layout of the view to call, and R.id.img_pager is the view that I want to call but am getting nullpointer exception why?

this is my image_view_layout.xml file:

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

that's the 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

that's the oncreate method:

 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);    
    }
È stato utile?

Soluzione

Do it like this way

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

You can follow this link Inflating and Encapsulating Layout XML into View Object in Android

Altri suggerimenti

Try this,

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top