我正在尝试创建一个具有GridView
的应用,并且可以从GridView
中选择一个图像并将其显示在图像切换器中。有两个文件。布局文件和MainActivity。
这是MainActivity:
package com.example.grid;
import android.content.Context;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
public class MainActivity extends AppCompatActivity {
ImageSwitcher imgSwitcher = (ImageSwitcher) findViewById(R.id.imgSwitcher);
//---the images to display---
Integer[] imageIDs = {
R.mipmap.butterfly,
R.mipmap.windows,
R.mipmap.ic_launcher
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgSwitcher.setImageResource(imageIDs[position]);
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageAdapter(Context c) { context = c; }
//---returns the number of images---
public int getCount() { return imageIDs.length; }
//---returns the item---
public Object getItem(int position) { return position; }
//---returns the ID of an item---
public long getItemId(int position) { return position; }
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIDs[position]);
return imageView;
}
}
}
这是我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.grid.MainActivity">
<GridView
android:id="@+id/gridview"
android:layout_width="384dp"
android:layout_height="511dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ImageSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/gridview"
android:id="@+id/imgSwitcher"
/>
</android.support.constraint.ConstraintLayout>
我遇到了错误:
由于:java.lang.NullPointerException:尝试调用虚拟 方法'android.view.Window $ Callback 空对象引用上的android.view.Window.getCallback()'
当我未声明ImageSwitcher imgSwitcher并将其保留为空时,从GridView
中选择图像时会出现空指针异常。