简体   繁体   中英

NullPointerException ArrayAdapter adapter Android XML

How are you? come with android problem, it is a NullPointerException ArrayAdapter adapter.Im a junior development, sorry.

I have this code

private String[] titulares;
private ListView listadoTitulos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Permisos
    StrictMode.ThreadPolicy permiso = new  StrictMode.ThreadPolicy.Builder().permitAll().build();
    //Damos los permisos
    StrictMode.setThreadPolicy(permiso);
    listadoTitulos = (ListView) findViewById(R.id.lvtitulos);
    try {
        url = new URL("http://www.videotutoriales.es/android-xml/cursos.xml");
        leerXml();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,titulares);
    listadoTitulos.setAdapter(adapter);
}

The NullPointerException ArrayAdapter in the logCat

05-12 03:25:18.807  32599-32599/com.cig.trlxml E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cig.trlxml/com.cig.trlxml.MainActivity}: java.lang.NullPointerException

in this line

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,titulares);
    listadoTitulos.setAdapter(adapter);

The XML File is

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:src="@mipmap/logon" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/lvtitulos"
    android:layout_below="@+id/imageView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

what is wrong?

Thank you

Edit

private void leerXml() {
    //Es necesario para utilizar la clase XMLPullParser
    XmlPullParserFactory factory;
    XmlPullParser xml;
    //Variable para los eventos
    int evento;
    boolean titulo;
    ArrayList<String> titulos;
    //Puedo leer el titulo?
    titulo = false;
    titulos = new ArrayList<String>();
    try {
        factory = XmlPullParserFactory.newInstance();
        xml=factory.newPullParser();
        //obtenemos la URL
        xml.setInput(url.openStream(),"UTF-8");

        evento = xml.getEventType();
        //Leer cada uno de los eventos hasta el final
        while (evento != XmlPullParser.END_DOCUMENT){
            switch(evento){
                case XmlPullParser.START_TAG:
                    if(xml.getName().equals("titulo")){
                        titulo = true;
                    }
                    break;
                case  XmlPullParser.TEXT:
                    if(titulo = true){
                        titulos.add(xml.getText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if(xml.getName().equals("titulo")) {
                        titulo = false;
                    }
                    break;
            }
            evento = xml.next();
        }

        titulares = new String[titulos.size()];
        for (int i = 0; i< titulos.size(); i++){
            titulares[i] = titulos.get(i);
        }

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

}

The reason fr your exception is that you neved initialize your titulares array, but try to use it as data when creating your adapter :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,titulares);
    listadoTitulos.setAdapter(adapter);

Just code it like this:

titulares = new String[your_lenght];

Before adapter creating or full it with content if you have it there

The NullPointerException is thrown because it tries to pass the value titularis to the Constructor of ArrayAdapter, but you have never defined titularis, so it is still null.

Try initializing titularis in onCreate() like this

titularis = new String[2];

BEFORE you initilialize the ArrayAdapter

Your leerxml() method doesn't work correctly, check the internet permissions or internet access. Thus the method not running correctly the String array that you are trying to pass in to the ArrayAdapter is not getting initialized and you end up passing null to the ArrayAdapter constructor.

Also you have a issue when inside leerxml() in your line

if(titulo = true){

change it with

if(titulo){

As you can see in the API the ArrayAdapter constructor receives three parameters

ArrayAdapter(Context context, int resource, T[] objects)

  • The current context
  • The resource ID for a layout file containing a TextView to use when instantiating views.
  • The objects to represent in the ListView. This value must never be null.

Try putting some Log messages in your Try - catch block and you will see that the method doesn't run correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM