我正在将正在运行的程序合并到另一个程序中。但是,我无法运行合并的代码。出现以下 logcat 错误
01-19 16:55:01.472: E/AndroidRuntime(1632): FATAL EXCEPTION: main
01-19 16:55:01.472: E/AndroidRuntime(1632): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exa_sales/com.example.exa_sales.ScrollGallery}: java.lang.NullPointerException
01-19 16:55:01.472: E/AndroidRuntime(1632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-19 16:55:01.472: E/AndroidRuntime(1632): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-19 16:55:01.472: E/AndroidRuntime(1632): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-19 16:55:01.472: E/AndroidRuntime(1632): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-19 16:55:01.472: E/AndroidRuntime(1632): at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 16:55:01.472: E/AndroidRuntime(1632): at android.os.Looper.loop(Looper.java:123)
01-19 16:55:01.472: E/AndroidRuntime(1632): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-19 16:55:01.472: E/AndroidRuntime(1632): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 16:55:01.472: E/AndroidRuntime(1632): at java.lang.reflect.Method.invoke(Method.java:521)
01-19 16:55:01.472: E/AndroidRuntime(1632): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-19 16:55:01.472: E/AndroidRuntime(1632): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-19 16:55:01.472: E/AndroidRuntime(1632): at dalvik.system.NativeStart.main(Native Method)
01-19 16:55:01.472: E/AndroidRuntime(1632): Caused by: java.lang.NullPointerException
01-19 16:55:01.472: E/AndroidRuntime(1632): at com.example.exa_sales.ScrollGallery.onCreate(ScrollGallery.java:28)
01-19 16:55:01.472: E/AndroidRuntime(1632): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 16:55:01.472: E/AndroidRuntime(1632): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-19 16:55:01.472: E/AndroidRuntime(1632): ... 11 more
这是调用 Gallery.java 类的类,我放置了一个 Gallery 按钮,并在 OnClickListener 上调用了图库类
gformbtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Log.v("Gallery","Gallery");
Intent i = new Intent(MainActivity.this,ScrollGallery.class);
startActivity(i);
}
});
这是图库类,一旦用户单击图库按钮,就会重定向到此类并显示图像
public class ScrollGallery extends Activity {
private Gallery gallery;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(ScrollGallery.this, "Position=" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
private Integer[] Imgid = {
R.drawable.pic_1,R.drawable.pic_2,R.drawable.pic_3,R.drawable.pic_4,R.drawable.pic_5,R.drawable.pic_6,R.drawable.pic_7,R.drawable.pic_8,R.drawable.pic_9,
R.drawable.pic_10,R.drawable.pic_11,R.drawable.pic_12,R.drawable.pic_13,R.drawable.pic_14
};
/*
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
*/
public AddImgAdp(Context c) {
cont = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
GalItemBg = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return Imgid.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 imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
}