尝试调用虚函数 [英] Attempt to invoke virtual method

查看:208
本文介绍了尝试调用虚函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一些 LinearLayouts 在我的 onCreateView ,但我的应用程序崩溃,出现以下消息:

  23430-23430 /? W / System.err的:显示java.lang.NullPointerException:尝试在空对象引用调用虚拟方法android.view.View android.view.View.findViewById(INT)'
 

下面是code:

  @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
                         捆绑savedInstanceState){

    查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);

    新的机构()执行()。

        card01 =(的LinearLayout)getView()findViewById(R.id.card01)。
        card02 =(的LinearLayout)getView()findViewById(R.id.card02)。
        card03 =(的LinearLayout)getView()findViewById(R.id.card03)。
        card04 =(的LinearLayout)getView()findViewById(R.id.card04)。
        card05 =(的LinearLayout)getView()findViewById(R.id.card05)。
        card06 =(的LinearLayout)getView()findViewById(R.id.card06)。


        card01.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                Toast.makeText(getActivity(),卡被点击的,Toast.LENGTH_SHORT).show();
            }
        });

    返回rootView;
}
 

这是一个纯粹的片段类。该活动调用该片段不包含此片段 。我是不是做错了?如若片段永远是活动中的

根据要求整个的AsyncTask

  // AsyncTask的
    私有类主体延伸的AsyncTask<虚空,虚空,虚空> {

        @覆盖
        在preExecute保护无效(){
            super.on preExecute();

            mProgressDialog =新ProgressDialog(getActivity());
            mProgressDialog.setTitle(抓取您的信息);
            mProgressDialog.setMessage(载入中...);
            mProgressDialog.setIndeterminate(假);
            mProgressDialog.show();

        }

        @覆盖
        保护无效doInBackground(虚空...... PARAMS){

            文档文档;

            尝试 {
                //连接到网站
                DOC = Jsoup.connect(URL)获得();

                //使用元素来获取元数据

                //事件日期
                元素dateElement = doc.select(跨度[CLASS =日期显示单]);

                //事件的标题
                元素titleElement = doc.select(H2 [属性=模式:名称]);

                //事件的说明
                元素bodyElement = doc.select(DIV [级=字段字段名-body字段类型文本与 - 汇总字段标签隐藏]);

                //获取日期
                日期1 = dateElement.eq(0)的.text();
                DATE2 = dateElement.eq(1)的.text();
                date3 = dateElement.eq(2)的.text();
                date4 = dateElement.eq(3)的.text();
                date5 = dateElement.eq(4)的.text();
                date6 = dateElement.eq(5)的.text();

                //头条新闻
                头1 = titleElement.eq(0)的.text();
                HEAD2 = titleElement.eq(1)的.text();
                头像3 = titleElement.eq(2)的.text();
                头像4 = titleElement.eq(3)的.text();
                头像5 = titleElement.eq(4)的.text();
                头像6 = titleElement.eq(5)的.text();

                //获取描述
                body1 = bodyElement.eq(0)的ToString();
                body1 = Jsoup.parse(body1.replaceAll(&所述峰; br />中,br2n))的toString()。
                body1 = Jsoup.parse(body1.replaceAll(&所述; / P>中,P2N))的文本()。
                body1 = body1.replaceAll(br2n,\ N);
                body1 = body1.replaceAll(P2N,\ñ\ N);

                body2 = bodyElement.eq(1)的ToString();
                body2 = Jsoup.parse(body2.replaceAll(&所述峰; br />中,br2n))的toString()。
                body2 = Jsoup.parse(body2.replaceAll(&所述; / P>中,P2N))的文本()。
                body2 = body2.replaceAll(br2n,\ N);
                body2 = body2.replaceAll(P2N,\ñ\ N);

                body3 = bodyElement.eq(2)的ToString();
                body3 = Jsoup.parse(body3.replaceAll(&所述峰; br />中,br2n))的toString()。
                body3 = Jsoup.parse(body3.replaceAll(&所述; / P>中,P2N))的文本()。
                body3 = body3.replaceAll(br2n,\ N);
                body3 = body3.replaceAll(P2N,\ñ\ N);

                body4 = bodyElement.eq(3)的ToString();
                body4 = Jsoup.parse(body4.replaceAll(&所述峰; br />中,br2n))的toString()。
                body4 = Jsoup.parse(body4.replaceAll(&所述; / P>中,P2N))的文本()。
                body4 = body4.replaceAll(br2n,\ N);
                body4 = body4.replaceAll(P2N,\ñ\ N);

                body5 = bodyElement.eq(4)的ToString();
                body5 = Jsoup.parse(body5.replaceAll(&所述峰; br />中,br2n))的toString()。
                body5 = Jsoup.parse(body5.replaceAll(&所述; / P>中,P2N))的文本()。
                body5 = body5.replaceAll(br2n,\ N);
                body5 = body5.replaceAll(P2N,\ñ\ N);

                body6 = bodyElement.eq(5)的ToString();
                body6 = Jsoup.parse(body6.replaceAll(&所述峰; br />中,br2n))的toString()。
                body6 = Jsoup.parse(body6.replaceAll(&所述; / P>中,P2N))的文本()。
                body6 = body6.replaceAll(br2n,\ N);
                body6 = body6.replaceAll(P2N,\ñ\ N);

                //切出的头条新闻(13个字符)的日期,如果字符串有一些削减
                //让我们希望网站保持这样的
                如果(head1.length()大于0){
                    头1 = head1.substring(13);
                }
                如果(head2.length()大于0){
                    HEAD2 = head2.substring(13);
                }
                如果(head3.length()大于0){
                    头像3 = head3.substring(13);
                }
                如果(head4.length()大于0){
                    头像4 = head4.substring(13);
                }
                如果(head5.length()大于0){
                    头像5 = head5.substring(13);
                }
                如果(head6.length()大于0){
                    头像6 = head6.substring(13);
                }

                //获取事件标签
                AssetManager AM = getActivity()getAssets()。
                BWE = BitmapFactory.de codeStream(am.open(标签/ bwe.jpg));
                commaklar = BitmapFactory.de codeStream(am.open(标签/ commaklar.jpg));
// MUSICA = BitmapFactory.de codeStream(am.open(标签/ musica.jpg));
                doualatag = BitmapFactory.de codeStream(am.open(标签/ doualatag.jpg));
                synestesia = BitmapFactory.de codeStream(am.open(标签/ synestesia.jpg));
                杜阿拉= BitmapFactory.de codeStream(am.open(标签/ douala.jpg));
//旦= BitmapFactory.de codeStream(am.open(标签/ tanzen.jpg));
// NACHT = BitmapFactory.de codeStream(am.open(标签/ nacht.jpg));
            }赶上(例外五){
                e.printStackTrace();
            }
            返回null;
        }

        @覆盖
        保护无效onPostExecute(无效的结果){
            mProgressDialog.dismiss();

            尝试 {

                RobotoTextView date_widget1 =(RobotoTextView)getActivity()findViewById(R.id.date01)。
                date_widget1.setText(DATE1);

                RobotoTextView head_widget1 =(RobotoTextView)getActivity()findViewById(R.id.head01)。
                head_widget1.setText(头1);

                RobotoTextView body_widget1 =(RobotoTextView)getActivity()findViewById(R.id.body1)。
                Whitelist.simpleText()addTags(BR,P)。
                body_widget1.setText(body1);

                ImageView的卷标1 =(ImageView的)getView()findViewById(R.id.label1)。

                如果(head1.contains(BLACKWHITE)){
                    label1.setImageBitmap(BWE);
                }
                如果(date1.contains(做)){
                    ;
                    label1.setImageBitmap(doualatag);
                }
                如果(head1.contains(COMMAKLAR)){
                    label1.setImageBitmap(commaklar);
                }
                如果(head1.toLowerCase()包含(synestesia)| head1.contains(SYNESTESIA)){
                    label1.setImageBitmap(synestesia);
                }
                如果(label1.getDrawable()== NULL){
                    label1.setImageBitmap(杜阿拉);
                }

                RobotoTextView date_widget2 =(RobotoTextView)getActivity()findViewById(R.id.date02)。
                date_widget2.setText(DATE2);

                RobotoTextView head_widget2 =(RobotoTextView)getActivity()findViewById(R.id.head02)。
                head_widget2.setText(HEAD2);

                RobotoTextView body_widget2 =(RobotoTextView)getActivity()findViewById(R.id.body2)。
                body_widget2.setText(body2);

                ImageView的标签2 =(ImageView的)getView()findViewById(R.id.label2)。

                如果(head2.contains(BLACKWHITE)){
                    label2.setImageBitmap(BWE);
                }
                如果(date2.contains(做)){
                    label2.setImageBitmap(doualatag);
                }
                如果(head2.contains(COMMAKLAR)){
                    label2.setImageBitmap(commaklar);
                }
                如果(head2.toLowerCase()。包含(synestesia)){
                    label2.setImageBitmap(synestesia);
                }
                如果(label2.getDrawable()== NULL){
                    label2.setImageBitmap(杜阿拉);
                }

                RobotoTextView date_widget3 =(RobotoTextView)getActivity()findViewById(R.id.date03)。
                date_widget3.setText(date3);

                RobotoTextView head_widget3 =(RobotoTextView)getActivity()findViewById(R.id.head03)。
                head_widget3.setText(头像3);

                RobotoTextView body_widget3 =(RobotoTextView)getActivity()findViewById(R.id.body3)。
                body_widget3.setText(body3);

                ImageView的LABEL3 =(ImageView的)getView()findViewById(R.id.label3)。

                如果(head3.contains(BLACKWHITE)){
                    label3.setImageBitmap(BWE);
                }
                如果(date3.contains(做)){
                    label3.setImageBitmap(doualatag);
                }
                如果(head3.contains(COMMAKLAR)){
                    label3.setImageBitmap(commaklar);
                }
                如果(head3.toLowerCase()。包含(synestesia)){
                    label3.setImageBitmap(synestesia);
                }
                如果(label3.getDrawable()== NULL){
                    label3.setImageBitmap(杜阿拉);
                }

                RobotoTextView date_widget4 =(RobotoTextView)getActivity()findViewById(R.id.date04)。
                date_widget4.setText(date4);

                RobotoTextView head_widget4 =(RobotoTextView)getActivity()findViewById(R.id.head04)。
                head_widget4.setText(头像4);

                RobotoTextView body_widget4 =(RobotoTextView)getActivity()findViewById(R.id.body4)。
                body_widget4.setText(body4);

                ImageView的label4 =(ImageView的)getView()findViewById(R.id.label4)。

                如果(head4.contains(BLACKWHITE)){
                    label4.setImageBitmap(BWE);
                }
                如果(date4.contains(做)){
                    label4.setImageBitmap(doualatag);
                }
                如果(head4.contains(COMMAKLAR)){
                    label4.setImageBitmap(commaklar);
                }
                如果(head4.toLowerCase()。包含(synestesia)){
                    label4.setImageBitmap(synestesia);
                }
                如果(label4.getDrawable()== NULL){
                    label4.setImageBitmap(杜阿拉);
                }

                如果(!head5.equals()){
                    RobotoTextView date_widget5 =(RobotoTextView)getActivity()findViewById(R.id.date05)。
                    date_widget5.setText(date5);

                    RobotoTextView head_widget5 =(RobotoTextView)getActivity()findViewById(R.id.head05)。
                    head_widget5.setText(头像5);

                    RobotoTextView body_widget5 =(RobotoTextView)getActivity()findViewById(R.id.body5)。
                    body_widget5.setText(body5);

                    ImageView的label5 =(ImageView的)getView()findViewById(R.id.label5)。

                    如果(head5.contains(BLACKWHITE)){
                        label5.setImageBitmap(BWE);
                    }
                    如果(date5.contains(做)){
                        label5.setImageBitmap(doualatag);
                    }
                    如果(head5.contains(COMMAKLAR)){
                        label5.setImageBitmap(commaklar);
                    }
                    如果(head5.toLowerCase()。包含(synestesia)){
                        label5.setImageBitmap(synestesia);
                    }
                    如果(label5.getDrawable()== NULL){
                        label5.setImageBitmap(杜阿拉);
                    }
                }

                如果(!head6.equals()){
                    RobotoTextView date_widget6 =(RobotoTextView)getActivity()findViewById(R.id.date06)。
                    date_widget6.setText(date6);

                    RobotoTextView head_widget6 =(RobotoTextView)getActivity()findViewById(R.id.head06)。
                    head_widget6.setText(头像6);

                    RobotoTextView body_widget6 =(RobotoTextView)getActivity()findViewById(R.id.body6)。
                    body_widget6.setText(body6);

                    ImageView的label6 =(ImageView的)getView()findViewById(R.id.label6)。

                    如果(head6.contains(BLACKWHITE)){
                        label6.setImageBitmap(BWE);
                    }
                    如果(date6.contains(做)){
                        label6.setImageBitmap(doualatag);
                    }
                    如果(head6.contains(COMMAKLAR)){
                        label6.setImageBitmap(commaklar);
                    }
                    如果(head6.toLowerCase()。包含(synestesia)){
                        label6.setImageBitmap(synestesia);
                    }
                    如果(label6.getDrawable()== NULL){
                        label6.setImageBitmap(杜阿拉);
                    }
                }

            }赶上(NullPointerException异常E){
                e.printStackTrace();
            }
        }
    }
 

