我想将数据库中的图像显示到我正在使用viewpager的imageslider中。当我将图像放在drawable中时,代码工作正常。但是,当我从数据库中获取图像时,应用程序崩溃了。请查看我的代码并帮助我
SliderActivity.java
public class SliderActivity extends Activity{
JSONObject jsonobject;
JSONArray jsonarray;
String[] imageurl;
ProgressDialog mProgressDialog;
ViewPager mPager;
CirclePageIndicator indicator;
ArrayList<String> imageList ;
ArrayList<String> ImagesArray;
int i;
/*private static final Integer[] IMAGES= {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4};
private ArrayList<Integer> ImagesArray = new ArrayList<Integer>();*/
private static int currentPage = 0;
private static int NUM_PAGES = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slider);
new DownloadJSON().execute();
mPager = (ViewPager) findViewById(R.id.pager);
CirclePageIndicator indicator = (CirclePageIndicator)findViewById(R.id.indicator);
// init();
}
class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
// Retrieve JSON Objects from the given URL address
jsonobject = JSONFunctions.getJSONfromURL("http://192.168.10.63/get_for_slider.php");
try{
jsonarray = jsonobject.getJSONArray("images");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
String s = jsonobject.getString("image1");
Log.e("",s);
String s1 = jsonobject.getString("image2");
String s2 = jsonobject.getString("image3");
imageurl = new String[]{
s,s1,s2 } ;
}
}
catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args) {
for(i=0;i<imageurl.length;i++)
ImagesArray.add(imageurl[i]);
Log.e("post block","" + imageurl[i]);
mPager.setAdapter(new SlidingAdapterActivity(SliderActivity.this,ImagesArray));
indicator.setViewPager(mPager);
final float density = getResources().getDisplayMetrics().density;
//Set circle indicator radius
indicator.setRadius(3 * density);
NUM_PAGES =imageurl.length;
Log.e("","" + NUM_PAGES);
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
public void run() {
handler.post(Update);
}
}, 3000, 3000);
// Pager listener over indicator
indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int pos) {
}
});
}
}
}
我的Adapterclass SliderAdapterActivity.java
public class SlidingAdapterActivity extends PagerAdapter {
private ArrayList<String> IMAGES;
private LayoutInflater inflater;
private Context context;
public SlidingAdapterActivity(Context context,ArrayList<String> IMAGES) {
this.context = context;
this.IMAGES=IMAGES;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return IMAGES.size();
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
View imageLayout = inflater.inflate(R.layout.activity_sliding_adapter, view, false);
assert imageLayout != null;
final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.slide_image);
Picasso.with(context).load(IMAGES.get(position)).into(imageView);
// imageView.setImageResource(IMAGES.get(position));
view.addView(imageLayout, 0);
return imageLayout;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
Logcat:
05-11 12:36:24.347: E/(740): image1.jpg
05-11 12:36:24.367: E/AndroidRuntime(740): FATAL EXCEPTION: main
05-11 12:36:24.367: E/AndroidRuntime(740): java.lang.NullPointerException
05-11 12:36:24.367: E/AndroidRuntime(740): at com.example.imageslider.SliderActivity$DownloadJSON.onPostExecute(SliderActivity.java:83)
05-11 12:36:24.367: E/AndroidRuntime(740): at com.example.imageslider.SliderActivity$DownloadJSON.onPostExecute(SliderActivity.java:1)
05-11 12:36:24.367: E/AndroidRuntime(740): at android.os.AsyncTask.finish(AsyncTask.java:602)
05-11 12:36:24.367: E/AndroidRuntime(740): at android.os.AsyncTask.access$600(AsyncTask.java:156)
05-11 12:36:24.367: E/AndroidRuntime(740): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
05-11 12:36:24.367: E/AndroidRuntime(740): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 12:36:24.367: E/AndroidRuntime(740): at android.os.Looper.loop(Looper.java:137)
05-11 12:36:24.367: E/AndroidRuntime(740): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-11 12:36:24.367: E/AndroidRuntime(740): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 12:36:24.367: E/AndroidRuntime(740): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 12:36:24.367: E/AndroidRuntime(740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-11 12:36:24.367: E/AndroidRuntime(740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-11 12:36:24.367: E/AndroidRuntime(740): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
更改onPostExecute方法:
protected void onPostExecute(Void args) {
ImagesArray = new ArrayList<String>();
for(i=0;i<imageurl.length;i++)
ImagesArray.add(imageurl[i]);
Log.e("post block","" + imageurl[i]);
mPager.setAdapter(new SlidingAdapterActivity(SliderActivity.this,ImagesArray));
indicator.setViewPager(mPager);
final float density = getResources().getDisplayMetrics().density;
//Set circle indicator radius
indicator.setRadius(3 * density);
NUM_PAGES =imageurl.length;
Log.e("","" + NUM_PAGES);
// Auto start of viewpager
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == NUM_PAGES) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
public void run() {
handler.post(Update);
}
}, 3000, 3000);
// Pager listener over indicator
indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
currentPage = position;
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int pos) {
}
});
}
答案 1 :(得分:0)
嘿,请初始化数组List
ArrayList<String> ImagesArray;
所以它的show null指针。
答案 2 :(得分:0)
在onPostExecute...
protected void onPostExecute(Void args) {
ImagesArray = new ArrayList<String>();//Initialisation than add items
for(i=0;i<imageurl.length;i++)
ImagesArray.add(imageurl[i]);