A
A
abukhvalov2015-05-03 19:01:26
Android
abukhvalov, 2015-05-03 19:01:26

Android. What is the problem with inheritance?

Hello. There is a main class. I have omitted the excess for brevity. At the start of this activity, everything works.

public class MainActivity  extends DrawerActivity {
    protected Activity activity = this;
    protected class GetDataFromMongoDb extends AsyncTask<String, Integer, ArrayList<CurrentNewsItem>> {
        protected ArrayList<CurrentNewsItem> doInBackground(String... provider) {
            наполняю массив данными для адаптера
        }
        protected void onPostExecute(final ArrayList<CurrentNewsItem> result) {
            adapter = new RecyclerAdapter(result);
            StaggeredGridLayoutManager llm = new StaggeredGridLayoutManager(UtilsScreen.getDisplayColumns((Activity) activity), StaggeredGridLayoutManager.VERTICAL);
            rv.setLayoutManager(llm);
            rv.setAdapter(adapter);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.activity_main, null, false);
        mDrawer.addView(contentView, 0);

        CurrentSection = ("Новости");
        rv = (RecyclerView)findViewById(R.id.rv_main);
        new GetDataFromMongoDb().execute(CurrentSection);
    }

Inherited:
public class News extends MainActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.activity_news, null, false);
        mDrawer.addView(contentView, 0);

        CurrentSection = getIntent().getExtras().getString("section");
        rv = (RecyclerView)findViewById(R.id.rv);
        new GetDataFromMongoDb().execute(CurrentSection);
    }
}

When you switch to it, the screen is empty, although the debugger says that the data is all there. When I click, I get an error:
E/InputEventReceiver﹕ Exception dispatching input event.
E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
at android.support.v7.widget.RecyclerView.computeVerticalScrollOffset(RecyclerView.java:1335)
at android.view.View.canScrollVertically(View.java:12724)
at android.support.v4.view.ViewCompatICS.canScrollVertically(ViewCompatICS .java:35)
at android.support.v4.view.ViewCompat$ICSViewCompatImpl.canScrollVertically(ViewCompat.java:1136)
at android.support.v4.view.ViewCompat.canScrollVertically(ViewCompat.java:1528)
at android.support.v4.widget.SwipeRefreshLayout.canChildScrollUp(SwipeRefreshLayout.java:624
) (SwipeRefreshLayout.java:638)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2059)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java: 2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent( ViewGroup.java:2119
)
ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup .java:2430)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
at android.app.Activity .dispatchTouchEvent(Activity.java:2739)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
at android.view.View.dispatchPointerEvent(View.java:8578)
at android. view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502 ) ) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:143) at android.os.Looper.loop(Looper.java:122)
) )
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java: 3635)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
at android.view .ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Well, this jumps to the heap:
No adapter attached; skipping layout
although everything works.
Where did I screw up?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
one pavel, 2015-05-03
@onepavel

If I understood the problem correctly, then you clearly wrote "No adapter attached".
The RecyclerView is trying to render, but the view can't get it, because there is no adapter when
the fragment is created.

A
abelthefirst, 2015-05-04
@abelthefirst

You have both RecyclerViews in your layout, but the LayoutManager and Adapter will only attach to one of them. The second RecyclerView seems to respond to touch events, but crashes with the NullPointer due to the lack of a LayoutManager.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question