Android Studio - 动态片段崩溃应用

时间:2016-04-18 14:29:48

标签: android android-fragments android-studio fragment android-recyclerview

我正在尝试使用以下教程的变体链接Fragment,RecycleView和CardView: http://www.treyrobinson.net/blog/android-l-tutorials-part-3-recyclerview-and-cardview/

不幸的是,我的应用崩溃时出现以下错误消息:

"java.lang.IllegalStateException: RecyclerView has no LayoutManager"

我对Android开发很新。我试图在Stackoverflow上找到类似的问题,但无法找到问题。任何帮助将不胜感激!

主要活动的布局:" activity_library.xml":

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LibraryActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/library_activity_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".LibraryActivity"
        />
</RelativeLayout> 

主要活动类&#34; LibraryActivity.java&#34;:

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class LibraryActivity extends AppCompatActivity{
    private static final String TAG = LibraryActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); /**Create the activity and populate the savedInstanceState if the activity existed but has been destroyed (otherwise savedInstanceState will return 'null'*/
        setContentView(R.layout.activity_library); /** Call to the XML layout library which display the activity */

        if (savedInstanceState != null) {
            // Restore value of members from saved state
        } else {
            // Probably initialize members with default values for a new instance
        }

        try {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            BooksFragment bookFragment = new BooksFragment();
            fragmentTransaction.add(R.id.library_activity_recycler_view,bookFragment);


            /*The line below is the one generating the error - java.lang.IllegalStateException: RecyclerView has no LayoutManager*/
            fragmentTransaction.commit();

        }
        catch(Exception e){
            Log.d(TAG,"[ERROR] " + e.getMessage());
        }
    }
}

片段的布局&#34; fragment_books.xml&#34;:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/Book_Recycler_View"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

片段类:

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class BooksFragment extends Fragment {

    public BooksFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_library, container, false);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerView bookRecycler = (RecyclerView) v.findViewById(R.id.library_activity_recycler_view);
        bookRecycler.setLayoutManager(layoutManager);
        BookRecyclerViewAdapter adapter = new BookRecyclerViewAdapter();
        bookRecycler.setAdapter(adapter);
        /*In the line below should we return the view 'v' or the RecyclerView 'bookRecycler'??*/
        return bookRecycler;
    }
}

正如我所说,我对Android很新,因此如果我的代码包含很多错误,我会道歉。谢谢你的帮助!

供参考完整的错误日志:

04-18 14:04:52.323 11225-11225/com.wldtaster.tellmeastory E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.wldtaster.tellmeastory, PID: 11225
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wldtaster.tellmeastory/com.wldtaster.tellmeastory.LibraryActivity}: java.lang.IllegalStateException: RecyclerView has no LayoutManager
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
         Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager
            at android.support.v7.widget.RecyclerView.generateLayoutParams(RecyclerView.java:3393)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at com.wldtaster.tellmeastory.BooksFragment.onCreateView(BooksFragment.java:23)
            at android.app.Fragment.performCreateView(Fragment.java:2053)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
            at android.app.BackStackRecord.run(BackStackRecord.java:834)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
            at android.app.Activity.performStart(Activity.java:6005)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
            at android.app.ActivityThread.access$800(ActivityThread.java:151) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5254) 
            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:903) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

1 个答案:

答案 0 :(得分:3)

您必须返回视图即。 v继承类的onCreateView方法中的Fragment