试图从fragment.Getting空指针异常错误创建动态textview?

时间:2017-03-11 23:12:54

标签: android textview

我在java代码中创建了一个线性布局并连接到XML代码,以便在布局中添加所需数量的文本视图。

我通过添加一个textview来测试它,给出了指定的文本,大小等等。它给了我。

我只是把头发拉过来。

package com.example.user.navigationdrawer;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class FirstFragment extends Fragment {

    View myView;
    static final String url = "https://website.com";
    ArrayList<String> outPut = new ArrayList<>();
    LinearLayout diaryLayout = (LinearLayout) myView.getRootView().findViewById(R.id.connections); // compiler giving error here.

    //ArrayList<String> h4 = new ArrayList<>();

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

        myView = inflater.inflate(R.layout.first_layout, container, false);

        new Ann().execute();
        return myView;
    }

    private class Ann extends AsyncTask<Void, Void, Void> {

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

                // Connect to the web site
                Document document = Jsoup.connect(url).get();
                // Get the html document title
                Elements notificationWall = document.select("div[class=flex_column av_one_fifth  flex_column_table_cell av-equal-height-column av-align-top av-zero-column-padding   " + "avia-builder-el-11  el_after_av_one_fifth  el_before_av_one_third]"); //Connect to website.The Notification wall.

                Elements sectionTag = notificationWall.select("section"); //Get section tag.
                Elements contentTitles = sectionTag.select("h4"); // Get H4
                Elements bodyText = sectionTag.select("p"); //Get P
                Elements linkTag = sectionTag.select("a"); //Get links
                Elements images = sectionTag.select("img"); // get image.

                for (int section = 0; section < sectionTag.size(); section++) {
                    outPut.add(sectionTag.get(section).text());
                }

            } catch(IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Set title into TextView
            //here.
            TextView dynamicTextView = new TextView(diaryLayout.getContext());
            dynamicTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            dynamicTextView.setText("TEST");
            dynamicTextView.setTextSize(30);

            diaryLayout.addView(dynamicTextView);

        }
    }
}

1 个答案:

答案 0 :(得分:0)

diaryLayout初始化移至onCreateView

LinearLayout diaryLayout;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.first_layout, container, false);
    diaryLayout = (LinearLayout) myView.findViewById(R.id.connections);
    new Ann().execute();
    return myView;
}