如何在PageAdapter类中以编程方式更改数组?
这是代码片段:
public class ViewPagerActivity extends Activity {
public int[] imgs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
String id = b.getString("id");
if (id == "1") {
int[] imgs = { R.drawable.image1 };
} else {
int[] imgs = { R.drawable.image2 };
}
ViewPager.setAdapter(new SamplePagerAdapter(imgs));
}
class SamplePagerAdapter extends PagerAdapter {
int[] myDrawables;
public SamplePagerAdapter(int[] img) {
this.myDrawables = img;
}
}
}
当我将 imgs 分配给 this.myDrawables 时,我收到错误:
04-16 11:03:34.823: E/AndroidRuntime(6222): FATAL EXCEPTION: main
04-16 11:03:34.823: E/AndroidRuntime(6222): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.catalog/com.catalog.ViewPagerActivity}: java.lang.NullPointerException
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.app.ActivityThread.access$700(ActivityThread.java:140)
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.os.Looper.loop(Looper.java:137)
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.app.ActivityThread.main(ActivityThread.java:4921)
04-16 11:03:34.823: E/AndroidRuntime(6222): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 11:03:34.823: E/AndroidRuntime(6222): at java.lang.reflect.Method.invoke(Method.java:511)
04-16 11:03:34.823: E/AndroidRuntime(6222): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-16 11:03:34.823: E/AndroidRuntime(6222): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
04-16 11:03:34.823: E/AndroidRuntime(6222): at dalvik.system.NativeStart.main(Native Method)
04-16 11:03:34.823: E/AndroidRuntime(6222): Caused by: java.lang.NullPointerException
04-16 11:03:34.823: E/AndroidRuntime(6222): at com.catalog.ViewPagerActivity$SamplePagerAdapter.getCount(ViewPagerActivity.java:216)
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:434)
04-16 11:03:34.823: E/AndroidRuntime(6222): at com.catalog.ViewPagerActivity.onCreate(ViewPagerActivity.java:87)
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.app.Activity.performCreate(Activity.java:5206)
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-16 11:03:34.823: E/AndroidRuntime(6222): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
04-16 11:03:34.823: E/AndroidRuntime(6222): ... 11 more
但是当我在SamplePageAdapter中声明我的数组时,一切正常:
class SamplePagerAdapter extends PagerAdapter {
int[] myDrawables = {R.drawable.image1};
public SamplePagerAdapter(int[] img) {
//this.myDrawables = img; - HOW TO DO IT HERE from "img" argument?
}
}
我该如何动态完成?
答案 0 :(得分:0)
因此,无法将该简单方法数组分配给数组。我需要设置新数组的长度并按元素分配。