新手求大神帮助。仿照window7写的图片浏览器,运行一直显示空指针异常,求帮助,谢谢

package com.xpc.picture;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout.LayoutParams;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

/*
*一直都是空指针异常,求大神指点一点
*/

public class MainActivity extends Activity {

private int[] imageId = new int[]{R.drawable.a,R.drawable.b,R.drawable.c,
        R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,
        R.drawable.i};//定义并初始化保存图片IDde 数组
private ImageSwitcher imageSwitcher;  //申明一个图像切换器对象

GridView gridview = (GridView) findViewById(R.id.gridview1);

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

    imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));//设置淡入动画
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));//淡出效果
    imageSwitcher.setFactory(new ViewFactory() {

        @Override
        public View makeView() {
            // TODO Auto-generated method stub
            ImageView imageView = new ImageView(MainActivity.this);//实例化一个ImageView类的对象
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));//设置保持纵横比居中缩放图像
            //是否导包错误???这是要导什么包,太多了

            return imageView;
        }
    });     
    imageSwitcher.setImageResource(imageId[6]);

    BaseAdapter adapter = new BaseAdapter(){

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return imageId.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ImageView imageview;
            if(convertView==null){
                imageview = new ImageView(MainActivity.this);
                imageview.setAdjustViewBounds(true);
                imageview.setMaxWidth(150);//设置长度宽度
                imageview.setMaxHeight(113);
                imageview.setPadding(5,5, 5,5);//设置内边距
            }else{
                imageview=(ImageView) convertView;
            }
            imageview.setImageResource(imageId[position]);//设置要显示的图片
            return imageview;
        }

    };

    gridview.setAdapter(adapter);
    gridview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            imageSwitcher.setImageResource(imageId[position]);

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

布局文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<GridView 
    android:id="@+id/gridview1"
    android:layout_height="match_parent"
    android:layout_width="640dp"
    android:layout_marginTop="10dp"
    android:horizontalSpacing="3dp"
    android:verticalSpacing="3dp"
    android:numColumns="4"
    />
<ImageSwitcher 
    android:id="@+id/imageSwitcher1"
    android:padding="20dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

3个回答

GridView gridview = (GridView) findViewById(R.id.gridview1);应该写在setContentView(R.layout.activity_main);后面的

GridView gridview = (GridView) findViewById(R.id.gridview1);怎么写在了onCreate的外面

谢谢,已经解决了。。。。。。。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问