Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试调用虚拟方法_Java_Android_Android Fragments_Android Activity_Nullpointerexception - Fatal编程技术网

Java 尝试调用虚拟方法

Java 尝试调用虚拟方法,java,android,android-fragments,android-activity,nullpointerexception,Java,Android,Android Fragments,Android Activity,Nullpointerexception,我试图在我的onCreateView中获取一些线性布局,但我的应用程序因以下消息而崩溃: 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 代码如下: @Override public View

我试图在我的
onCreateView
中获取一些
线性布局
,但我的应用程序因以下消息而崩溃:

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
代码如下:

@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;
}
这是一个纯
片段
类。调用此
片段的
活动
不包含此
片段
。我做错了吗?
片段是否应始终位于
活动中

根据要求,整个
异步任务

    // 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();
            }
        }
    }
//异步任务
私有类主体扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
mProgressDialog=newprogressdialog(getActivity());
setTitle(“获取您的信息”);
设置消息(“加载…”);
mProgressDialog.setUndeterminate(false);
mProgressDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
文件文件;
试一试{
//连接到网站
doc=Jsoup.connect(url.get();
//使用元素获取元数据
//活动日期
Elements dateElement=doc.select(“span[class=date display single]”;
//活动标题
元素titleElement=doc.select(“h2[property=schema:name]”;
//事件描述
Elements bodyElement=doc.select(“div[class=字段名body字段类型文本,隐藏摘要字段标签]”;
//得到日期
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();
//头条新闻
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();
//获取描述
body1=bodyElement.eq(0).toString();
body1=Jsoup.parse(body1.replaceAll(“
”,即“br2n”)).toString(); body1=Jsoup.parse(body1.replaceAll(“

”,“p2n”)).text(); body1=body1.replaceAll(“br2n”和“\n”); body1=body1.replaceAll(“p2n”,“\n\n”); body2=bodyElement.eq(1).toString(); body2=Jsoup.parse(body2.replaceAll(“
”,即“br2n”)).toString(); body2=Jsoup.parse(body2.replaceAll(“

”,“p2n”)).text(); body2=body2.replaceAll(“br2n”和“\n”); body2=body2.replaceAll(“p2n”,“\n\n”); body3=bodyElement.eq(2).toString(); body3=Jsoup.parse(body3.replaceAll(“
”,即“br2n”)).toString(); body3=Jsoup.parse(body3.replaceAll(“

”,“p2n”)).text(); body3=body3.replaceAll(“br2n”和“\n”); body3=body3.replaceAll(“p2n”,“\n\n”); body4=bodyElement.eq(3).toString(); body4=Jsoup.parse(body4.replaceAll(“
”,“br2n”)).toString(); body4=Jsoup.parse(body4.replaceAll(“

”,“p2n”)).text(); body4=body4.replaceAll(“br2n”和“\n”); body4=body4.replaceAll(“p2n”,“\n\n”); body5=bodyElement.eq(4).toString(); body5=Jsoup.parse(body5.replaceAll(“
”,即“br2n”)).toString(); body5=Jsoup.parse(body5.replaceAll(“

”,“p2n”)).text(); body5=body5.replaceAll(“br2n”和“\n”); body5=body5.replaceAll(“p2n”,“\n\n”); body6=bodyElement.eq(5).toString(); body6=Jsoup.parse(body6.replaceAll(“
”,即“br2n”)).toString(); body6=Jsoup.parse(body6.replaceAll(“

”,“p2n”)).text(); body6=body6.replaceAll(“br2n”和“\n”); body6=body6.replaceAll(“p2n”,“\n\n”); //如果字符串有要剪切的内容,请在标题中剪切日期(13个字符) //让我们希望网站保持这样 如果(head1.length()>0){ head1=head1.子串(13); } 如果(head2.length()>0){ head2=head2.子串(13); } 如果(head3.length()>0){ head3=head3.子串(13); } 如果(head4.length()>0){ head4=head4.子串(13); } 如果(head5.length()>0){ head5=head5.子串(13); } 如果(头6.length()>0){ head6=head6.子串(13); } //获取事件标签 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”); }捕获(例外e){ e、 printStackTrace(); } 返回null; }
 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);
 RobotoTextView date_widget2;
 date_widget2= (RobotoTextView) rootView.findViewById(R.id.date02);
View view = LayoutInflater.from(context).inflate(R.layout.layout_index_bar, null);
container = (LinearLayout) view.findViewById(R.id.indexContainer);