我需要制作一个像图片库一样工作的小应用程序:我应该使用查看分页器,当用户向左或向右滑动时,应该从网址加载图片。我有一个带网址的数组,当用户滑动到新页面时它应该开始加载特定的图像(它不应该在开始时加载所有图像)。例如,如果我在第一页上,我应该看到来自url的第一个图像(数组中的索引0)。当我滑到第二页时,app应该开始从索引1加载图像,如果完成加载,它应该出现在屏幕上。
在运行我的应用程序时,我正在打算
11-06 07:47:43.108 9133-9133/com.example.prem.myapplication E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.prem.myapplication, PID: 9133
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)' on a null object reference
at com.example.prem.myapplication.ImageAdapter.instantiateItem(ImageAdapter.java:42)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:943)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1091)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1083)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17547)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
at android.view.View.measure(View.java:17547)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
主要活动
公共类MainActivity扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ArrayList<String> articleList = getArticleData();
ImageAdapter adapter = new ImageAdapter(this,articleList);
viewPager.setAdapter(adapter);
}
private ArrayList<String> getArticleData() {
ArrayList<String> articleList = new ArrayList<String>(Arrays.asList(MyArticleData.articles));
return articleList;
}
}
MyArticleData for Urls
public class MyArticleData {
static String[] articles = {
"https://api.learn2crack.com/android/images/donut.png",
"https://api.learn2crack.com/android/images/eclair.png",
"https://api.learn2crack.com/android/images/froyo.png",
"https://api.learn2crack.com/android/images/ginger.png",
"https://api.learn2crack.com/android/images/honey.png",
"https://api.learn2crack.com/android/images/icecream.png",
"https://api.learn2crack.com/android/images/jellybean.png",
"https://api.learn2crack.com/android/images/kitkat.png",
"https://api.learn2crack.com/android/images/lollipop.png",
"https://api.learn2crack.com/android/images/marshmallow.png",
"https://api.learn2crack.com/android/images/donut.png",
"https://api.learn2crack.com/android/images/eclair.png",
"https://api.learn2crack.com/android/images/froyo.png",
"https://api.learn2crack.com/android/images/ginger.png",
"https://api.learn2crack.com/android/images/honey.png",
"https://api.learn2crack.com/android/images/icecream.png",
"https://api.learn2crack.com/android/images/jellybean.png",
"https://api.learn2crack.com/android/images/kitkat.png",
"https://api.learn2crack.com/android/images/lollipop.png",
"https://api.learn2crack.com/android/images/marshmallow.png",
"https://api.learn2crack.com/android/images/donut.png",
"https://api.learn2crack.com/android/images/eclair.png",
"https://api.learn2crack.com/android/images/froyo.png",
"https://api.learn2crack.com/android/images/ginger.png",
"https://api.learn2crack.com/android/images/honey.png",
"https://api.learn2crack.com/android/images/icecream.png",
"https://api.learn2crack.com/android/images/jellybean.png",
"https://api.learn2crack.com/android/images/kitkat.png",
"https://api.learn2crack.com/android/images/lollipop.png",
"https://api.learn2crack.com/android/images/marshmallow.png"
};
}
ImageAdapter
public class ImageAdapter extends PagerAdapter {
Context context;
ArrayList<String> data = new ArrayList<>();
public LayoutInflater mLayoutInflater;
View imageView;
ImageAdapter(Context context, ArrayList<String> urls) {
this.context = context;
this.data = urls;
}
@Override
public int getCount() {
return 30;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView
imageView = (ImageView) mLayoutInflater.inflate(R.layout.pager_item, container, false);
//ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
// ImageView imageView = new ImageView(context);
// int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
// imageView.setPadding(padding, padding, padding, padding);
// imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//imageView.setImageResource(GalImages[position]);
Picasso.with(context).load(data.get(position)).into(imageView);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
activity_main.xml中
<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=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
pager_item.xml
<ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imageView" />
我是android.please帮助的新手 感谢
答案 0 :(得分:1)
try this
public class MyArticleData {
static String[] articles = {
"https://api.learn2crack.com/android/images/donut.png",
"https://api.learn2crack.com/android/images/eclair.png",
"https://api.learn2crack.com/android/images/froyo.png",
"https://api.learn2crack.com/android/images/ginger.png",
"https://api.learn2crack.com/android/images/honey.png",
"https://api.learn2crack.com/android/images/icecream.png",
"https://api.learn2crack.com/android/images/jellybean.png",
"https://api.learn2crack.com/android/images/kitkat.png",
"https://api.learn2crack.com/android/images/lollipop.png",
"https://api.learn2crack.com/android/images/marshmallow.png",
"https://api.learn2crack.com/android/images/donut.png",
"https://api.learn2crack.com/android/images/eclair.png",
"https://api.learn2crack.com/android/images/froyo.png",
"https://api.learn2crack.com/android/images/ginger.png",
"https://api.learn2crack.com/android/images/honey.png",
"https://api.learn2crack.com/android/images/icecream.png",
"https://api.learn2crack.com/android/images/jellybean.png",
"https://api.learn2crack.com/android/images/kitkat.png",
"https://api.learn2crack.com/android/images/lollipop.png",
"https://api.learn2crack.com/android/images/marshmallow.png",
"https://api.learn2crack.com/android/images/donut.png",
"https://api.learn2crack.com/android/images/eclair.png",
"https://api.learn2crack.com/android/images/froyo.png",
"https://api.learn2crack.com/android/images/ginger.png",
"https://api.learn2crack.com/android/images/honey.png",
"https://api.learn2crack.com/android/images/icecream.png",
"https://api.learn2crack.com/android/images/jellybean.png",
"https://api.learn2crack.com/android/images/kitkat.png",
"https://api.learn2crack.com/android/images/lollipop.png",
"https://api.learn2crack.com/android/images/marshmallow.png"
};
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ArrayList<String> articleList = getArticleData();
ImageAdapter adapter = new ImageAdapter(this,articleList);
viewPager.setAdapter(adapter);
}
private ArrayList<String> getArticleData() {
ArrayList<String> articleList = new ArrayList<String>(Arrays.asList(MyArticleData.articles));
return articleList;
}
}
public class ImageAdapter extends PagerAdapter {
Context context;
ArrayList<String> data = new ArrayList<String>;
ImageAdapter(Context context, ArrayList<String> urls) {
this.context = context;
this.data = urls;
}
@Override
public int getCount() {
return 30;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//imageView.setImageResource(GalImages[position]);
Picasso.with(context).load(data.get(position)).into(imageView);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
add this dependency in your build gradle.
compile 'com.squareup.picasso:picasso:2.3.2'
答案 1 :(得分:0)
为了从网址加载图片,您需要使用Picasso,这是一款功能强大的Android图像下载和缓存库。
Picasso.with(context).load("https://api.learn2crack.com/android/images/kitkat.png")
.into(imageView);