【问题标题】:Views in Fragment are null片段中的视图为空
【发布时间】:2020-06-11 15:40:18
【问题描述】:

我仍在开发我的 Android 应用程序。现在我面临这个问题:

代码:

package smoca.ch.kreagen.Fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import io.realm.Realm;
import io.realm.RealmQuery;
import smoca.ch.kreagen.R;
import smoca.ch.kreagen.models.Idea;

public class SingleIdeaFragment extends Fragment{
    private TextView title;
    private TextView owner;
    private TextView description;
    private Realm realm;
    private Idea idea;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.single_idea_fragment_layout, container, false);  // inflate layout for Fragment

        idea = getSingleIdea(getActivity().getBaseContext());

        title = (TextView) container.findViewById(R.id.singleIdeaTitle);
        owner = (TextView) container.findViewById(R.id.singleIdeaOwner);
        description = (TextView) container.findViewById(R.id.singleIdeaDescription);

        title.setText(idea.getTitle());
        owner.setText(idea.getOwnerId());
        description.setText(idea.getText());

        return layout;
    }

    public Idea getSingleIdea(Context ctx) {
        realm = Realm.getInstance(ctx);
        RealmQuery<Idea> ideaQuery = realm.where(Idea.class);
        idea = ideaQuery.findFirst();
        return idea;
    }
}

错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at smoca.ch.kreagen.Fragments.SingleIdeaFragment.onCreateView(SingleIdeaFragment.java:35)

我知道,问题是我的 TextViews(标题、所有者、描述)为空。但我不知道为什么。我将它们分配给布局中的 TextView,引用 ID。

我做错了什么?

【问题讨论】:

    标签: android android-fragments layout null textview


    【解决方案1】:

    当您调用inflater.inflate 时,您将第三个参数attachToRoot 指定为false。这意味着充气器返回的只是R.layout.single_idea_fragment_layout 的视图。因此,应该在 layout 而不是 container 上调用 findViewById

    title = (TextView) layout.findViewById(R.id.singleIdeaTitle);
    owner = (TextView) layout.findViewById(R.id.singleIdeaOwner);
    description = (TextView) layout.findViewById(R.id.singleIdeaDescription);
    

    【讨论】:

    • 其实可以在容器上调用findViewById方法,但前提是调用inflate方法时以true作为最后一个参数(attachToRoot)
    • 呃。它不会修改container 吗?它不会返回一个包含容器所有内容的视图,并附有R.layout.single_idea_fragment_layout 吗?
    • 它会修改container,将新的膨胀视图附加到它的孩子上。有时您不想要这种行为,因此可以选择使用attachToRoot 的方法。无论如何,findViewByIdcontainer 上不起作用的原因恰恰是——膨胀的布局没有container 作为父级,因此在容器上调用findViewById 并没有搜索膨胀布局中的 ID。
    • 明白了,有道理
    • 感谢您的帮助!! :) 解决方案非常简单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多