android studio中片段内的RecyclerView使我的应用程序崩溃

Posted

技术标签:

【中文标题】android studio中片段内的RecyclerView使我的应用程序崩溃【英文标题】:RecyclerView inside a fragment in android studio crashing my app 【发布时间】:2021-12-17 02:50:58 【问题描述】:

每当我尝试将 recyclerView 放入 android studio 中的片段时,我的应用就会崩溃。

我不知道发生了什么。我尝试使用按钮替换 Fragment 中的布局。该应用程序启动良好,但每当我单击按钮时,应用程序崩溃 每当我从模拟器甚至手机点击应用程序时,应用程序都会打开并立即返回主屏幕,并且不会显示任何错误消息。

主活动:

   public class MainActivity extends AppCompatActivity 
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView=findViewById(R.id.recyclerView);

        FrameLayout frameLayout = new FrameLayout(this);
        frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        frameLayout.setId(R.id.layout);

        setContentView(frameLayout);


        getSupportFragmentManager().beginTransaction().add(R.id.layout,new Fragment1()).commit();

    

片段类:



public class Fragment1 extends Fragment 
    String[] Text="he","hdhd","hdhdhdh","dhlsd","hshdsss","he","hdhd";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        View view = LayoutInflater.from(getContext()).inflate(R.layout.rows,container,false);

        RecyclerView recyclerView = view.findViewById(R.id.RecyclerView2);

        ImrulsAdapter Adapter = new ImrulsAdapter(getContext(),Text);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(Adapter);

        return view;
    


自定义适配器类:

public class ImrulsAdapter extends RecyclerView.Adapter<ImrulsAdapter.ImrulsViewHolder> 

    String[]Text;
    Context context;

    public ImrulsAdapter(Context context, String[] text ) 
        Text = text;
        this.context = context;
    




    @NonNull
    @Override
    public ImrulsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(context).inflate(R.layout.rows,parent,false);

        return new ImrulsViewHolder(view);
    

    @Override
    public void onBindViewHolder(@NonNull ImrulsViewHolder holder, int position) 
        holder.textView.setText(Text[position]);

    

    @Override
    public int getItemCount() 
        return Text.length;
    

    public class ImrulsViewHolder extends RecyclerView.ViewHolder
        TextView textView;

        public ImrulsViewHolder(@NonNull View itemView) 
            super(itemView);
            textView= itemView.findViewById(R.id.textView);
        
    

日志猫输出:

2021-11-02 22:46:34.026 12332-12332/com.example.recyclerviewinsideafragment E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.recyclerviewinsideafragment, PID: 12332
    java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference
        at com.example.recyclerviewinsideafragment.Fragment1.onCreateView(Fragment1.java:29)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1432)
        at android.app.Activity.performStart(Activity.java:7923)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3359)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2068)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7551)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
2021-11-02 22:46:34.047 12332-12332/? I/Process: Sending signal. PID: 12332 SIG: 9

【问题讨论】:

请编辑问题并复制粘贴日志输出。 丢失了最重要的一段代码:崩溃的堆栈跟踪。请也删除不必要的部分(例如 Manifest + build.gradle,可能还有其他东西) 现在可以了吗? 堆栈跟踪显示回收器视图为空。在 findViewById 中好像找不到 有什么可能的原因吗,为什么会这样? 【参考方案1】:

您需要在 XML 中的 RecyclerView 中添加这一行:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

最后应该是这样的:

<androidx.recyclerview.widget.RecyclerView
   android:id="@+id/yourRecyclerViewId" //RecyclerView2 
   android:layout_
   android:layout_
   app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

【讨论】:

我添加了,但仍然显示相同的错误。 还有另一种解决方案,即您在 XML 中的 RecyclerView 中没有 RecyclerView2 id,因此找不到它。如果可以的话,分享代码,这样会更容易找到错误 非常感谢。我找到了解决方案。我在给片段充气时错误地为我的适配器使用了行布局。 太好了,很高兴您找到了解决方案。这很难从上面的代码中推断出来:)

以上是关于android studio中片段内的RecyclerView使我的应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Android:导航抽屉片段内的Viewpager

Android Studio - 片段按钮操作

片段中的Android Studio RecyclerView [重复]

如何在android studio中用另一个片段替换一个片段

Android:片段内的按钮操作

带有片段的 NavigationDrawer 内的 Android TabLayout