【问题标题】:Fragement cant vind the ryclefiew java android studio [duplicate]片段找不到 ryclefiew java android studio [重复]
【发布时间】:2021-10-05 09:04:51
【问题描述】:

这是我的片段 '''java 包 com.example.healthapp.ui.profile;

import android.database.Cursor;
import android.os.Bundle;
import android.os.RecoverySystem;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.example.healthapp.CustomAdapter;
import com.example.healthapp.MyDatabase;
import com.example.healthapp.R;
import com.example.healthapp.databinding.FragmentSettingsBinding;

import java.util.ArrayList;
import java.util.Objects;

public class ProfileFragment extends Fragment {

    private ProfileViewModel profileViewModel;
    private FragmentSettingsBinding binding;

    public MyDatabase myDB;
    public ArrayList<String> user_id, user_firstName, user_lastName, user_email, user_phone_number, user_age, user_weight;
    public CustomAdapter customAdapter;

    private static final String TAG = "Testing";


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        profileViewModel =
                new ViewModelProvider(this).get(ProfileViewModel.class);
        //View view = inflater.inflate(R.layout.fragment_profile);
        binding = FragmentSettingsBinding.inflate(inflater, container, false);

        View root = binding.getRoot();

        //getActivity().setContentView();

        Log.i("Root Info: ", root.toString());
        RecyclerView recyclerView = root.findViewById(R.id.recyclerview);

        myDB = new MyDatabase(getActivity());
        user_id = new ArrayList<>();
        user_firstName = new ArrayList<>();
        user_lastName = new ArrayList<>();
        user_email = new ArrayList<>();
        user_phone_number = new ArrayList<>();
        user_age = new ArrayList<>();
        user_weight = new ArrayList<>();

        storeDataInArrays();

        customAdapter = new CustomAdapter(ProfileFragment.this, getActivity(), user_id, user_firstName, user_lastName, user_email,
                        user_phone_number , user_age, user_weight);
        try {
            recyclerView.setAdapter(customAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        } catch (Exception e) {
            Log.e("Error setting Adapter", "Custom Adapter null: " +
                    (customAdapter == null ? "true" : "false") +
                    ", RecyclerView Null: " + (recyclerView == null ? "true" : "false"), e);
        }



        final TextView textView = binding.textFragment;
        profileViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;
    }

    public void storeDataInArrays(){
        Cursor cursor = myDB.readAllData();
        if(cursor.getCount() == 0){
            Toast.makeText(getActivity(), "No data" , Toast.LENGTH_SHORT).show();
            Log.i(TAG, "No Data");
        }else{
            while (cursor.moveToNext()){
                user_id.add(cursor.getString(0));
                user_firstName.add(cursor.getString(1));
                user_lastName.add(cursor.getString(2));
                user_email.add(cursor.getString(3));
                user_phone_number.add(cursor.getString(4));
                user_age.add(cursor.getString(5));
                user_weight.add(cursor.getString(6));
                Log.i(TAG, user_firstName + " " + user_lastName);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}

'''

fragmant_profile.xml

'''xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.profile.ProfileFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />


</androidx.constraintlayout.widget.ConstraintLayout>

'''

错误代码 E/错误设置适配器:自定义适配器 null:false,RecyclerView Null:true java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)” 在 com.example.healthapp.ui.profile.ProfileFragment.onCreateView(ProfileFragment.java:71) 在 androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963) 在 androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518) 在 androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282) 在 androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189) 在 androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106) 在 androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002) 在 androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524) 在 android.os.Handler.handleCallback(Handler.java:938) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:223) 在 android.app.ActivityThread.main(ActivityThread.java:7656) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) **

【问题讨论】:

  • 使用 viewbinding 然后还使用 findViewById 有什么意义?如果您的绑定没有检测到它,那么您需要检查原因

标签: java android android-fragments android-recyclerview


【解决方案1】:

如果你使用 Viewbinding (Line:48) 那么你不应该使用 findViewById (Line:55) 你应该像这样添加你的 RecyclerView:

RecyclerView recyclerView = this.binding.recyclerView

如果您的 RecyclerView 不在布局文件中或有其他 android:id IDE 将显示错误 ;)

【讨论】:

  • 它找不到recycleview,但我认为这与viewmodel有关,因为它与正常活动一起工作我将xml的代码添加到问题中
  • 您的 Bindingclass 的名称为 FragmentSettingBinding 您的 Layout 的名称为 fragment_profile.xml。这应该是 FragmentProfileBinding
猜你喜欢
  • 2015-02-24
  • 2016-06-09
  • 2018-07-24
  • 2017-06-24
  • 1970-01-01
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多