2016-11-05 88 views
-2

我需要製作一個像圖片庫一樣工作的小應用程序:我應該使用視圖傳呼機,當用戶向左或向右滑動時,應用程序應該從url加載圖像。我有一個數組與網址,它應該開始加載特定的圖像時,用戶刷新到新頁面(它不應該加載所有圖像的開始)。例如,如果我在第一頁上,我應該看到來自url的第一個圖像(數組中的索引0)。當我滑動到第二頁時,應用程序應該從索引1開始加載圖像,並且當完成加載時,它應該出現在屏幕上。使用viewpager的圖像幻燈片

在運行過程中我的應用我正在geeting

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; 
} 

}

MyAr ticleData針對URL

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

爲了從URL加載圖像,你需要使用畢加索,一款功能強大的Android圖片下載和緩存庫。 Picasso.with(context).load("https://api.learn2crack.com/android/images/kitkat.png") .into(imageView);

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'