コンパイル時にエラーは発生しませんが、gridview項目をクリックするとアプリケーションが破損して、pagerviewアクティビティに移動します。ここで画像URLからViewPagerを使用するとクラッシュする
はViewpagerのための活動のための完全なコードです:
package com.example.samer.applicationformatech.activities;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.example.samer.applicationformatech.R;
import com.example.samer.applicationformatech.adapters.ImagePagerAdapter;
import java.util.ArrayList;
public class BirdsGalleryActivity extends AppCompatActivity {
private String img0=getIntent().getExtras().getString("IMG0");
private String img1=getIntent().getExtras().getString("IMG1");
private String img2=getIntent().getExtras().getString("IMG2");
private String img3=getIntent().getExtras().getString("IMG3");
private String img4=getIntent().getExtras().getString("IMG4");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pagerview);
final ArrayList<String> birds = new ArrayList<>();
birds.add(new String(img0));
birds.add(new String(img1));
birds.add(new String(img2));
birds.add(new String(img3));
birds.add(new String(img4));
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter(this,birds);
viewPager.setAdapter(adapter);
}
}
そして、アダプタクラス:
package com.example.samer.applicationformatech.adapters;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.example.samer.applicationformatech.R;
import com.example.samer.applicationformatech.activities.BirdsGalleryActivity;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class ImagePagerAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
ArrayList<String> arrayList;
public ImagePagerAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.arrayList = arrayList;
}
public ImagePagerAdapter(BirdsGalleryActivity birdsGalleryActivity) {
}
@Override
public int getCount() {
if(arrayList != null){
return arrayList.size();
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = layoutInflater.inflate(R.layout.image_viewpager_layout, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.viewPagerItem_image1);
Picasso.with(context).load(arrayList.get(position));
// .placeholder(R.drawable.image_uploading)
// .error(R.drawable.image_not_found).into(imageView);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
XMLS:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.samer.applicationformatech.activities.BirdsGalleryActivity"
>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
と
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/viewPagerItem_image1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
理由は何ですか?
は、私が試した「クラッシュ」 – lelloman