我是Android新手,知道一点JAVA,但我想学习并继续做教程。我想做的是以下几点: 我有一个渲染菜单,例如: 1.鸟 2. ROCKS 3.植物 当我按BIRDS时,我想要显示图片和一个小描述。我保存在xml中的图像的id和描述。像这样:
<signs>
<sign id="1_1" category="1">
<name>desc1</name>
</sign>
<sign id="1_2" category="1">
<name>desc2</name>
</sign>
<sign id="1_3_1" category="1">
<name>desc3</name>
</sign>
<sign id="1_3_2" category="1">
<name>desc4</name>
</sign>
</signs>
图片就像draw_1中的sign_1_1.png,sign_1_2.png一样。
我制作了画廊,它正在显示,描述和图像也是可见的。我在图库上选中了图片,在TextView中显示相应的描述。 但是当我点击时,我得到一个致命的例外:
E/AndroidRuntime(22141): FATAL EXCEPTION: main
E/AndroidRuntime(22141): java.lang.NullPointerException
E/AndroidRuntime(22141): at apcmag.examples.singleSignListItem$ImageAdapter.getView(singleSignListItem.java:117)
E/AndroidRuntime(22141): at android.widget.Gallery.makeAndAddView(Gallery.java:849)
E/AndroidRuntime(22141): at android.widget.Gallery.fillToGalleryRightLtr(Gallery.java:803)
E/AndroidRuntime(22141): at android.widget.Gallery.fillToGalleryRight(Gallery.java:747)
E/AndroidRuntime(22141): at android.widget.Gallery.layout(Gallery.java:656)
E/AndroidRuntime(22141): at android.widget.Gallery.onLayout(Gallery.java:351)
E/AndroidRuntime(22141): at android.view.View.layout(View.java:13754)
E/AndroidRuntime(22141): at android.view.ViewGroup.layout(ViewGroup.java:4362)
代码是:
package apcmag.examples;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class singleSignListItem extends Activity
{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.single_sign_gallery);
Gallery g = (Gallery) findViewById(R.id.gallery);
final Intent i = getIntent();
final String REGEX = "/%%/";
String product = i.getStringExtra("product");
setTitle(product);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String [] products = i.getStringExtra("product_text").split(REGEX);
Toast.makeText(singleSignListItem.this, ""+position, Toast.LENGTH_SHORT).show();
TextView show_intro = (TextView) findViewById(R.id.show_intro);
show_intro.setText(""+products[position]);
}
public void onNothingSelected(AdapterView<?> parent)
{
// TODO Auto-generated method stub
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
// private Integer[] mImageIds = {
// R.drawable.sign_1_1,
// R.drawable.sign_1_1,
// R.drawable.sign_1_1,
// R.drawable.sign_1_1
// };
private Integer[] mImages = takePhotos();
public Integer[] takePhotos (){
Intent g = getIntent();
String Reg = "/%%/";
String Reg2 = "_%_";
String dataList = g.getStringExtra("product_text");
String [] datastring = dataList.split(Reg);
Integer[] imageResource = new Integer[20];
String[] dd = null;
for(int k = 0; k<datastring.length;k++){
dd = datastring[k].split(Reg2);
String imagename = "sign_"+dd[0];
imageResource[k] = getResources().getIdentifier(imagename, "drawable", getPackageName());
}
return imageResource;
}
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImages.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImages[position]);
i.setLayoutParams(new Gallery.LayoutParams(115, 200));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
}
我无法得到问题几个小时,我有一些想法可能在这里:
i.setLayoutParams(new Gallery.LayoutParams(115, 200));
但我不确定该怎么做
更新
实际上我发现了问题:
在第77行,我正在初始化大小为20的ImageResource
Integer[] imageResource = new Integer[20];
但是分裂的数据串只有4个元素
String [] datastring = dataList.split(Reg);
所以mImages变量
private Integer[] mImages = takePhotos();
将有20个元素,其中16个为null 最后
i.setImageResource(mImages[position]);
它无法呈现null元素并且崩溃了。
所以我还有另一个问题:
如果我不知道可能的整数[]的大小,我该如何初始化并推入它?有了List?
答案 0 :(得分:1)
实际上我发现了问题:
在第77行,我正在初始化大小为20的ImageResource
Integer [] imageResource = new Integer [20];
但是分裂的数据串只有4个元素
String [] datastring = dataList.split(Reg);
所以mImages变量
private Integer[] mImages = takePhotos();
将有20个元素,其中16个为空,最后为
i.setImageResource(mImages[position]);
它无法呈现null元素并且崩溃了。
答案 1 :(得分:0)
public Object getItem(int position){返回位置; public long getItemId(int position){返回位置;检查返回类型。我认为两者都是int。
答案 2 :(得分:-1)
此行中有错误:
for(int k = 0; k<datastring.length;k++){
dd = datastring[k].split(Reg2);
我认为这样做会为你做这件事:
dd = datastring.split(Reg2);
答案 3 :(得分:-2)
我认为问题是String [] dd = null;码。你必须初始化dd变量