Problems with rss reader

1

Why do I get this error? java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference

I paste the rss reader code:

public class MainActivity extends Activity
{
Button conectar;
TextView t_cmpFeed;
String url_web = "http://ep00.epimg.net/rss/elpais/portada.xml";
Conection conexion;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    t_cmpFeed = (TextView) findViewById(R.id.txt1);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    conectar = (Button) findViewById(R.id.btnconectar);
    conectar.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.btnconectar:

                    conexion = new Conection();
                    conexion.execute();

                    break;

                default:
                    break;
            }
        }
    });

}


public class Conection extends AsyncTask<Void, Integer, String>
{

    @Override
    protected String doInBackground(Void... params)
    {
        String l_mostrar = " ";
        int i=0, j=0;

        try
        {
            URL url = new URL(url_web);
            HttpURLConnection t_connect = (HttpURLConnection) url.openConnection();
            t_connect.setRequestProperty("User-Agent", "Mozilla/5.0" + "(Windows; Android 6.0; es-ES) Ejemplo HTTP");

            int setRespuesta = t_connect.getResponseCode();

            if(setRespuesta == HttpURLConnection.HTTP_OK)
            {
                BufferedReader rfeed = new BufferedReader( new InputStreamReader(t_connect.getInputStream()));
                String l = rfeed.readLine();

                while (l != null)
                {
                    if(l.indexOf("<title>") >= 0)
                    {
                        i = l.indexOf("<title>")+16;
                        j = l.indexOf("<title>")-3;
                        l_mostrar = l.substring(j,i);
                        l_mostrar += "\n--------------------\n";
                        //l_mostrar += l;
                    }
                    l = rfeed.readLine();
                }
                rfeed.close();
            }

            else
                {
                    Toast.makeText(getApplicationContext(), "No se ha encontrado la conexión", Toast.LENGTH_SHORT).show();
                }
                t_connect.disconnect();

                return l_mostrar;
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }

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

        return null;
    }


    @Override
    protected void onCancelled(String aVoid)
    {
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(String l_mostrar)
    {
        t_cmpFeed.append(l_mostrar);
    }


}


}
    
asked by wololó91 10.05.2017 в 22:33
source

1 answer

0

The problem is generated within AsyncTask in the onPostExecute() method:

 t_cmpFeed.append(l_mostrar);

the instance of TextView t_cmpFeed , has a value of null.

The reason is that you are getting the reference before loading the layout where the TextView is found, make this change within your method onCreate() :

@Override
protected void onCreate(Bundle savedInstanceState)
{
   // t_cmpFeed = (TextView) findViewById(R.id.txt1);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);  //Carga layout.

    t_cmpFeed = (TextView) findViewById(R.id.txt1); //Obtiene referencia de TextView en el Layout activity_main.xml.
   ...
   ...
}

As an extra recommendation, if it is only a value that you are going to show in the TextView you can use the setText() method:

@Override
    protected void onPostExecute(String l_mostrar)
    {
        t_cmpFeed.setText(l_mostrar);
    }
    
answered by 10.05.2017 / 22:56
source