解决方案

更​​改为

  card01 =(的LinearLayout)rootView.findViewById(R.id.card01);
 card02 =(的LinearLayout)rootView.findViewById(R.id.card02);
 card03 =(的LinearLayout)rootView.findViewById(R.id.card03);
 card04 =(的LinearLayout)rootView.findViewById(R.id.card04);
 card05 =(的LinearLayout)rootView.findViewById(R.id.card05);
 card06 =(的LinearLayout)rootView.findViewById(R.id.card06);
 

您膨胀的布局。的意见属于充气布局。因此,使用视图对象初始化意见 onCreateView

片段是由Acitvity托管

您可以在 onActivityCreated 使用 getView 和初始化意见

初​​始化您的TextView的也在 onCreateView 。由于AsyncTask的是,你可以更新UI有一个内部类

声明

  RobotoTextView date_widget2;
 

作为实例变量

然后在onCreateView

  date_widget2 =(RobotoTextView)rootView.findViewById(R.id.date02);
 

I am trying to get some LinearLayoutsin my onCreateView but my App is crashing with the following message:

23430-23430/? W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

Here is the code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    new Body().execute();

        card01 = (LinearLayout) getView().findViewById(R.id.card01);
        card02 = (LinearLayout) getView().findViewById(R.id.card02);
        card03 = (LinearLayout) getView().findViewById(R.id.card03);
        card04 = (LinearLayout) getView().findViewById(R.id.card04);
        card05 = (LinearLayout) getView().findViewById(R.id.card05);
        card06 = (LinearLayout) getView().findViewById(R.id.card06);


        card01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Card Clicked", Toast.LENGTH_SHORT).show();
            }
        });

    return rootView;
}

