Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

android - Views in Fragment are null

Im still working on my Android App. Now im facing this problem:

Code:

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;
    }
}

Error:

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)

I know, the problem is that my TextViews (title, owner, description) are null. But I don't know why. I assigned them to the TextView's from the layout, referencing the IDs.

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you called inflater.inflate, you specified your third parameter, attachToRoot, as false. This means that what gets returned by the inflater is just the view for R.layout.single_idea_fragment_layout. Thus, findViewById should be called on layout rather than container

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...