情况如何? 我在ViewPager中有4个片段,它们看起来完全相同。但我想要实现的是使每个片段不同(在更改ImageView和相关文本方面)。 但是,我不知道在哪里放置代码来更改ImageViewResource,因为它总是说我正在尝试调用null对象引用上的方法。所以直截了当地说,我必须在哪里放置代码行来更改Image资源(不是在java中的xml中),这样我就不会得到错误。如果您需要进一步澄清,请随时提出。任何帮助表示赞赏。
爪哇: 包含ViewPager的活动
package com.mediatechindo.wahyu.materialuikit.template.WalktroughCategory.Style14;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.mediatechindo.wahyu.materialuikit.BuildConfig;
import com.mediatechindo.wahyu.materialuikit.R;
public class WalkthroughStyle14Activity extends AppCompatActivity implements View.OnClickListener {
private ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter = null;
private View indicator1;
private View indicator2;
private View indicator3;
private View indicator4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.walkthrough14_layout);
indicator1 = findViewById(R.id.indicator1);
indicator2 = findViewById(R.id.indicator2);
indicator3 = findViewById(R.id.indicator3);
indicator4 = findViewById(R.id.indicator4);
//Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(new WizardPageChangeListener());
View image = findViewById(R.id.activity_walkthrough_style14);
String url = BuildConfig.IMAGE_URL + "walkthrough/style-14/Welcome-14-960.jpg";
String urlThumb = BuildConfig.IMAGE_URL + "walkthrough/style-14/Welcome-14-960-thumb.jpg";
loadImageRequest(image, url, urlThumb);
updateIndicators(0);
}
private void loadImageRequest(final View bg, String url, String urlThumb) {
DrawableRequestBuilder<String> thumbnailRequest = Glide
.with(this)
.load(urlThumb);
Glide.with(this)
.load(url)
.thumbnail(thumbnailRequest)
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
bg.setBackground(resource);
}
});
}
//Implementation of PagerAdapter that represents each page as a Fragment that is persistently kept in the fragment manager as long as the user can return to the page.
//Class that holds all the frames to swipe through
private class ViewPagerAdapter extends FragmentPagerAdapter {
private int WIZARD_PAGES_COUNT = 4;
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
WalkthroughStyle14Fragment walkthroughStyle14Fragment = new WalkthroughStyle14Fragment(position);
switch (position){
case 1:
walkthroughStyle14Fragment.imageView.setImageResource(R.drawable.ic_store);
break;
default:
break;
}
return walkthroughStyle14Fragment;
}
@Override
public int getCount() {
return WIZARD_PAGES_COUNT;
}
}
//Change listener to call action when page is changed
private class WizardPageChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int position) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
updateIndicators(position);
}
}
//Method used to change the color of the dots when page is changed
public void updateIndicators(int position) {
switch (position) {
case 0:
indicator1.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot));
indicator2.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
indicator3.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
indicator4.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
break;
case 1:
indicator1.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
indicator2.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot));
indicator3.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
indicator4.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
break;
case 2:
indicator1.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
indicator2.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
indicator3.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot));
indicator4.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
break;
case 3:
indicator1.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
indicator2.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
indicator3.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot_grey));
indicator4.setBackground(ContextCompat.getDrawable(this, R.drawable.ic_dot));
break;
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnLoginSignupBack:
onBackPressed();
break;
case R.id.btnSignIn:
Toast.makeText(this, "Button Sign In clicked!", Toast.LENGTH_SHORT).show();
break;
case R.id.btnSignUp:
Toast.makeText(this, "Button Sign Up clicked!", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
包含我现在尝试更改图像资源的片段的活动:
package com.mediatechindo.wahyu.materialuikit.template.WalktroughCategory.Style14;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.mediatechindo.wahyu.materialuikit.R;
/**
* Created by wahyu on 15/11/16.
*/
@SuppressLint("ValidFragment")
public class WalkthroughStyle14Fragment extends Fragment {
int wizard_page_position;
public ImageView imageView = null;
public WalkthroughStyle14Fragment(int position) {
this.wizard_page_position = position;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
View view = inflater.inflate(R.layout.walkthrough14_fragment, container, false);
imageView = (ImageView) view.findViewById(R.id.imagePage1);
imageView.setImageResource(R.drawable.ic_store);
return view;
}
}
片段布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_walkthrough_style14"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/walkthrough14Background"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_marginBottom="110dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="124dp"
android:animateLayoutChanges="true"
android:gravity="center"
android:orientation="horizontal">
<View
android:id="@+id/indicator1"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="2dp"
android:background="@drawable/ic_dot_grey" />
<View
android:id="@+id/indicator2"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="2dp"
android:background="@drawable/ic_dot_grey" />
<View
android:id="@+id/indicator3"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="2dp"
android:background="@drawable/ic_dot_grey" />
<View
android:id="@+id/indicator4"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="2dp"
android:background="@drawable/ic_dot_grey" />
</LinearLayout>
<LinearLayout
android:id="@+id/buttonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/walkthrough4Background"
android:orientation="horizontal">
<Button
android:id="@+id/btnSignUp"
style="@style/ButtonWhite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="SIGN UP" />
<Button
android:id="@+id/btnSignIn"
style="@style/ButtonMagenta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="SIGN IN"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
错误堆栈跟踪
06-26 16:53:55.397 7240-7240/com.mediatechindo.wahyu.materialuikit E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mediatechindo.wahyu.materialuikit, PID: 7240
Theme: themes:{default=overlay:com.cyngn.hexo, iconPack:com.cyngn.hexo, fontPkg:com.cyngn.hexo, com.android.systemui=overlay:com.cyngn.hexo, com.android.systemui.navbar=overlay:com.cyngn.hexo}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at com.mediatechindo.wahyu.materialuikit.template.WalktroughCategory.Style14.WalkthroughStyle14Activity$ViewPagerAdapter.getItem(WalkthroughStyle14Activity.java:94)
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:101)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1248)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1642)
at android.view.View.measure(View.java:18799)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2899)
at android.view.View.measure(View.java:18799)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
您的公共变量ImageView
为空,因为您在创建父WalkthroughStyle14Fragment
的实例后尝试立即访问它。布局尚未膨胀,因此尚未调用onCreateView
。在FragmentPagerAdapter.getItem(int)
中创建片段是正确的设计,但访问成员变量并不适合您。
一个很好的面向对象的解决方案是从WalkthroughStyle14Fragment
定义4个派生类,每个派生类都有自己的背景/前景图像或任何其他自定义。例如。 WalkthroughStyle14FragmentOne
,WalkthroughStyle14FragmentTwo
,WalkthroughStyle14FragmentThree
,WalkthroughStyle14FragmentFour
。在onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
中设置适当的图像/文字。您可以将每个派生片段的数量添加到适配器中显示的片段集。您可以根据传入的位置在适配器中实例化相应的片段。