ヌルポインター例外エラーが発生し続けますが、修正方法がわかりません。以下は私の丸太猫です。エラーは Second.java の 53 行目にあると思います。ログの猫情報の下には、私の Second.java ファイルからの私のコードがあります。このエラーを修正するために誰でも提供できるヘルプに感謝します。
10-11 13:39:10.340: E/AndroidRuntime(7905): FATAL EXCEPTION: main
10-11 13:39:10.340: E/AndroidRuntime(7905): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ebonybutler.cammct/com.ebonybutler.cammct.Second}: java.lang.NullPointerException
10-11 13:39:10.340: E/AndroidRuntime(7905): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1702)
10-11 13:39:10.340: E/AndroidRuntime(7905): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1722)
10-11 13:39:10.340: E/AndroidRuntime(7905): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
10-11 13:39:10.340: E/AndroidRuntime(7905): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:974)
10-11 13:39:10.340: E/AndroidRuntime(7905): at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 13:39:10.340: E/AndroidRuntime(7905): at android.os.Looper.loop(Looper.java:130)
10-11 13:39:10.340: E/AndroidRuntime(7905): at android.app.ActivityThread.main(ActivityThread.java:3821)
10-11 13:39:10.340: E/AndroidRuntime(7905): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 13:39:10.340: E/AndroidRuntime(7905): at java.lang.reflect.Method.invoke(Method.java:507)
10-11 13:39:10.340: E/AndroidRuntime(7905): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-11 13:39:10.340: E/AndroidRuntime(7905): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-11 13:39:10.340: E/AndroidRuntime(7905): at dalvik.system.NativeStart.main(Native Method)
10-11 13:39:10.340: E/AndroidRuntime(7905): Caused by: java.lang.NullPointerException
10-11 13:39:10.340: E/AndroidRuntime(7905): at com.ebonybutler.cammct.Second.onCreate(Second.java:53)
10-11 13:39:10.340: E/AndroidRuntime(7905): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-11 13:39:10.340: E/AndroidRuntime(7905): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1666)
10-11 13:39:10.340: E/AndroidRuntime(7905): ... 11 more
そして、ここに私のコードがあります:
package com.ebonybutler.cammct;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.net.Uri;
import android.provider.MediaStore;
public class Second extends Activity {
//variable for selection intent
private final int PICKER = 1;
//variable to store the currently selected image
private int currentPic = 0;
//adapter for gallery view
private PicAdapter imgAdapt;
//gallery object
private Gallery picGallery;
//image view for larger display
private ImageView picView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get the large image view
picView = (ImageView) findViewById(R.id.picture);
//get the gallery view
picGallery = (Gallery) findViewById(R.id.gallery);
//create a new adapter
imgAdapt = new PicAdapter(this);
//set the gallery adapter
picGallery.setAdapter(imgAdapt);
//set long click listener for each gallery thumbnail item
picGallery.setOnItemLongClickListener(new OnItemLongClickListener()
{
//handle long clicks
public boolean onItemLongClick(AdapterView<?> parent, View v,
int position, long id) {
//update the currently selected position so that we assign the imported bitmap to correct item
currentPic = position;
//take the user to their chosen image selection app (gallery or file manager)
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(pickIntent, "Select Picture"), PICKER);
return true;
}
});
//set the click listener for each item in the thumbnail gallery
picGallery.setOnItemClickListener(new OnItemClickListener() {
//handles clicks
public void onItemClick(AdapterView<?> parent, View v, int
position, long id) {
//set the larger image view to display the chosen bitmap calling method of adapter class
picView.setImageBitmap(imgAdapt.getPic(position));
}
});
}
//Base Adapter subclass creates Gallery view
public class PicAdapter extends BaseAdapter {
//use the default gallery background image
int defaultItemBackground;
//gallery context
private Context galleryContext;
//array to store bitmaps to display
private Bitmap[] imageBitmaps;
//place holder bitmap for empty spaces in gallery
Bitmap placeholder;
//constructor
public PicAdapter(Context c){
//instantiate context
galleryContext = c;
//create bitmap array
imageBitmaps = new Bitmap[10];
//decode the place holder image
placeholder = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//more processing
//set place holder as all thumb nail images in the gallery initially
for(int i=0; i<imageBitmaps.length; i++)
imageBitmaps[i]= placeholder;
//get the styling attributes - use default Android system resources
TypedArray styleAttrs = galleryContext.obtainStyledAttributes(R.styleable.PicGallery);
//get the background resource
defaultItemBackground = styleAttrs.getResourceId(R.styleable.PicGallery_android_galleryItemBackground, 0);
//recycle attributes
styleAttrs.recycle();
}
//BaseAdapter methods
//return number of data items i.e. bitmap images
public int getCount() {
return imageBitmaps.length;
}
//return item t specified position
public Object getItem(int position) {
return position;
}
//return item ID at specified position
public long getItemId(int position) {
return position;
}
//get view specifies layout and display options for each thumb nail in the gallery
public View getView(int position, View convertView, ViewGroup parent) {
//create the view
ImageView imageView = new ImageView(galleryContext);
//specify the bitmap at this position in the array
imageView.setImageBitmap(imageBitmaps[position]);
//set layout options
imageView.setLayoutParams(new Gallery.LayoutParams(300, 200));
//scale type within view area
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
//set default gallery item background
imageView.setBackgroundResource(defaultItemBackground);
//return the view
return imageView;
}
//custom methods for this app
//helper method to add a bitmap to the gallery when the user choose one
public void addPic(Bitmap newPic)
{
//set at currently selected index
imageBitmaps[currentPic] = newPic;
}
//return bitmap at specified position for larger display
public Bitmap getPic(int posn)
{
//return bitmap at posn index
return imageBitmaps[posn];
}
}
//Handle returning from gallery or file manager image selection
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
//check if we are returning from picture selection
if(requestCode == PICKER) {
//the returned picture URI
Uri pickedUri = data.getData();
//declare the bitmap
Bitmap pic = null;
//declare the path string
String imgPath = "";
//retrieve the string using media data
String[] medData = { MediaStore.Images.Media.DATA };
//query the data
Cursor picCursor = managedQuery(pickedUri, medData, null, null, null);
if(picCursor!=null)
{
//get the path string
int index = picCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
picCursor.moveToFirst();
imgPath = picCursor.getString(index);
}
else
imgPath = pickedUri.getPath();
//if and ese handle both choosing from gallery and from file manager
//if we have a new URI attempt to decode the image itmap
if(pickedUri!=null) {
//set the width and height we want to use as maximum display
int targetWidth = 600;
int targetHeight = 400;
//sample the incoming image to save on memory resources
//create bitmap options to calculate and use sample size
BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
//first decode image dimensions only - not the image bitmap itself
bmpOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imgPath, bmpOptions);
//work out what the sample size should be
//image width and height before sampling
int currHeight = bmpOptions.outHeight;
int currWidth = bmpOptions.outWidth;
//variable to store new sample size
int sampleSize = 1;
//calculate the sample size if the existing size is larger than target size
if (currHeight>targetHeight || currWidth>targetWidth)
{
//use either width of height
if (currWidth>currHeight)
sampleSize = Math.round((float)currHeight/(float)targetHeight);
else
sampleSize = Math.round((float)currWidth/(float)targetWidth);
}
//use the new sample size
bmpOptions.inSampleSize = sampleSize;
//now decode the bitmap using sample options
bmpOptions.inJustDecodeBounds = false;
//get the file as a bitmap
pic = BitmapFactory.decodeFile(imgPath, bmpOptions);
//pass bitmap to ImageAdapter to add to array
imgAdapt.addPic(pic);
//redraw the gallery thumbnails to reflect the new addition
picGallery.setAdapter(imgAdapt);
//display the newly selected image at larger size
picView.setImageBitmap(pic);
//scale options
picView.setScaleType(ImageView.ScaleType.FIT_CENTER);
}
}
}
//superclass method
super.onActivityResult(requestCode, resultCode, data);
}
}
xml は次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="@string/select_intro"
android:gravity="center"
android:textStyle="bold"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:textStyle="italic"
android:text="@string/show_intro"/>
<ImageView
android:id="@+id/picture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="@string/picture"/>
</LinearLayout>