无法实例化活动ComponentInfo,android?

时间:2015-04-03 09:27:01

标签: android android-recyclerview

我正在尝试在我的应用中实现ObservableScrollView。但是当我运行

时,我得到的是以下异常
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.admin.obserscroll/com.example.admin.obserscroll.MainActivity}: java.lang.InstantiationException: can't instantiate class com.example.admin.obserscroll.MainActivity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
        at android.app.ActivityThread.access$800(ActivityThread.java:145)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5127)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.InstantiationException: can't instantiate class com.example.admin.obserscroll.MainActivity
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1208)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)

这是我的活动课

public abstract class MainActivity<S extends Scrollable> extends ActionBarActivity implements ObservableScrollViewCallbacks {
private Toolbar mToolbar;
private S mScrollable;
private static final int NUM_OF_ITEMS_FEW = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    mScrollable = createScrollable();
    mScrollable.setScrollViewCallbacks(this);
    ObservableRecyclerView recyclerView = (ObservableRecyclerView) findViewById(R.id.scrollable);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(new SimpleRecyclerAdapter(this, getDummyData(NUM_OF_ITEMS_FEW)));
}




public static ArrayList<String> getDummyData(int num) {
    ArrayList<String> items = new ArrayList<String>();
    for (int i = 1; i <= num; i++) {
        items.add("Item " + i);
    }
    return items;
}

protected abstract S createScrollable();

@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
}

@Override
public void onDownMotionEvent() {
}

@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
    Log.e("DEBUG", "onUpOrCancelMotionEvent: " + scrollState);
    if (scrollState == ScrollState.UP) {
        if (toolbarIsShown()) {
            hideToolbar();
        }
    } else if (scrollState == ScrollState.DOWN) {
        if (toolbarIsHidden()) {
            showToolbar();
        }
    }
}

private boolean toolbarIsShown() {
    return ViewHelper.getTranslationY(mToolbar) == 0;
}

private boolean toolbarIsHidden() {
    return ViewHelper.getTranslationY(mToolbar) == -mToolbar.getHeight();
}

private void showToolbar() {
    moveToolbar(0);
}

private void hideToolbar() {
    moveToolbar(-mToolbar.getHeight());
}

private void moveToolbar(float toTranslationY) {
    if (ViewHelper.getTranslationY(mToolbar) == toTranslationY) {
        return;
    }
    ValueAnimator animator = ValueAnimator.ofFloat(ViewHelper.getTranslationY(mToolbar), toTranslationY).setDuration(200);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float translationY = (float) animation.getAnimatedValue();
            ViewHelper.setTranslationY(mToolbar, translationY);
            ViewHelper.setTranslationY((View) mScrollable, translationY);
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) ((View) mScrollable).getLayoutParams();
            lp.height = (int) -translationY + getScreenHeight() - lp.topMargin;
            ((View) mScrollable).requestLayout();
        }
    });
    animator.start();
}
protected int getScreenHeight() {
    return findViewById(android.R.id.content).getHeight();
}}

活动XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
    app:theme="@style/Toolbar" />

<com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView
    android:id="@+id/scrollable"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize" />

1 个答案:

答案 0 :(得分:0)

为什么要进行活动abstract? Java不允许您创建它的实例,因此要么删除关键字,要么创建一些实现。