This is a pure Fragment class. The Activity which calls this Fragment does not contain this Fragment. Am I doing it wrong? Should the Fragmentalways be within the Activity?

As requested the whole AsyncTask:

    // AsyncTask
    private class Body extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            mProgressDialog = new ProgressDialog(getActivity());
            mProgressDialog.setTitle("Fetching your info");
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.show();

        }

        @Override
        protected Void doInBackground(Void... params) {

            Document doc;

            try {
                // Connect to the web site
                doc = Jsoup.connect(url).get();

                // Using Elements to get the Meta data

                //Date of Event
                Elements dateElement = doc.select("span[class=date-display-single]");

                //Headline of Event
                Elements titleElement = doc.select("h2[property=schema:name]");

                //Description of Event
                Elements bodyElement = doc.select("div[class=field field-name-body field-type-text-with-summary field-label-hidden]");

                // get the dates
                date1 = dateElement.eq(0).text();
                date2 = dateElement.eq(1).text();
                date3 = dateElement.eq(2).text();
                date4 = dateElement.eq(3).text();
                date5 = dateElement.eq(4).text();
                date6 = dateElement.eq(5).text();

                // get headlines
                head1 = titleElement.eq(0).text();
                head2 = titleElement.eq(1).text();
                head3 = titleElement.eq(2).text();
                head4 = titleElement.eq(3).text();
                head5 = titleElement.eq(4).text();
                head6 = titleElement.eq(5).text();

                // get description
                body1 = bodyElement.eq(0).toString();
                body1 = Jsoup.parse(body1.replaceAll("<br />", "br2n")).toString();
                body1 = Jsoup.parse(body1.replaceAll("</p>", "p2n")).text();
                body1 = body1.replaceAll("br2n", "\n");
                body1 = body1.replaceAll("p2n", "\n\n");

                body2 = bodyElement.eq(1).toString();
                body2 = Jsoup.parse(body2.replaceAll("<br />", "br2n")).toString();
                body2 = Jsoup.parse(body2.replaceAll("</p>", "p2n")).text();
                body2 = body2.replaceAll("br2n", "\n");
                body2 = body2.replaceAll("p2n", "\n\n");

                body3 = bodyElement.eq(2).toString();
                body3 = Jsoup.parse(body3.replaceAll("<br />", "br2n")).toString();
                body3 = Jsoup.parse(body3.replaceAll("</p>", "p2n")).text();
                body3 = body3.replaceAll("br2n", "\n");
                body3 = body3.replaceAll("p2n", "\n\n");

                body4 = bodyElement.eq(3).toString();
                body4 = Jsoup.parse(body4.replaceAll("<br />", "br2n")).toString();
                body4 = Jsoup.parse(body4.replaceAll("</p>", "p2n")).text();
                body4 = body4.replaceAll("br2n", "\n");
                body4 = body4.replaceAll("p2n", "\n\n");

                body5 = bodyElement.eq(4).toString();
                body5 = Jsoup.parse(body5.replaceAll("<br />", "br2n")).toString();
                body5 = Jsoup.parse(body5.replaceAll("</p>", "p2n")).text();
                body5 = body5.replaceAll("br2n", "\n");
                body5 = body5.replaceAll("p2n", "\n\n");

                body6 = bodyElement.eq(5).toString();
                body6 = Jsoup.parse(body6.replaceAll("<br />", "br2n")).toString();
                body6 = Jsoup.parse(body6.replaceAll("</p>", "p2n")).text();
                body6 = body6.replaceAll("br2n", "\n");
                body6 = body6.replaceAll("p2n", "\n\n");

                //cut out the date in the headlines (13 characters) if the String has something to cut
                //Let's  hope the website stays like this
                if (head1.length() > 0) {
                    head1 = head1.substring(13);
                }
                if (head2.length() > 0) {
                    head2 = head2.substring(13);
                }
                if (head3.length() > 0) {
                    head3 = head3.substring(13);
                }
                if (head4.length() > 0) {
                    head4 = head4.substring(13);
                }
                if (head5.length() > 0) {
                    head5 = head5.substring(13);
                }
                if (head6.length() > 0) {
                    head6 = head6.substring(13);
                }

                //Get Event Labels
                AssetManager am = getActivity().getAssets();
                bwe = BitmapFactory.decodeStream(am.open("label/bwe.jpg"));
                commaklar = BitmapFactory.decodeStream(am.open("label/commaklar.jpg"));
//                musica = BitmapFactory.decodeStream(am.open("label/musica.jpg"));
                doualatag = BitmapFactory.decodeStream(am.open("label/doualatag.jpg"));
                synestesia = BitmapFactory.decodeStream(am.open("label/synestesia.jpg"));
                douala = BitmapFactory.decodeStream(am.open("label/douala.jpg"));
//                tanzen = BitmapFactory.decodeStream(am.open("label/tanzen.jpg"));
//                nacht = BitmapFactory.decodeStream(am.open("label/nacht.jpg"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mProgressDialog.dismiss();

            try {

                RobotoTextView date_widget1 = (RobotoTextView) getActivity().findViewById(R.id.date01);
                date_widget1.setText(date1);

                RobotoTextView head_widget1 = (RobotoTextView) getActivity().findViewById(R.id.head01);
                head_widget1.setText(head1);

                RobotoTextView body_widget1 = (RobotoTextView) getActivity().findViewById(R.id.body1);
                Whitelist.simpleText().addTags("br", "p");
                body_widget1.setText(body1);

                ImageView label1 = (ImageView) getView().findViewById(R.id.label1);

                if (head1.contains("BLACKWHITE")) {
                    label1.setImageBitmap(bwe);
                }
                if (date1.contains("Do")) {
                    ;
                    label1.setImageBitmap(doualatag);
                }
                if (head1.contains("COMMAKLAR")) {
                    label1.setImageBitmap(commaklar);
                }
                if (head1.toLowerCase().contains("synestesia") | head1.contains("SYNESTESIA")) {
                    label1.setImageBitmap(synestesia);
                }
                if (label1.getDrawable() == null) {
                    label1.setImageBitmap(douala);
                }

                RobotoTextView date_widget2 = (RobotoTextView) getActivity().findViewById(R.id.date02);
                date_widget2.setText(date2);

                RobotoTextView head_widget2 = (RobotoTextView) getActivity().findViewById(R.id.head02);
                head_widget2.setText(head2);

                RobotoTextView body_widget2 = (RobotoTextView) getActivity().findViewById(R.id.body2);
                body_widget2.setText(body2);

                ImageView label2 = (ImageView) getView().findViewById(R.id.label2);

                if (head2.contains("BLACKWHITE")) {
                    label2.setImageBitmap(bwe);
                }
                if (date2.contains("Do")) {
                    label2.setImageBitmap(doualatag);
                }
                if (head2.contains("COMMAKLAR")) {
                    label2.setImageBitmap(commaklar);
                }
                if (head2.toLowerCase().contains("synestesia")) {
                    label2.setImageBitmap(synestesia);
                }
                if (label2.getDrawable() == null) {
                    label2.setImageBitmap(douala);
                }

                RobotoTextView date_widget3 = (RobotoTextView) getActivity().findViewById(R.id.date03);
                date_widget3.setText(date3);

                RobotoTextView head_widget3 = (RobotoTextView) getActivity().findViewById(R.id.head03);
                head_widget3.setText(head3);

                RobotoTextView body_widget3 = (RobotoTextView) getActivity().findViewById(R.id.body3);
                body_widget3.setText(body3);

                ImageView label3 = (ImageView) getView().findViewById(R.id.label3);

                if (head3.contains("BLACKWHITE")) {
                    label3.setImageBitmap(bwe);
                }
                if (date3.contains("Do")) {
                    label3.setImageBitmap(doualatag);
                }
                if (head3.contains("COMMAKLAR")) {
                    label3.setImageBitmap(commaklar);
                }
                if (head3.toLowerCase().contains("synestesia")) {
                    label3.setImageBitmap(synestesia);
                }
                if (label3.getDrawable() == null) {
                    label3.setImageBitmap(douala);
                }

                RobotoTextView date_widget4 = (RobotoTextView) getActivity().findViewById(R.id.date04);
                date_widget4.setText(date4);

                RobotoTextView head_widget4 = (RobotoTextView) getActivity().findViewById(R.id.head04);
                head_widget4.setText(head4);

                RobotoTextView body_widget4 = (RobotoTextView) getActivity().findViewById(R.id.body4);
                body_widget4.setText(body4);

                ImageView label4 = (ImageView) getView().findViewById(R.id.label4);

                if (head4.contains("BLACKWHITE")) {
                    label4.setImageBitmap(bwe);
                }
                if (date4.contains("Do")) {
                    label4.setImageBitmap(doualatag);
                }
                if (head4.contains("COMMAKLAR")) {
                    label4.setImageBitmap(commaklar);
                }
                if (head4.toLowerCase().contains("synestesia")) {
                    label4.setImageBitmap(synestesia);
                }
                if (label4.getDrawable() == null) {
                    label4.setImageBitmap(douala);
                }

                if (!head5.equals("")) {
                    RobotoTextView date_widget5 = (RobotoTextView) getActivity().findViewById(R.id.date05);
                    date_widget5.setText(date5);

                    RobotoTextView head_widget5 = (RobotoTextView) getActivity().findViewById(R.id.head05);
                    head_widget5.setText(head5);

                    RobotoTextView body_widget5 = (RobotoTextView) getActivity().findViewById(R.id.body5);
                    body_widget5.setText(body5);

                    ImageView label5 = (ImageView) getView().findViewById(R.id.label5);

                    if (head5.contains("BLACKWHITE")) {
                        label5.setImageBitmap(bwe);
                    }
                    if (date5.contains("Do")) {
                        label5.setImageBitmap(doualatag);
                    }
                    if (head5.contains("COMMAKLAR")) {
                        label5.setImageBitmap(commaklar);
                    }
                    if (head5.toLowerCase().contains("synestesia")) {
                        label5.setImageBitmap(synestesia);
                    }
                    if (label5.getDrawable() == null) {
                        label5.setImageBitmap(douala);
                    }
                }

                if (!head6.equals("")) {
                    RobotoTextView date_widget6 = (RobotoTextView) getActivity().findViewById(R.id.date06);
                    date_widget6.setText(date6);

                    RobotoTextView head_widget6 = (RobotoTextView) getActivity().findViewById(R.id.head06);
                    head_widget6.setText(head6);

                    RobotoTextView body_widget6 = (RobotoTextView) getActivity().findViewById(R.id.body6);
                    body_widget6.setText(body6);

                    ImageView label6 = (ImageView) getView().findViewById(R.id.label6);

                    if (head6.contains("BLACKWHITE")) {
                        label6.setImageBitmap(bwe);
                    }
                    if (date6.contains("Do")) {
                        label6.setImageBitmap(doualatag);
                    }
                    if (head6.contains("COMMAKLAR")) {
                        label6.setImageBitmap(commaklar);
                    }
                    if (head6.toLowerCase().contains("synestesia")) {
                        label6.setImageBitmap(synestesia);
                    }
                    if (label6.getDrawable() == null) {
                        label6.setImageBitmap(douala);
                    }
                }

            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
    }

解决方案

Change to

 card01 = (LinearLayout) rootView.findViewById(R.id.card01);
 card02 = (LinearLayout) rootView.findViewById(R.id.card02);
 card03 = (LinearLayout) rootView.findViewById(R.id.card03);
 card04 = (LinearLayout) rootView.findViewById(R.id.card04);
 card05 = (LinearLayout) rootView.findViewById(R.id.card05);
 card06 = (LinearLayout) rootView.findViewById(R.id.card06);

You inflate a layout. the views belong to the inflated layout. So use the view object to initialize views in onCreateView

Fragment is hosted by a Acitvity

You can use getView in onActivityCreated and initialize views

Initialize your TextView's also in onCreateView. Since Asynctask is a inner class you can update ui there

Declare

 RobotoTextView date_widget2;

as instance variable

Then in onCreateView

 date_widget2= (RobotoTextView) rootView.findViewById(R.id.date02);

这篇关于尝试调用虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