在我的设备中强制关闭片段

时间:2015-07-23 11:04:40

标签: android android-layout android-fragments android-recyclerview

我正在尝试从模板中将卡片视图添加到我的应用程序中,它已经正常工作,我正在理解,从这个模板中学习和开发,我想我理解它,但显然不是。

当我尝试将其添加到我的应用程序时,我会收到有关recyclerview的一些问题,我正在尝试进行预处理

我的相关主要代码是:

public class MainActivity extends ActionBarActivity
        implements NavigationDrawerCallbacks {
(...)
    private List<Anime> items = new ArrayList<>();
    private RecyclerView recycler;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager lManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.fragment_drawer);

    // Set up the drawer.
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
    coordinates = new String[3];
    coordinates[0]="";coordinates[1]="";coordinates[2]="";
    location = null;
    textViewTitulo = (TextView) findViewById(R.id.textViewCoordinates);

    //this line is the SOLUTION 
    recicle();


}

(...)

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        (...)
        items.add(new Anime(R.drawable.iamhere, "You are at:", "lat. 4.59 long. 67.9 alt. 0.0"));
            recicle();
     }
    public void recicle() {
    //Obtaining the recicler
    recicler = (RecyclerView) findViewById(R.id.reciclador);
        recicler.setHasFixedSize(true);

        // Usar un administrador para LinearLayout
        lManager = new LinearLayoutManager(this);
        recycler.setLayoutManager(lManager);

        // Crear un nuevo adaptador
        adapter = new AnimeAdapter(items);
        recycler.setAdapter(adapter);
    }

Anime类只包含一个int和两个字符串作为atruibutes

编译好,但我认为问题出在布局上。我有一个导航抽屉片段,我几乎可以肯定我的问题是管理XML中片段中的recyclerView。这是我的主要XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include android:id="@+id/toolbar_actionbar" layout="@layout/toolbar_default"
        android:layout_width="match_parent" android:layout_height="wrap_content" />

    <android.support.v4.widget.DrawerLayout android:id="@+id/drawer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_actionbar">

        <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
            android:clickable="true" android:layout_height="match_parent" >

      <!---This is the RecyclerView at hand -->
            <android.support.v7.widget.RecyclerView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/reciclador"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="3dp"
                android:scrollbars="vertical" />

        </FrameLayout>

        <!-- android:layout_marginTop="?android:attr/actionBarSize"-->
        <fragment android:id="@+id/fragment_drawer"
            android:name="com.example.jesus.myapplication.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent" android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer" />


    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>

由于我的其他应用程序(来自模板的应用程序)运行良好,我认为问题不在于膨胀或与此相关的问题。

如果有人想要获取此模板,则基本版本中的here为{}。我没有添加引用,因为我已经更改了代码。但这来自here(西班牙语)。

Logcat将此返回给我:

07-23 13:52:10.962  21206-21206/com.example.jesus.myapplication E/RecyclerView? No adapter attached; skipping layout
07-23 13:52:11.006  21206-21206/com.example.jesus.myapplication D/AndroidRuntime? Shutting down VM
07-23 13:52:11.010  21206-21206/com.example.jesus.myapplication E/AndroidRuntime? FATAL EXCEPTION: main
    Process: com.example.jesus.myapplication, PID: 21206
    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.computeVerticalScrollRange(RecyclerView.java:1535)
            at android.view.View.onDrawScrollBars(View.java:12943)
            at android.view.View.draw(View.java:15237)
            at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:2997)
            at android.view.View.updateDisplayListIfDirty(View.java:14167)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1089)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.updateDisplayListIfDirty(View.java:14162)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.View.draw(View.java:14959)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
            at android.view.View.draw(View.java:15234)
            at android.widget.FrameLayout.draw(FrameLayout.java:598)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2650)
            at android.view.View.updateDisplayListIfDirty(View.java:14167)
            at android.view.View.getDisplayList(View.java:14189)
            at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
            at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
            at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)
            at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler

然后,我们可以看到RecyclerView像我假设的那样有罪。它解释了我,RecyclerView指向一个空指针,然后得到一个空指针异常但是,真诚地,我不知道如何解决它。

你能帮我试一下我的错误吗?它编译好了,当我尝试在我的设备中运行它时它会在开放时返回给我一个FC。

1 个答案:

答案 0 :(得分:1)

我刚刚意识到这是错的。它没有任何与回收者视图或布局有关的东西。我只是没有初始化变量: LinearLayoutManager和我的适配器(AnimeAdapter)。

只需添加recicle();到oncreate方法工作