我正在尝试在github上使用示例代码(https://github.com/kmshack/Android-ParallaxHeaderViewPager)我完全复制了代码,只是更改了它们的值,但问题是当我运行应用程序时它给了我这个错误:
07-04 15:42:38.683 6621-6621/com.moradisani.sorush.flstudio E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.moradisani.sorush.flstudio, PID: 6621
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.moradisani.sorush.flstudio/com.moradisani.sorush.flstudio.Edu}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.moradisani.sorush.flstudio.KenBurnsSupportView.setResourceIds(int[])' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
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:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.moradisani.sorush.flstudio.KenBurnsSupportView.setResourceIds(int[])' on a null object reference
at com.moradisani.sorush.flstudio.Edu.onCreate(Edu.java:58)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
以下是给出错误的行:
package com.moradisani.sorush.flstudio;
import android.annotation.TargetApi;
import android.graphics.RectF;
import android.os.Build;
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.util.SparseArrayCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.ImageView;
import com.nineoldandroids.view.ViewHelper;
public class Edu extends ActionBarActivity implements ScrollTabHolder, ViewPager.OnPageChangeListener {
private static AccelerateDecelerateInterpolator sSmoothInterpolator = new AccelerateDecelerateInterpolator();
private KenBurnsSupportView mHeaderPicture;
private View mHeader;
private PagerSlidingTabStrip mPagerSlidingTabStrip;
private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
private int mActionBarHeight;
private int mMinHeaderHeight;
private int mHeaderHeight;
private int mMinHeaderTranslation;
private ImageView mHeaderLogo;
private RectF mRect1 = new RectF();
private RectF mRect2 = new RectF();
private TypedValue mTypedValue = new TypedValue();
private SpannableString mSpannableString;
private AlphaForegroundColorSpan mAlphaForegroundColorSpan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMinHeaderHeight = getResources().getDimensionPixelSize(R.dimen.min_header_height);
mHeaderHeight = getResources().getDimensionPixelSize(R.dimen.header_height);
mMinHeaderTranslation = -mMinHeaderHeight + getActionBarHeight();
setContentView(R.layout.edu);
mHeaderPicture = (KenBurnsSupportView) findViewById(R.id.header_picture);
mHeaderPicture.setResourceIds(R.drawable.head1, R.drawable.head2);
mHeaderLogo = (ImageView) findViewById(R.id.header_logo);
mHeader = findViewById(R.id.header);
mPagerSlidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(4);
mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
mPagerAdapter.setTabHolderScrollingContent(this);
mViewPager.setAdapter(mPagerAdapter);
mPagerSlidingTabStrip.setViewPager(mViewPager);
mPagerSlidingTabStrip.setOnPageChangeListener(this);
mSpannableString = new SpannableString(getString(R.string.actionbar_title));
mAlphaForegroundColorSpan = new AlphaForegroundColorSpan(0xffffffff);
ViewHelper.setAlpha(getActionBarIconView(), 0f);
getSupportActionBar().setBackgroundDrawable(null);
}
@Override
public void onPageScrollStateChanged(int arg0) {
// nothing
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// nothing
}
@Override
public void onPageSelected(int position) {
SparseArrayCompat<ScrollTabHolder> scrollTabHolders = mPagerAdapter.getScrollTabHolders();
ScrollTabHolder currentHolder = scrollTabHolders.valueAt(position);
currentHolder.adjustScroll((int) (mHeader.getHeight() + ViewHelper.getTranslationY(mHeader)));
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount, int pagePosition) {
if (mViewPager.getCurrentItem() == pagePosition) {
int scrollY = getScrollY(view);
ViewHelper.setTranslationY(mHeader, Math.max(-scrollY, mMinHeaderTranslation));
float ratio = clamp(ViewHelper.getTranslationY(mHeader) / mMinHeaderTranslation, 0.0f, 1.0f);
interpolate(mHeaderLogo, getActionBarIconView(), sSmoothInterpolator.getInterpolation(ratio));
setTitleAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F));
}
}
@Override
public void adjustScroll(int scrollHeight) {
// nothing
}
public int getScrollY(AbsListView view) {
View c = view.getChildAt(0);
if (c == null) {
return 0;
}
int firstVisiblePosition = view.getFirstVisiblePosition();
int top = c.getTop();
int headerHeight = 0;
if (firstVisiblePosition >= 1) {
headerHeight = mHeaderHeight;
}
return -top + firstVisiblePosition * c.getHeight() + headerHeight;
}
public static float clamp(float value, float max, float min) {
return Math.max(Math.min(value, min), max);
}
private void interpolate(View view1, View view2, float interpolation) {
getOnScreenRect(mRect1, view1);
getOnScreenRect(mRect2, view2);
float scaleX = 1.0F + interpolation * (mRect2.width() / mRect1.width() - 1.0F);
float scaleY = 1.0F + interpolation * (mRect2.height() / mRect1.height() - 1.0F);
float translationX = 0.5F * (interpolation * (mRect2.left + mRect2.right - mRect1.left - mRect1.right));
float translationY = 0.5F * (interpolation * (mRect2.top + mRect2.bottom - mRect1.top - mRect1.bottom));
ViewHelper.setTranslationX(view1, translationX);
ViewHelper.setTranslationY(view1, translationY - ViewHelper.getTranslationY(mHeader));
ViewHelper.setScaleX(view1, scaleX);
ViewHelper.setScaleY(view1, scaleY);
}
private RectF getOnScreenRect(RectF rect, View view) {
rect.set(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
return rect;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int getActionBarHeight() {
if (mActionBarHeight != 0) {
return mActionBarHeight;
}
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB){
getTheme().resolveAttribute(android.R.attr.actionBarSize, mTypedValue, true);
}else{
getTheme().resolveAttribute(R.attr.actionBarSize, mTypedValue, true);
}
mActionBarHeight = TypedValue.complexToDimensionPixelSize(mTypedValue.data, getResources().getDisplayMetrics());
return mActionBarHeight;
}
private void setTitleAlpha(float alpha) {
mAlphaForegroundColorSpan.setAlpha(alpha);
mSpannableString.setSpan(mAlphaForegroundColorSpan, 0, mSpannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(mSpannableString);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private ImageView getActionBarIconView() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
return (ImageView)findViewById(android.R.id.home);
}
return (ImageView)findViewById(android.support.v7.appcompat.R.id.home);
}
public class PagerAdapter extends FragmentPagerAdapter {
private SparseArrayCompat<ScrollTabHolder> mScrollTabHolders;
private final String[] TITLES = { "Page 1", "Page 2", "Page 3", "Page 4"};
private ScrollTabHolder mListener;
public PagerAdapter(FragmentManager fm) {
super(fm);
mScrollTabHolders = new SparseArrayCompat<ScrollTabHolder>();
}
public void setTabHolderScrollingContent(ScrollTabHolder listener) {
mListener = listener;
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public int getCount() {
return TITLES.length;
}
@Override
public Fragment getItem(int position) {
ScrollTabHolderFragment fragment = (ScrollTabHolderFragment) ListFragment.newInstance(position);
mScrollTabHolders.put(position, fragment);
if (mListener != null) {
fragment.setScrollTabHolder(mListener);
}
return fragment;
}
public SparseArrayCompat<ScrollTabHolder> getScrollTabHolders() {
return mScrollTabHolders;
}
}
}
答案 0 :(得分:0)
KenBurnsSupportView
的 header_picture
可能不是activity_main.xml
布局的孩子。
因此,您必须将KenBurnsSupportView
添加到标识为activity_main.xml
的{{1}}。
与其他项目相同。
编辑:
对于第二个问题 - 从header_picture
开始,您还需要在活动类中添加此方法:
minSdkVersion 14