我是新來的Android,知道一點JAVA,但我想學習,我一直在做教程。我想要做的想法如下: 我有一個渲染的菜單,例如: 1. BIRDS 2. ROCKS 3.植物 當我按BIRDS我想顯示圖片和一個小的描述。圖像的id和我保存在xml中的描述。像這樣:Android Java重要異常java.lang.NullPointerException
<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));
,但我不太確定該怎麼做
UPDATE
其實我發現了問題:
在77號線,我初始化ImageResource大小爲20
Integer[] imageResource = new Integer[20];
但splited datastring剛剛4元
String [] datastring = dataList.split(Reg);
所以mImages變量
private Integer[] mImages = takePhotos();
將有20種元素,從其中16個是空 並在年底
i.setImageResource(mImages[position]);
它不能使null元素,並且墜毀。
所以有我有另外一個問題:
如果我不知道的大小更多鈔票整數[]我怎麼initiliaze和推動在它的元素?有名單?
錯誤標記在singleSignListItem的第130行,這是您發佈它的126行。哪一行代碼是130行? –
哦,我刪除了一些註釋行。所以現在錯誤在這裏:i.setImageResource(mImages [position]); – doomie
在該行上設置一個斷點並遍歷代碼,直到達到斷點。檢查mImages。多久了?職位的價值是什麼? – Simon