从代码更改ImageView src时出现空指针异常

时间:2013-09-13 14:25:19

标签: android exception imageview

我正在尝试为ViewPageAdapter创建片段,以便为beetwin 6个不同的图像进行浏览 这是我的代码:

package com.example.newyorkfinal;

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.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.Toast;

public class CatalogActivity extends FragmentActivity {
    /**
     * The number of pages (wizard steps) to show in this demo.
     */
    private static final int NUM_PAGES = 6;

    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.catalog_pager);



        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);



    }

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            // If the user is currently looking at the first step, allow the system to handle the
            // Back button. This calls finish() on this activity and pops the back stack.
            super.onBackPressed();
        } else {
            // Otherwise, select the previous step.
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }

    /**
     * A simple pager adapter that represents 5 ScreenSlidePageFragment objects, in
     * sequence.
     */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {
            ImageView catalogImage;
            catalogImage = (ImageView) findViewById(R.id.catalogImageSlide);
            switch (position){
            case 0:
            {
                Toast.makeText(getBaseContext(), String.valueOf(position), 200).show();
                catalogImage.setImageResource(R.drawable.page_1_12_tel);
                break;
            }

        case 1:
        {
            Toast.makeText(getBaseContext(), String.valueOf(position++), 200).show();
            catalogImage.setImageResource(R.drawable.page_2_11);
            break;
        }
        case 2:
        {
            Toast.makeText(getBaseContext(), String.valueOf(position++), 200).show();
            catalogImage.setImageResource(R.drawable.page_3_10);
            break;
        }
        case 3:
        {
            Toast.makeText(getBaseContext(), String.valueOf(position++), 200).show();
            catalogImage.setImageResource(R.drawable.page_4_9);
            break;
        }
        case 4:
        {
            Toast.makeText(getBaseContext(), String.valueOf(position++), 200).show();
            catalogImage.setImageResource(R.drawable.page_5_8);
            break;
        }
        case 5:
        {
            Toast.makeText(getBaseContext(), String.valueOf(position++), 200).show();
            catalogImage.setImageResource(R.drawable.page_6_7);
            break;
        }}

            return new CatalogFragment() ;
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
}

我的交换机案例中有一个空指针错误,我想我在我的代码中错误的prace中定义了catalogImage,但我找不到答案

09-13 17:31:30.170: E/AndroidRuntime(17862): FATAL EXCEPTION: main
09-13 17:31:30.170: E/AndroidRuntime(17862): java.lang.NullPointerException
09-13 17:31:30.170: E/AndroidRuntime(17862):    at com.example.newyorkfinal.CatalogActivity$ScreenSlidePagerAdapter.getItem(CatalogActivity.java:81)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:105)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:829)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.support.v4.view.ViewPager.populate(ViewPager.java:979)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.View.measure(View.java:15855)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.View.measure(View.java:15855)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.View.measure(View.java:15855)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.View.measure(View.java:15855)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2472)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.View.measure(View.java:15855)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.Choreographer.doFrame(Choreographer.java:532)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.os.Handler.handleCallback(Handler.java:730)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.os.Looper.loop(Looper.java:137)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at android.app.ActivityThread.main(ActivityThread.java:5287)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at java.lang.reflect.Method.invokeNative(Native Method)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at java.lang.reflect.Method.invoke(Method.java:525)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
09-13 17:31:30.170: E/AndroidRuntime(17862):    at dalvik.system.NativeStart.main(Native Method)

catalog_slide.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@color/BurlyWood" >


    <ScrollView
    android:id="@+id/contentCatalog"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <ImageView
            android:id="@+id/catalogImageSlide"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:lineSpacingMultiplier="1.2"
            android:padding="16dp"
            android:src="@drawable/page_1_12_tel" />

</ScrollView>


</LinearLayout>

catalog_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

CatalogFragment.java

package com.example.newyorkfinal;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class CatalogFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.catalog_slide, container, false);
        return rootView;
    }
}

1 个答案:

答案 0 :(得分:2)

您应该向CatalogFragment添加一个参数,该参数接收需要加载的图像资源。并在CatalogFragment中加载imageview。

目前,您正在更改一些随机的imageview,然后加载CatalogFragment,它不知道ImageView,也不知道它要加载的资源。