gpt4 book ai didi

android - 无法返回 View ,但只能在 onCreateView 方法( fragment )中返回 recyclerview

转载 作者:行者123 更新时间:2023-11-29 23:50:39 28 4
gpt4 key购买 nike

我计划在新的提要功能中添加一个 float 按钮。但是,在 onCreateView 方法期间,它只允许我返回 recycleview 而不是整个 View ,它只返回 fragment 上的 recycleview。

这是我在 onCreateView 方法中的代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
posts = new ArrayList<>();

for(int i=9; i>=0; i--){
posts.add(new Post("Testing"+i,"For testing","https://picsum.photos/600/300?random?"+i));
}


recyclerView = new RecyclerView(getContext());
LinearLayoutManager lim = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(lim);
pa = new PostAdapter(posts, this);
recyclerView.setAdapter(pa);
recyclerView.setItemAnimator(new DefaultItemAnimator());

return recyclerView;
}

这是我尝试返回整个 View 时的做法

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
posts = new ArrayList<>();

for(int i=9; i>=0; i--){
posts.add(new Post("Testing"+i,"For testing","https://picsum.photos/600/300?random?"+i));
}

view = inflater.inflate(R.layout.fragment_chat, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.strView);
LinearLayoutManager lim = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(lim);
pa = new PostAdapter(posts, this);
recyclerView.setAdapter(pa);


return view;
}

这是xml文件

<FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.RecyclerView
android:id="@+id/strView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/addStrBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:backgroundTint="@color/colorWhite"
android:clickable="true"
android:cropToPadding="false"
android:elevation="@dimen/cardview_default_elevation"
android:src="@drawable/baseline_create_black_18dp"
app:fabSize="normal"/>

Logcat 尝试返回 View

06-20 20:53:57.733 4990-4990/com.example.cheejin.fyp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cheejin.fyp, PID: 4990
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cheejin.fyp/com.example.cheejin.fyp.HomePage}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class android.support.design.widget.FloatingActionButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class android.support.design.widget.FloatingActionButton
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.cheejin.fyp.ChatFragment.onCreateView(ChatFragment.java:56)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2343)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1421)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1752)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1821)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2595)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2382)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2337)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2244)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3255)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3205)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.FloatingActionButton
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at com.example.cheejin.fyp.ChatFragment.onCreateView(ChatFragment.java:56) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2343) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1421) 
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1752) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1821) 
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797) 
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2595) 
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2382) 
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2337) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2244) 
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3255) 
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3205) 
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195) 
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597) 
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237) 
at android.app.Activity.performStart(Activity.java:6253) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at com.example.cheejin.fyp.ChatFragment.onCreateView(ChatFragment.java:56) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2343) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1421) 
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1752) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1821) 
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797) 
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2595) 
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2382) 
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2337) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2244) 
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3255) 
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3205) 
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195) 
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597) 
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237) 
at android.app.Activity.performStart(Activity.java:6253) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable()' on a null object reference
at android.graphics.drawable.LayerDrawable$ChildDrawable.<init>(LayerDrawable.java:1723)
at android.graphics.drawable.LayerDrawable$LayerState.<init>(LayerDrawable.java:1792)
at android.graphics.drawable.RippleDrawabl

最佳答案

我找到了解决方案....

引用这个链接 https://blog.csdn.net/Sean_css/article/details/68592412

只需要改变android:backgroundTint="@color/colorPrimary"到app:backgroundTint="@color/colorPrimary"

关于android - 无法返回 View ,但只能在 onCreateView 方法( fragment )中返回 recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50933970/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com