0

我目前正在使用以下链接代码在 android 中滑动菜单。

Android - 如何制作像 facebook、spotify 和 Google + 这样的幻灯片菜单

调用函数时出错。

我的日志猫是

[11-15 13:26:02.528: E/AndroidRuntime(9120): 致命异常: main 11-15 13:26:02.528: E/AndroidRuntime(9120): java.lang.RuntimeException: Unable to start activity ComponentInfo{com .example.slidemenu/com.example.slidemenu.MainActivity}:java.lang.NullPointerException 11-15 13:26:02.528:E/AndroidRuntime(9120):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android. app.ActivityThread.access$600(ActivityThread.java:134) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247) 11-15 13 :26:02.528:E / AndroidRuntime(9120):在android.os.Handler。dispatchMessage(Handler.java:99) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.os.Looper.loop(Looper.java:154) 11-15 13:26:02.528: E/ AndroidRuntime(9120): 在 android.app.ActivityThread.main(ActivityThread.java:4624) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 java.lang.reflect.Method.invokeNative(Native Method) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 java.lang.reflect.Method.invoke(Method.java:511) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 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): Caused经过: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): 在 com.example.slidemenu.MainActivity.onCreate(MainActivity.java:35) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.app.Activity.performCreate(Activity.java: 4488) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041) 11-15 13:26:02.528: E/AndroidRuntime(9120)120) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 com.example.slidemenu.MainActivity.onCreate(MainActivity.java:35) 11-15 13:26:02.528: E/AndroidRuntime(9120) : 在 android.app.Activity.performCreate(Activity.java:4488) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041) 11-15 13:26:02.528: E/AndroidRuntime(9120)120) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 com.example.slidemenu.MainActivity.onCreate(MainActivity.java:35) 11-15 13:26:02.528: E/AndroidRuntime(9120) : 在 android.app.Activity.performCreate(Activity.java:4488) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041) 11-15 13:26:02.528: E/AndroidRuntime(9120)callActivityOnCreate(Instrumentation.java:1050) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041) 11-15 13:26:02.528: E/安卓运行时(9120)callActivityOnCreate(Instrumentation.java:1050) 11-15 13:26:02.528: E/AndroidRuntime(9120): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041) 11-15 13:26:02.528: E/安卓运行时(9120)

它显示 NullPointerException 我知道我得到了空值。但不明白为什么,我也不知道我需要替换哪段代码。请帮助我摆脱困境。

我的主要活动课程

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

0 回答 0