【问题标题】:android:id="@+id/user_info" is nullandroid:id=\"@+id/user_info\" 为空
【发布时间】:2022-08-23 15:16:42
【问题描述】:

我可以使用帐户运行我的应用程序。但是个人资料页面上的帐户信息似乎为空。因此应用程序由于空值而崩溃。我搜索了黄油刀有一个错误。我是安卓新手。请帮我解决这个问题。首先我有一个错误 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 +
                \'}\';
    }
}

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

编辑:答案如下。

  • 使用视图绑定或数据绑定
  • 你能提供更多信息吗?我认为我使用视图绑定。
  • 这没有帮助,也在问题中添加错误,并提供更多问题的上下文。
  • 并停止使用黄油刀,因为它已被弃用。使用视图绑定。
  • 我在上面编辑了我的问题。如果可以,请你帮助我。

标签: android butterknife


【解决方案1】:

也许您没有在片段中绑定视图或在 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'
}

【讨论】:

  • 我做到了。但它不起作用。我有同样的情况。我在 gradle 中缺少什么实现?
  • 这对我没有帮助,但谢谢银。我在上面编辑了我的问题。
【解决方案2】:
private UserModel userInfo = new UserModel();

如上所述,在 LoginModel.java 中创建对象是必需的。抱歉我关于这个问题的信息很差

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 2013-03-07
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多