0

Ive been trying to load data to a listview on an android app

This is my activity that will load the listview:

    public class DetallesCliente extends ActionBarActivity {
    String[] data = {"S","D","A"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detalles_cliente);


    ListView lista = (ListView)findViewById(R.id.listaDetalles);
    ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.list_content, data);
    try{lista.setAdapter(adaptador);}
    catch (Exception e){Log.i("WS", e.toString());}

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

ok, now this is the code in the fragment of this activity

    <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:gravity="center"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
                                        tools:context="com.example.facturas.DetallesCliente$PlaceholderFragment" >

        <ListView
            android:id="@+id/listaDetalles"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="27dp" >
</ListView>

<TextView
    android:id="@+id/tvDetalles"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:text="Detalles del Cliente"
    android:textAppearance="?android:attr/textAppearanceMedium" />

the problem is when i try to load the data it is throwing me a null pointer exception, i have no fragment class cause i dont understand them, i tried to modify text from a textview and it didnt work neither

what is the correct way to allow the listview to be edited

Diego Rivera
  • 403
  • 1
  • 5
  • 19

1 Answers1

0

That's not code in fragment...i hope you intended to write activity_detalles_cliente.xml code and moreover it's layout for PlaceholderFragment Fragment according to line :

tools:context="com.example.facturas.DetallesCliente$PlaceholderFragment"

You are using it for mainactivity instead(it was unable to find listview R.id.listaDetalles as it is in PlaceholderFragment's layout file) so it's showing a NullPointerException.

i have no fragment class cause i dont understand them

If i got you correct how come you have no PlaceholderFragment class and trying to create it's instance?

METHOD 1:

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_detalles_cliente, container, false);
    ListView lista = (ListView)findViewById(R.id.listaDetalles);
    ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getApplicationContext(),
       android.R.layout.list_content, data);
     try{
         lista.setAdapter(adaptador);
        }catch (Exception e){
           Log.i("WS", e.toString());}
    return rootView;
}

}

add the above code (Don't forget to remove ListView lista from mainActivity) .

METHOD 2: if you don't need fragments then just remove

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
}

above from mainactivity and change line

tools:context="com.example.facturas.DetallesCliente$PlaceholderFragment"

to

tools:context="com.example.facturas.DetallesCliente"
  • Thanks for the answer but as the question is a duplicate of another (http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate) I wouldn't count on it surviving too long. Don't let that slow you down though; it was a good answer. – indivisible May 30 '14 at 19:17