解决方案 »

  1.   

    R.layout.activity_main:
    <?xml version="1.0" encoding="utf-8"?>
    <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"
         >    <ViewSwitcher
            android:id="@+id/viewSwither"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
        
        <Button
            android:id="@+id/button_pre"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="prev"
            android:text="@string/pre"
            />    <Button
            android:id="@+id/button_next"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="next"
            android:text="@string/next"
            /> </RelativeLayout>R.layout.slidelistview
    <?xml version="1.0" encoding="utf-8"?>
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:verticalSpacing="5dp"
        android:gravity="center" 
        android:stretchMode="spacingWidth"
        android:numColumns="4">
      </GridView>R.layout.lableicon.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:gravity="center" >
        
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />"
        </LinearLayout>
    AndroidManifest.xml
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.learning.viewswitchertest"
        android:versionCode="1"
        android:versionName="1.0" >    <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="20" />    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application></manifest>
      

  2.   

    不是报了NullPointer 控制真错误吗?应该是你有变量没有初始化就直接用了,你点Logcat中的日志,跳转到错误位置就知道了12-26 14:54:45.214: E/AndroidRuntime(615): FATAL EXCEPTION: main
    12-26 14:54:45.214: E/AndroidRuntime(615): java.lang.NullPointerException
    12-26 14:54:45.214: E/AndroidRuntime(615):  at com.learning.viewswitchertest.MainActivity$1.getView(MainActivity.java:97)
      

  3.   

    应该是这里错了:
     private BaseAdapter adapter = new BaseAdapter() {               //动态Adapter
             
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                View view = convertView ;
                if(convertView == null){
                    view = inflater.inflate(R.layout.lableicon, null);  //  元素是name和drawable的线性布局lableicon
                }
                 
                ImageView imageView = (ImageView) view .findViewById(R.id.imageView);
                TextView textView = (TextView) view .findViewById(R.id.textView);
     
                imageView.setImageDrawable(getItem(position).drawable);
                textView.setText(getItem(position).dataName);
                 
                return view;
            }
             
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
             
            @Override
            public DataItem getItem(int position) {
                // TODO Auto-generated method stub
            return items.get(screenNum * NUM_PER_SCREEN + position);
     
            }
             
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                if(screenNum == screenCount-1 && items.size() % NUM_PER_SCREEN != 0 )
                    return items.size() % NUM_PER_SCREEN ;
                return NUM_PER_SCREEN; 
            }
        };
    }
      

  4.   


    定位到 原代码的97行:imageView.setImageDrawable(getItem(position).drawable);
    其中drawable在 现在的第23行 for循环中已经初始化了,还是找不到错
      

  5.   

    不好意思啊,之前看得太快没看出你加的View
    现在程序可以跑起来了但是gridView没有加载出图标,界面只有两个button,怀疑adapter中有问题,能帮我看看么谢谢
      

  6.   

    不好意思啊,之前看得太快没看出你加的View
    现在程序可以跑起来了但是gridView没有加载出图标,界面只有两个button,怀疑adapter中有问题,能帮我看看么谢谢
    也没有报错
      

  7.   

    没加载出图标你要确认你你的imageview是否有设置成功,跟日志看看。需要具体调试才知道是什么问题