程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 安卓 運行 出錯-新手求大神幫助。仿照window7寫的圖片浏覽器,運行一直顯示空指針異常,求幫助,謝謝

安卓 運行 出錯-新手求大神幫助。仿照window7寫的圖片浏覽器,運行一直顯示空指針異常,求幫助,謝謝

編輯:編程解疑
新手求大神幫助。仿照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"
    />

最佳回答:


GridView gridview = (GridView) findViewById(R.id.gridview1);應該寫在setContentView(R.layout.activity_main);後面的

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved