当前位置: 首页 > 知识库问答 >
问题:

ryclefiew java android工作室[副本]

董霖
2023-03-14

这是我的碎片

 package 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 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/error ;setting ;适配器:自定义适配器null:false,recycerview null:true java.lang.nullpointerException:试图在com.example.healthapp.ui.profile.profileFragment.oncreateView(profileFragment.java.app.fragment.app.fragment.fragment.performCreateView(fragment.java/2963)和在AndroidX.fragment.app.fragmentstatemanager.movetoExpectedState(FragmentStatemanager.java:282)在AndroidX.fragment.app.fragmentManager.executeOpstoGether(FragmentManager.java:2189)在AndroidX.fragment.app.fragmentManager.removeredDnantOperationSandExecute(FragmentManager.java:2106)在AndroidX.fragment.app.fragmentManager.java:2106)在在Android.os.handler.handleCallback(handler.java:938)在Android.os.handler.dispatchMessage(handler.java:99)在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(原生方法)在

共有1个答案

胥诚
2023-03-14

如果使用Viewbinding(行:48),则不应使用findViewById(行:55),应添加RecyclerView,如下所示:

RecyclerView recyclerView = this.binding.recyclerView

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

 类似资料:
  • 地图工作室的一般流程包括:创建项目、选择底图、添加图层、添加数据、修改图层属性、分析数据、保存并分享。 创建项目 您可以在应用中心中点击地图工作室,在弹出页面中点击“立即体验”。您也可以依次点击“资源中心”,“地图”,“创建地图”,“地图工作室”打开地图工作室。 在资源中心中,可以查看或编辑您保存的地图。 选择底图 地图工作室默认中国暗色地图作为底图。纯色底图支持设置颜色及底图坐标系,支持设置的坐

  • 突然间被识别不出来了,这可能是我做的事情。 有人能给我解释一下...是什么意思吗?我能做些什么来修复它呢?

  • 我们使用的是visual studio 2015,并有以下字体:- 使用visual studio 2015 typescript编译器,这可以归结为:- 这很好,在chrome上运行良好,但是,我们希望它也能在ie10 / ie11上运行。 我相信我们需要“多填充”这个,以便将生成的js文件转换为es5。 目前visual studio中有什么可以做到这一点吗?这种转换的最佳方式是什么?或任何样

  • 我被鼓励开始android开发,所以我安装了android studio,并尝试启动Hello-world项目。我安装了android studio,它下载了所有必需的文件。我在android studio网站上遵循了guide,但每次尝试创建一个空活动-项目时,我都有很多错误。我试着改变SDK版本,但我对java和android开发完全是新手,不能理解我做错了什么。拜托,我花了一整天的时间来解决

  • 感激地接受任何帮助。 提前致谢

  • 我用的是Jaspersoft Studio 5.5.2。我需要改变单个条形的颜色。我发现了如何创建和分配一个定制类,以及如何创建自己的bar渲染器并覆盖它。 问题是,一旦我这样做,图表就会显示一系列重复的条形,这些柱线在Y刻度上向右移动并略低。起初,我以为这是某种阴影,但是对于每个行,列组合,getItemPaint 方法被调用两次。 我的定制器和渲染器(简化,但测试显示相同的影响) 我想显示“好