Android:滑动菜单

时间:2013-11-15 08:00:32

标签: android nullpointerexception

我目前正在使用Android链接代码下载滑动菜单。

Android - How to make slide menu like facebook, spotify and Google +

调用函数时出错。

我的logcat是

  

[11-15 13:26:02.528:E / AndroidRuntime(9120):致命异乎寻常:主要   11-15 13:26:02.528:E / AndroidRuntime(9120):   java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.slidemenu / com.example.slidemenu.MainActivity}:   java.lang.NullPointerException 11-15 13:26:02.528:   E / AndroidRuntime(9120):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)   11-15 13:26:02.528:E / AndroidRuntime(9120):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)   11-15 13:26:02.528:E / AndroidRuntime(9120):at   android.app.ActivityThread.access $ 600(ActivityThread.java:134)11-15   13:26:02.528:E / AndroidRuntime(9120):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1247)   11-15 13:26:02.528:E / AndroidRuntime(9120):at   android.os.Handler.dispatchMessage(Handler.java:99)11-15   13:26:02.528:E / AndroidRuntime(9120):at   android.os.Looper.loop(Looper.java:154)11-15 13:26:02.528:   E / AndroidRuntime(9120):at   android.app.ActivityThread.main(ActivityThread.java:4624)11-15   13:26:02.528:E / AndroidRuntime(9120):at   java.lang.reflect.Method.invokeNative(Native Method)11-15   13:26:02.528:E / AndroidRuntime(9120):at   java.lang.reflect.Method.invoke(Method.java:511)11-15 13:26:02.528:   E / AndroidRuntime(9120):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:809)   11-15 13:26:02.528:E / AndroidRuntime(9120):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)11-15   13:26:02.528:E / AndroidRuntime(9120):at   dalvik.system.NativeStart.main(Native Method)11-15 13:26:02.528:   E / AndroidRuntime(9120):引起:java.lang.NullPointerException   11-15 13:26:02.528:E / AndroidRuntime(9120):at   com.example.slidemenu.MainActivity.initializeAnimations(MainActivity.java:120)   11-15 13:26:02.528:E / AndroidRuntime(9120):at   com.example.slidemenu.MainActivity.onCreate(MainActivity.java:35)   11-15 13:26:02.528:E / AndroidRuntime(9120):at   android.app.Activity.performCreate(Activity.java:4488)11-15   13:26:02.528:E / AndroidRuntime(9120):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)   11-15 13:26:02.528:E / AndroidRuntime(9120):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)   11-15 13:26:02.528:E / AndroidRuntime(9120)

它显示的是NullPointerException我知道我得到了null值。但没有得到原因,我也不知道我需要替换哪一段代码。请帮我摆脱这个。

我的主要活动课程

public class MainActivity extends Activity implements OnClickListener {

RelativeLayout filterLayout, findLayout;
ImageView btFilter;
FilterAnimation filterAnimation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    filterLayout = (RelativeLayout) findViewById(R.id.filter_layout);
    findLayout = (RelativeLayout) findViewById(R.id.find_layout);
    btFilter = (ImageView) findViewById(R.id.imageView1);
    btFilter.setOnClickListener(this);
    filterAnimation = new FilterAnimation(this);
    initializeAnimations();
}

private void initializeAnimations() {
    final ViewTreeObserver filterObserver = filterLayout
            .getViewTreeObserver();

    filterObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            filterLayout.getViewTreeObserver()
                    .removeGlobalOnLayoutListener(this);

            DisplayMetrics displayMetrics = getResources()
                    .getDisplayMetrics();

            int deviceWidth = displayMetrics.widthPixels;

            int filterLayoutWidth = (deviceWidth * 80) / 100;
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    filterLayoutWidth,
                    RelativeLayout.LayoutParams.MATCH_PARENT);

            filterLayout.setLayoutParams(params);
            filterAnimation.initializeFilterAnimations(filterLayout);

        }
    });

    ViewTreeObserver findObserver = findLayout.getViewTreeObserver();
    findObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            findLayout.getViewTreeObserver().removeGlobalOnLayoutListener(
                    this);
            filterAnimation.initializeOtherAnimations(findLayout);
        }
    });
}

@Override
public void onClick(View v) {
    int id = v.getId();
    switch (id) {
    case R.id.imageView1:
        filterAnimation.toggleSliding();
        break;
    }
}
}

然后我的FilterAnimation类

public class FilterAnimation implements AnimationListener {

Context context;
RelativeLayout player_sliderLayout, sliderLayout;
private Animation filterSlideIn, filterSlideOut, otherSlideIn,
        otherSlideOut;
private static int sliderLayoutWidth, sliderLayoutHeight;
private boolean isOtherSlideOut = false;
private int deviceWidth;
private int margin;

public FilterAnimation(Context context) {
    this.context = context;

    DisplayMetrics displayMetrics = context.getResources()
            .getDisplayMetrics();
    deviceWidth = displayMetrics.widthPixels;
}

public void initializeFilterAnimations(RelativeLayout player_sliderLayout) {
    this.player_sliderLayout = player_sliderLayout;
    filterSlideIn = AnimationUtils.loadAnimation(context,
            R.anim.filter_slide_in);
    filterSlideOut = AnimationUtils.loadAnimation(context,
            R.anim.filter_slide_out);

}

public void initializeOtherAnimations(RelativeLayout sliderLayout) {
    this.sliderLayout = sliderLayout;
    sliderLayoutWidth = sliderLayout.getWidth();
    sliderLayoutHeight = sliderLayout.getHeight();
    otherSlideIn = AnimationUtils.loadAnimation(context,
            R.anim.other_slide_in);
    otherSlideIn.setAnimationListener(this);
    otherSlideOut = AnimationUtils.loadAnimation(context,
            R.anim.other_slide_out);
    otherSlideOut.setAnimationListener(this);
}

public void toggleSliding() {
    if (isOtherSlideOut) {

        player_sliderLayout.startAnimation(filterSlideOut);
        player_sliderLayout.setVisibility(View.INVISIBLE);
        sliderLayout.startAnimation(otherSlideIn);

    } else // slide findLayout Out and player_sliderLayout In
    {
        sliderLayout.startAnimation(otherSlideOut);
        player_sliderLayout.setVisibility(View.VISIBLE);
        player_sliderLayout.startAnimation(filterSlideIn);
    }
}

@Override
public void onAnimationEnd(Animation animation) {
    if (isOtherSlideOut) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                sliderLayoutWidth, sliderLayoutHeight);
        sliderLayout.setLayoutParams(params);
        isOtherSlideOut = false;
    } else {
        margin = (deviceWidth * 80) / 100;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                sliderLayoutWidth, sliderLayoutHeight);
        params.leftMargin = margin;
        params.rightMargin = -margin;
        sliderLayout.setLayoutParams(params);
        isOtherSlideOut = true;
        dimsliderLayout();
    }
}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationStart(Animation animation) {
}

private void dimsliderLayout() {
    AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);
    alphaAnimation.setFillAfter(true);
    sliderLayout.startAnimation(alphaAnimation);
}
}

0 个答案:

没有答案