android 安卓系统:id="@+id/用户信息”为空

jhdbpxl9  于 2022-11-03  发布在  Android
关注(0)|答案(2)|浏览(142)

我可以运行我的应用程序与帐户。但帐户信息似乎是空的个人资料页面。所以应用程序崩溃,因为空值。我搜索有一个错误与butterknife。我是新的android。请帮助我解决这个问题。首先,我有一个错误的NPE,我看到的ID是空的。

我的fragment_account.xml是:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:fillViewport="true"
    android:background="@color/white"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/profile_image"
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_marginTop="45dp"
            android:src="@drawable/icon_face"
            android:padding="25dp"
            android:background="@drawable/cirlce"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Üye No: 18598575"
            android:textColor="@color/profile_uye_no_color"
            android:fontFamily="@font/nunito_bold"
            android:textSize="@dimen/title_sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/constraintLayout6" />

        <TextView
            android:id="@+id/user_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="32dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="20dp"
            android:gravity="center_horizontal"
            android:textColor="#565656"
            android:fontFamily="@font/nunito_bold"
            android:textSize="@dimen/big_sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/user_name"
            tools:text="AppLogist Bilisim Teknolojileri San. ve Tic. Limited Sirketi" />

        <Button
            android:id="@+id/profile_edit"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginEnd="100dp"
            android:layout_marginStart="100dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/button_gradiant_blue"
            android:text="@string/account_edit_profile_button_text"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="12sp"
            android:visibility="gone" />

        <LinearLayout
            android:id="@+id/provizyon_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:orientation="vertical"
            android:visibility="gone">

            <View
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginEnd="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginTop="10dp"
                android:drawableEnd="@drawable/next_3_kopya_2"
                android:drawableRight="@drawable/next_3_kopya_2"
                android:fontFamily="@font/nunito"
                android:text="@string/account_provizyon_text"
                android:textSize="@dimen/medium_sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/satin_alma_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="vertical"
            android:visibility="visible">

            <View
                android:id="@+id/view"
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginEnd="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginTop="10dp"
                android:drawableEnd="@drawable/next_3_kopya_2"
                android:drawableRight="@drawable/next_3_kopya_2"
                android:fontFamily="@font/nunito"
                android:text="@string/account_buyings"
                android:textSize="@dimen/medium_sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ihale_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"
                android:fontFamily="@font/nunito"
                android:textSize="@dimen/medium_sp"
                android:drawableRight="@drawable/next_3_kopya_2"
                android:drawableEnd="@drawable/next_3_kopya_2"
                android:text="@string/account_offers_text" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/cikis_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginTop="10dp"
                android:text="@string/account_exit_text"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"
                android:fontFamily="@font/nunito"
                android:textSize="@dimen/medium_sp"
                android:drawableRight="@drawable/next_3_kopya_2"
                android:drawableEnd="@drawable/next_3_kopya_2"
                android:textColor="@color/orange_color" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />

        </LinearLayout>

    </LinearLayout>

</ScrollView>

我的AccountFragment.java是:

@BindView(R.id.user_info)
    protected TextView userInfo;

我的日志目录是:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.applogist.filomotive.network.responese.model.user.UserModel com.applogist.filomotive.network.responese.model.login.LoginModel.getUserInfo()' on a null object reference

my profil page with "üye no..."Normally the first and last name should appear.

我的LoginModel.java是:

public class LoginModel extends BaseModel {

    @SerializedName("LoginKey")
    private String loginKey;

    @SerializedName("LogoutTime")
    private Date logoutTime;

    @SerializedName("UserInfo")
    private UserModel userInfo;

    public String getLoginKey() {
        return loginKey;
    }

    public void setLoginKey(String loginKey) {
        this.loginKey = loginKey;
    }

    public Date getLogoutTime() {
        return logoutTime;
    }

    public void setLogoutTime(Date logoutTime) {
        this.logoutTime = logoutTime;
    }

    public UserModel getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserModel userInfo) {
        this.userInfo = userInfo;
    }

    @Override
    public String toString() {
        return "LoginModel{" +
                "loginKey='" + loginKey + '\'' +
                ", logoutTime=" + logoutTime +
                ", userInfo=" + userInfo +
                '}';
    }
}

我模型无法接收用户信息。

deikduxw

deikduxw1#

也许您还没有在fragment中绑定视图,或者在gradle中缺少实现。

public class AccountFragment extends Fragment {

    private Unbinder unbinder;

    @BindView(R.id.user_info)
    protected TextView mTvUserInfo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.my_fragment_account, container, false);

        // bind view using butter knife
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        // unbind the view to free some memory
        unbinder.unbind();
    }
}

**编辑:**如果是Gradle中的问题,您可以在您的app:Build-Gradle中添加以下行:

android {
  ...
  // Butterknife requires Java 8.
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  implementation 'com.jakewharton:butterknife:10.2.3'
  annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
}
abithluo

abithluo2#

private UserModel userInfo = new UserModel();

在LoginModel.java对象创建是必需的如上所述。对不起,我对这个问题的信息不足

相关问题